362 - 18,000 Seconds Remaining

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

User avatar
kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

Re: 362 - IO tricky samples

Post by kbr_iut » Sun Nov 23, 2008 3:40 am

abid_iut.
check ur pm inbox.
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................

vizardo
New poster
Posts: 8
Joined: Sun Mar 15, 2009 9:42 pm

362 - time limit exceeded... pls help...

Post by vizardo » Sun Apr 26, 2009 1:02 pm

I really don't know how to optimize the code more than this... I got time limit exceeded.. :( .. please help...!!

Code: Select all

import java.util.*;
import java.io.*;

public class Main{
	
	public static void main(String[] args){
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			StringTokenizer token = new StringTokenizer(br.readLine());
			
			int number = 0;
			
			int size = Integer.parseInt(token.nextToken());
				
			while (size > 0){
				int sum = 0;
				int sumTemp = 0;
				int count = 0;
				int value = 0;
				int totalTime = 0;
				int timeRemain = 0;
				
				if (number != 0) System.out.println("");
				
				System.out.println("Output for data set " + ++number + ", " + size + " bytes:");
				
				while (sum != size){
					if (count != 5){
						token = new StringTokenizer(br.readLine());
						value = Integer.parseInt(token.nextToken());
						
						sum = sum + value;
						sumTemp = sumTemp + value;
						count++;
						totalTime++;
					}
					else{
						if (sumTemp != 0){
							timeRemain = (int) Math.ceil((((size - sum)<<2) + (size - sum)) / (double)sumTemp);
							System.out.println("   Time remaining: " + timeRemain + " seconds");
						}
						else{
							System.out.println("   Time remaining: stalled");
						}
						
						count = 0;
						sumTemp = 0;
					}
				}
				
				if (count == 5){
					System.out.println("   Time remaining: 0 seconds");
				}
				
				System.out.println("Total time: " + totalTime + " seconds");
				
				token = new StringTokenizer(br.readLine());
				size = Integer.parseInt(token.nextToken());
			}
			
			br.close();
		}
		catch(Exception e){
			System.exit(0);
		}
	}
}

braincoder
New poster
Posts: 1
Joined: Fri May 15, 2009 6:09 am

362 WA

Post by braincoder » Fri May 15, 2009 10:41 am

Any one could spot out the error?

Code: Select all

[code]import java.util.ArrayList;
import java.util.Scanner;
public class Main {

    public static void main (String args[])
    {
        Scanner in = new Scanner(System.in);
        long totalByte = in.nextLong();
        
        ArrayList second = new ArrayList();
        long eachByte =0;
        long time = 0;
        long round= 1;
        long remainByte = 0;
        long byteTransfer= 0;
        double r=0.0;
        int count = 0;

        while (totalByte!=0)
        {
            remainByte = totalByte;
            while (remainByte>0)
            {
                eachByte = in.nextLong();
                byteTransfer +=eachByte;
                remainByte -= eachByte; // decrease bytes
                time++; // totall seconds
                count++;
                
                if (count==5)
                {
                    count = 0;                    
                    if (byteTransfer!=0)
                    {                                           
                         r = (remainByte/(byteTransfer/5.0));
                         second.add((long)Math.ceil(r));
                    }
                    else
                    {
                        second.add("d");
                    }                  
                    byteTransfer=0;
                }
            }            
            
        System.out.println("Output for data set "+round+", "+totalByte+" bytes:");       
        
        //long _5= time-5;
        
        //if (_5>=0)
        //{
           if (second.size()!=0)
           {
                for (int i=0; i<second.size();i++)
                {
                if (second.get(i).equals("d"))
                System.out.println("   Time remaining: stalled");
                else
                System.out.println("   Time remaining: "+second.get(i)+ " seconds");
                }
           }
        //}
        System.out.println("Total time: "+time+ " seconds");
        System.out.println();
        
        round++;
        second.clear();
        byteTransfer = 0;
        count = 0;
        r =0.0;
        time =0;
        totalByte = in.nextLong();
        }        
    }
}
[/code]

mustak
New poster
Posts: 8
Joined: Sun Sep 06, 2009 5:11 pm
Contact:

ACM NO : 362(18000 Seconds remaining)

Post by mustak » Sun Sep 06, 2009 5:25 pm

Code: Select all

Acc At last
:D

mustak
New poster
Posts: 8
Joined: Sun Sep 06, 2009 5:11 pm
Contact:

Re: 362 - IO tricky samples

Post by mustak » Wed Sep 09, 2009 7:34 am

Its a really interesting problem to me,it seems very easy at 1st look.But now i got 10 time WA on that and still i cannot understand where my problem is.
I give my code below,plz try to help me with giving correction or any type of tips for the code.

Code: Select all

Code Removed After Accepted.
Last edited by mustak on Mon Apr 26, 2010 11:02 pm, edited 1 time in total.

matthiasbreda
New poster
Posts: 2
Joined: Tue Sep 29, 2009 5:11 am

Re: 362 - IO tricky samples

