10137 - The Trip

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

Moderator: Board moderators

shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

Where is the problem(10137 - The Trip)

Post by shikhorroy » Sat Sep 14, 2013 6:44 pm

Help me please....I get WA....

Code: Select all

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 1001
#define int64 long long
int main()
{
    int n, cost[MAX];
    while(scanf("%d",&n))
    {
        if(n == 0)  break;
        int val, fVal;
        int64 total = 0, i;
        for(i = 1; i <= n; i++)
        {
            scanf("%d.%d",&val,&fVal);
            cost[i] = (val * 100) + fVal;
            total += cost[i];
        }
        double average = total / (100.0 * n), result = 0.0;
        average = (int64)(average * 100) / 100.0;
        for(i = 1; i <= n; i++)
        {
            float x = (cost[i] / 100.0);
            if(x < average) result += (average - x);
        }
        printf("$%.2lf\n",result);
    }
    return 0;
}

shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

Re: Where is the problem(10137 - The Trip)

Post by shikhorroy » Mon Sep 16, 2013 9:58 am

I got AC..

TheGodHasCome
New poster
Posts: 1
Joined: Thu Nov 07, 2013 9:50 pm

Re: 10137 - The Trip

Post by TheGodHasCome » Thu Nov 07, 2013 9:57 pm

now it's 2013 and still wrong answer makes people mad with this one. So, i did it in pascal, it looks quite straightforward. I tries all tests mentioned within these 8 pages and got right answers. But when i submit it, i get WA. Can anyone suggest what may be wrong? Code:

program trip(input, output);
var a:array[1..1000] of real;
i,j,n:integer;
avg,sum,exc1,exc2,lowAvg,highAvg:real;

begin
repeat
exc1:=0;exc2:=0; avg:=0;sum:=0;
readln(n);
if n=0 then break;
for j:=1 to n do readln(a[j]);

for j:=1 to n do sum:=sum+a[j];
avg:=sum/n;

if avg=trunc(avg) then begin
lowAvg:=avg;
highAvg:=avg;
end;
if avg<>trunc(avg) then begin
avg:=trunc(avg*100)/100;
lowAvg:=avg;
highAvg:=avg+0.01;
end;

for j:=1 to n do if a[j]<lowAvg then exc1:=exc1+(lowAvg-a[j]);
for j:=1 to n do if a[j]>highAvg then exc2:=exc2+(a[j]-highAvg);

if exc1>exc2 then writeln(exc1:8:2)
else writeln(exc2:8:2);

until EOF(input);
end.

any ideas? thank you

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

Re: 10137 - The Trip

Post by brianfry713 » Fri Nov 08, 2013 10:37 pm

brianfry713 wrote:Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!

venkikumar.m
New poster
Posts: 6
Joined: Tue Nov 12, 2013 5:57 pm

Re: 10137 - The Trip

Post by venkikumar.m » Wed Nov 13, 2013 12:54 pm

I tried with all inputs , but it shows WA, I am frustrated with this prob....anybody help me....
here my code....

Code: Select all

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char str[100];
    long long n,len,i;
    long long x,sum,med,s;
    cin>>n;
    while(n)
    {
        sum=0LL;
        long long *a=new long long[n];
        for(i=0LL;i<n;i++)
        {
            cin>>str;
            x=0LL;
            len=strlen(str);
            for(int j=0LL;j<len;j++)
            {
                if(str[j]!='.')
                    x=x*10LL+(str[j]-'0');
            }
            a[i]=x;
            sum=sum+a[i];
        }
//        cout<<sum;
        s=sum*100LL/(n);
        if((s%100LL)>=45LL)
        {
            s=s/100LL;
            s=s+1LL;
        }
        else
        {
            s=s/100LL;
        }
//        cout<<s<<endl;
        med=s;
        sum=0LL;
        s=0LL;
        for(i=0LL;i<n;i++)
        {
            if(a[i]<med)
                sum=sum+(med-a[i]);
            if(a[i]>med)
                s=s+(a[i]-med);
        }
        if(s<sum)
            sum=s;
        cout<<"$"<<sum/100LL<<".";
        if(sum%100LL==0LL)
            cout<<"00\n";
        else if(sum%100LL<=9LL)
            cout<<"0"<<sum%10LL<<endl;
        else
            cout<<sum%100LL<<endl;
        delete a;
        cin>>n;
    }
    return 0;
}


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

Re: 10137 - The Trip

Post by brianfry713 » Thu Nov 14, 2013 12:32 am

Input:

Code: Select all

21 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.03 
0
AC output:

Code: Select all

$0.01
Check input and AC output for thousands of problems on uDebug!

venkikumar.m
New poster
Posts: 6
Joined: Tue Nov 12, 2013 5:57 pm

Re: 10137 - The Trip

Post by venkikumar.m » Thu Nov 14, 2013 6:30 pm

I am getting

Code: Select all

$0.00
as output....
My approach is calculate the average of all and
calculated sum as

Code: Select all

sum+=(a[i]-avg) if a[i]>avg
sum1+=(avg-a[i]) if a[i]<avg
if(sum>sum1)
  sum=sum1;
prints the sum value....
this is my approach ....
is this wrong ..... ?

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

Re: 10137 - The Trip

Post by brianfry713 » Thu Nov 14, 2013 10:56 pm

Yes your code is wrong on the I/O I posted. If the person who spent 0.03 takes 0.01 from one of the people who spent 0.01 then they are all equal within a cent.
Check input and AC output for thousands of problems on uDebug!

venkikumar.m
New poster
Posts: 6
Joined: Tue Nov 12, 2013 5:57 pm

Re: 10137 - The Trip

Post by venkikumar.m » Mon Nov 18, 2013 6:14 am

I changed my code still shows WA....

Code: Select all

#include<iostream>
#include<numeric>
using namespace std;
double calculate(double *amount,int n)
{
    int i;
    double avg,high=0.0,low=0.0;
    for(i=0;i<n;i++)
        avg=amount[i]+avg;
    avg=avg/n;
    for(i=0;i<n;i++)
    {
        if(amount[i]>=avg)
        {
            high+=(double)(long)((amount[i]-avg)*100.0)/100.0;
        }
        else
        {
            low+=(double)(long)((avg-amount[i])*100.0)/100.0;
        }
    }
    if(high>low)
        return (double)high;
    return (double)low;
}
int main()
{
    int n;
    cin>>n;
    while(n)
    {
        double *amount=new double[n];
        double exchange;
        for(int i=0;i<n;i++)
            cin>>amount[i];
        exchange=calculate(amount,n);
        cout.precision(2);
        cout.setf(ios::fixed,ios::floatfield);
        cout<<"$"<<exchange<<endl;
        delete amount;
        cin>>n;
    }
}


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

Re: 10137 - The Trip

Post by brianfry713 » Mon Nov 18, 2013 10:58 pm

brianfry713 wrote:
brianfry713 wrote:Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!

unreleased
New poster
Posts: 16
Joined: Sun Nov 10, 2013 7:41 pm

uva 10137 the trip getting WA

Post by unreleased » Thu Jan 30, 2014 8:22 pm

i cant find error whats wrong in my code pls helpan



#include<stdio.h>
int main()
{
int a, b, c, d, st_num,rest;
double amnt[1010], avrg, ans, total;
while(scanf("%d", &st_num)&&st_num!=0)
{
total=0;
for(a=0; a<st_num; a++)
{
scanf("%lf", &amnt[a]);
total+=amnt[a];
}
avrg=total/st_num;

ans=0;
for(b=0; b<st_num; b++)
{
if(amnt>avrg)
{
rest=(amnt-avrg)*100;
ans+=rest;
}

}
printf("$%.2lf\n", ans/100);
}
return 0;
}

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

Re: uva 10137 the trip getting WA

Post by brianfry713 » Thu Jan 30, 2014 10:17 pm

Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!

unreleased
New poster
Posts: 16
Joined: Sun Nov 10, 2013 7:41 pm

Re: uva 10137 the trip getting WA

Post by unreleased » Mon Feb 03, 2014 1:25 pm

would u please elaborate.........
i didnt get it clearly.......... :p

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

Re: uva 10137 the trip getting WA

Post by brianfry713 » Mon Feb 03, 2014 9:53 pm

Use only integers instead of double.
Check input and AC output for thousands of problems on uDebug!

gg004
New poster
Posts: 4
Joined: Mon Feb 17, 2014 9:25 am

10137 - The Trip, get WA

Post by gg004 » Mon Feb 17, 2014 9:34 am

Here is my code.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void format(char s[]) {
	int i = 0;
	while(isdigit(s[i])) {
		i++;
	}

	if (s[i] == '.') {
		while (s[i]) {
			s[i] = s[i + 1];
			i++;
		}
	}
	else {
		s[i++] = '0';
		s[i++] = '0';
		s[i] = '\0';
	}
}

int main(void) {
	int i, n;
	int sum, min, max, take, give;
	int unit[1000];
	char buff[20];

	while (gets(buff) != NULL) {
		sscanf(buff, "%d", &n);
		if (n) {
			sum = 0;

			for (i = 0; i < n; ++i) {
				gets(buff);
				format(buff);
				unit[i] = atoi(buff);
				sum += unit[i];
			}

			min = sum / n;
			max = (min * n != sum) ? min + 1 : min;

			take = give = 0;
			for (i = 0; i < n; ++i) {
				if (unit[i] < min) {
					give = give + (min - unit[i]);
				}
				else if (unit[i] > max) {
					take = take + (unit[i] - max);
				}
			}

			printf("$%.2f\n", ((give > take) ? give : take) / 100.0f);

		} else {
			break;
		}
	}

	return EXIT_SUCCESS;
}
I passed all the test case that I can find in the board, but still got WA.

Someone help me

Post Reply

Return to “Volume 101 (10100-10199)”