Post by matthiasbreda » Tue Sep 29, 2009 5:30 am

I got 15 time WA --'
i dont know where is my problem,
My code is below,plz heelpp me

Code: Select all

#include <stdio.h>
#include <math.h>

main()
{
      long le=0,x=0;
      scanf("%i",&le);
      while(le!=0)
      {
           x++;
           long sec=0,sec2=0,cont=0,cont2=0,otra,b;
           double remainSeconds;
           printf("Output for data set %d, %d bytes:\n",x,le);
           while(le>cont){
           scanf("%i",&otra);
           cont=cont+otra;
           cont2=cont2+otra;
           sec++;
           sec2++;
            if (sec2==5)
            {
               int remainbyte=le-cont;
               remainSeconds=remainbyte/(cont2/5.0);
      
        
               if(cont2==0){
               printf("  Time remaining: stalled\n");            
               }else{
                     printf("  Time remaining: %d seconds\n",long((ceil(remainSeconds))));
                     }
               sec2=0;     
               cont2=0;         
            }
                                          
          }
        printf("Total time: %d seconds\n", sec);
        scanf("%i", &le);
         printf("\n");
         
            }   
}

matthiasbreda
New poster
Posts: 2
Joined: Tue Sep 29, 2009 5:11 am

362 - (18000 Seconds remaining) - WA

Post by matthiasbreda » Tue Sep 29, 2009 5:33 am

Hey guys,
i dont understand,i got 15 time WA,
plz help me!!!

Code: Select all

#include <stdio.h>
#include <math.h>

main()
{
      long le=0,x=0;
      scanf("%i",&le);
      while(le!=0)
      {
           x++;
           long sec2=0,sec=0,cont=0,cont2=0,otra,b;
           double remainSeconds;
           printf("Output for data set %d, %d bytes:\n",x,le);
           while(le>cont){
           scanf("%i",&otra);
           cont=cont+otra;
           cont2=cont2+otra;
           sec++;
           sec2++;
            if (sec2==5)
            {
               int remainbyte=le-cont;
               remainSeconds=remainbyte/((float)cont2/5.0);
      
        
               if(cont2==0){
               printf("  Time remaining: stalled\n");            
               }else{
                     printf("  Time remaining: %d seconds\n",long((ceil(remainSeconds))));
                     }
               sec2=0;     
               cont2=0;         
            }
                                          
          }
        printf("Total time: %d seconds\n", sec);
        scanf("%i", &le);
         printf("\n");
         
            }   
}

Mostafa_Nageeb
New poster
Posts: 1
Joined: Tue Jul 20, 2010 6:57 pm

362 - WA

Post by Mostafa_Nageeb » Tue Jul 20, 2010 9:12 pm

I have written this code for problem 362 but I am getting WA .. anyone suggestions??

Code: Select all

#include <iostream>
#include <string>
#include<sstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#define length(a) (sizeof a / sizeof a[0])
using namespace std;
int main()
{
    //freopen ("a.txt","r",stdin);
    //freopen ("a.out","w",stdout);
    int size,sum,secondsCounter,temp,setCounter=0;
    while(true)
    {
        queue<int> numbers;
        secondsCounter=0;
        sum=0;
        cin>>size;
        if(size==0)
            break;
        setCounter++;
        if(setCounter>1)
            cout<<endl;
        while(sum<size)
        {
            cin>>temp;
            sum+=temp;
            secondsCounter++;
            numbers.push(temp);
        }
        cout<<"Output for data set "<<setCounter<<", "<<size<<" bytes:"<<endl;
        if(numbers.size()<5)
            cout<<"Total time: "<<secondsCounter<<" seconds"<<endl;
        else
        {
            while(!numbers.empty())
            {
                if(numbers.size()<5)
                    break;
                int s=0;int i=0;
                for(i=1;i<=5;i++)
                {
                    s+=numbers.front();
                    numbers.pop();
                }
                if(s==0)
                    cout<<"   Time remaining: stalled"<<endl;
                else
                {
                    sum-=s;
                    double remainSecond; 
                    remainSecond = sum/(s/5.0);
                    //double TR = ceil(sum/avg);
                    cout<<"   Time remaining: "<<round(remainSecond)<<" seconds"<<endl;
                }
            }
            cout<<"Total time: "<<secondsCounter<<" seconds"<<endl;
        }
    }
    cout<<endl;
    //system("pause");
    return 0;
}


Shafaet_du
Experienced poster
Posts: 147
Joined: Mon Jun 07, 2010 11:43 am
Location: University Of Dhaka,Bangladesh
Contact:

Re: 362 - IO tricky samples

Post by Shafaet_du » Fri Jan 14, 2011 2:36 pm

HORRIBLE. DONT WASTE YOUR TIME SOLVING THIS,THERE ARE LOTS OF OTHER PROBLEMS.
after 6 wa i got ac with this:

Code: Select all

double remainSecond = (5*(double)rem)/(double)fsum;
printf("   Time remaining: %d seconds\n",int(ceil(remainSecond)));

User avatar
plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: 362 - IO tricky samples

Post by plamplam » Tue Aug 30, 2011 4:07 pm

One of the worst problems I've seen here :oops: :oops: I feel like kicking my monitor-.- Thanks a lot for the snippet @Shafaet_DU. Thanks to sijan too. Finally got Accepted. :x
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 362 - IO tricky samples

Post by uDebug » Mon Dec 30, 2013 9:11 am

As other people have observed, this problem looks simple but is quite tricky.

There are two things that you need to keep in mind:
(1)
fR0D wrote:

Code: Select all

Got AC.
Changed the line
printf("   Time remaining: %.0f seconds\n",ceil(remainingBytes/((float)transferedBytes/5.0)));
to
printf("   Time remaining: %.0f seconds\n",ceil(remainingBytes/((double)transferedBytes*0.2)));
and GOT AC.
As fR0D mentioned, this line gives a WA

Code: Select all

printf("   Time remaining: %.0f seconds\n",ceil(remainingBytes/(transferedBytes/5.0)));
However, as crazy at this sounds, this gives an AC

Code: Select all

printf("   Time remaining: %.0f seconds\n",ceil(remainingBytes/(transferedBytes*0.2)));
In my program, remainingBytes and transferedBytes are both variables of type double.
(2)
Remember to print a newline after each case - including the last one.

Good luck!
Last edited by uDebug on Sat Mar 01, 2014 1:34 pm, edited 1 time in total.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

kukker
New poster
Posts: 5
Joined: Fri Feb 28, 2014 6:39 pm

Re: 362 - IO tricky samples

Post by kukker » Fri Feb 28, 2014 6:45 pm

Hello!

I have tried almost everything I read in this forum, but every time I got Wrong Answer (but for the examples I got the rigth answers). I think there must be a problem with the output formatting. Could someone help me, because I can not get through this. Here is the code:

Code: Select all

import java.io.*;
import java.util.*;

//ACM 362 18,000 Seconds Remaining

/*
 * The first line telling the size of the file in bytes. 
 * The subsequent lines will be the number of bytes transmitted in each second
 * The end of input will be indicated by a file size of 0 bytes
 * 
 * The output for each data set should begin with a line with the number of the data set 
 * and the size of the file being transferred. 
 * Then, there should be update lines estimating how many seconds remain, 
 * issued once every 5 seconds during the transfer.
 */

class Main {

	static void calculateSpeed(long bytes, long iteration) {
		long input = 0, totalbytes = 0, rate = 0, tmp, remaining;

		Scanner scnr = new Scanner(System.in);
		System.out.println("Output for data set " + iteration + ", " + bytes
				+ " bytes:");

		while (true) {
			tmp = scnr.nextLong();
			// line number or sec
			input++;
			totalbytes += tmp;

			rate += tmp;

			if (input % 5 == 0) {
				// print rate

				if (rate == 0)
					System.out.println("   Time remaining: stalled");
				else {
					remaining = rate / 5;
					Double d = (5 * (double) (bytes - totalbytes) / rate);
					d = Math.ceil(d);

					remaining = d.longValue();

					System.out.println("   Time remaining: " + remaining
							+ " seconds");
				}

				rate = 0;
			}

			if (totalbytes >= bytes)
				break;
		}

		System.out.println("Total time: " + input + " seconds");
	}

	public static void main(String[] args) throws NumberFormatException,
			IOException {
		Main myWork = new Main();
		myWork.Begin();

	}

	void Begin() throws NumberFormatException, IOException {
		long bytes, iteration = 0;

		Scanner input = new Scanner(System.in);

		while (true) {

			bytes = input.nextLong();

			// read until the 0 bytes sign
			if (bytes == 0) {
				input.close();
				return;
			}

			// how many rates have been computed
			iteration++;

			// write new line after every case
			if (iteration > 1)
				System.out.println();

			calculateSpeed(bytes, iteration);

		}
	}

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 362 - IO tricky samples

Post by brianfry713 » Sat Mar 01, 2014 12:18 am

You're calling new Scanner(System.in) multiple times, try just doing that once.
Check input and AC output for thousands of problems on uDebug!

kukker
New poster
Posts: 5
Joined: Fri Feb 28, 2014 6:39 pm

Re: 362 - IO tricky samples

Post by kukker » Sat Mar 01, 2014 1:18 pm

Thank you brianfry713, it helped! And also I had to write a new line after EVERY case, even after the last one, or I got a wrong answer.

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 362 - IO tricky samples

Post by uDebug » Sat Mar 01, 2014 1:31 pm

kukker wrote:And also I had to write a new line after EVERY case, even after the last one, or I got a wrong answer.
Glad you figured it out.

If only you had read what was written in the post above yours
(2)
Remember to print a newline after each case - including the last one.
P.S: I edited my original post to make this more clear by adding some more emphasis. Hopefully it helps out future problem-solvers.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Post Reply

Return to “Volume 3 (300-399)”