## 10137 - The Trip

Moderator: Board moderators

### Got it here, too

Yo, ggll, good sample data. I posted on other threads, including one of my onw, about my frustrations with this problem, but after using that sample data I was able to change one line and life was good again.

Thanks.
xidao
New poster

Posts: 3
Joined: Mon Jun 09, 2003 1:59 am

### 10137 help plz

I just don't see what is wrong with my program.
I converted everything into cents.
I then found the average of these costs in cents rounding down.
I then found the remaining cents which need to be distributed using
remainder = sum mod # of trips.
I counted the # of costs larger than the average, denote by count.
I then added all the difference between the average and the costs
smaller than the average.
This total of difference + remainder - count should equal
the total exchanged amount.

what is wrong with my program????
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
int n;
int *array;
int sum;

while (cin >> n)
{
if (n == 0)
return 0;

sum = 0;

array = new int[n];

int i;

double temp;
for (i = 0; i < n; i++)
{
cin >> temp;
array[i] = (int) (temp * 100);
sum += array[i];
}

int myAverage = sum / n;
int remainder = sum % n;

int mySum = 0;

int count = 0;
for (i = 0; i < n; i++)
{
if (myAverage > array[i])
{
mySum += myAverage - array[i];
}
else if (array[i] < myAverage)
{
count++;
}
}

if (remainder != 0)
mySum += remainder - count;

double tempSum = mySum/(100.0);
printf("\$%.2f\n", tempSum);
delete array;
}
}
powerboy
New poster

Posts: 8
Joined: Mon May 12, 2003 6:13 am

you are forgetting the cases in which there are more costs above the average than below (or vice-versa)
[]s
Mauricio Oliveira Carneiro
carneiro
Learning poster

Posts: 54
Joined: Sun May 18, 2003 1:19 am
Location: Rio de Janeiro, Brazil

Hi, I did take the case into consideration.
Can you give a testing case?
powerboy
New poster

Posts: 8
Joined: Mon May 12, 2003 6:13 am

maybe this one :

3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
5
5000.00
11.11
11.11
11.11
11.11
3
0.01
0.03
0.03
4
25.00
25.00
25.00
28.00
3
10.01
15.25
18.96
4
25.03
25.00
25.00
25.00
0

Output for this should be:

\$10.00
\$11.99
\$3991.11
\$0.01
\$2.25
\$4.73
\$0.02
[]s
Mauricio Oliveira Carneiro
carneiro
Learning poster

Posts: 54
Joined: Sun May 18, 2003 1:19 am
Location: Rio de Janeiro, Brazil

Hey, Mauricio
I did the test cases you provided. Thank you.
The answers are the same as the ones you provided.
So i still don't see anything wrong with it.
powerboy
New poster

Posts: 8
Joined: Mon May 12, 2003 6:13 am

The variable count will always be 0, can you see why?

Most likely there is also a double precision issue. Try adding an EPS to the int cast.

Looks like these two bugs together get you passed the test cases above.
ggll
New poster

Posts: 13
Joined: Tue Jun 10, 2003 5:15 am

### 10137 HELPPPPPPPPPPPPP!!!!

hi guys ive beeen struggining with the trip, but am getting a wa
my approach is simple, i find the average ,round it mathematically ie <0.5 =0, >0.5 =1 , then i find the sum of extra pennies, and the sum of the penny deficit ie sum of pennies bigger and smaller than average, then i output the smaller of the 2, but still am getting a WA.

this is my code with some input and output!!!

input
3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
5
5000.00
11.11
11.11
11.11
11.11
3
0.01
0.03
0.03
4
25.00
25.00
25.00
28.00
3
10.01
15.25
18.96
4
25.03
25.00
25.00
25.00
0

output
\$10.00
\$11.99
\$3991.11
\$0.01
\$2.25
\$4.73
\$0.02
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
using namespace std;

double round(double x, int precision);

int main()
{
#ifndef ONLINE_JUDGE
ifstream cin("input.txt",ios::in);
#endif

int number,i;
cout.setf(ios::showpoint|ios::fixed);

for(cin>>number,cin.ignore();number!=0;cin>>number,cin.ignore())
{
float sum=0;

float *arr=new float[number];
for(i=0;i<number;i++)
{
cin>>arr[i];
cin.ignore();
}
for(i=0;i<number;i++)
sum+=(arr[i]);
float average=round((sum/number),2);

float result1=0;
float result2=0;
for(i=0;i<number;i++)/*finds the smallest from the average*/
if(average-arr[i]>0) result1+=average-arr[i];
else result2+=arr[i]-average;
if (result1==0)
result1=result2;
if (result2==0)
result2=result1;
float result=(result1<result2)?result1:result2;

cout<<setprecision(2)<<"\$"<<result<<endl;
}

return 0;
}

double round(double x, int precision)
{

return floor(x*pow(10.0,(double)precision)+0.5)/pow(10.0,(double)precision);
}
waseem50
New poster

Posts: 2
Joined: Wed Jul 23, 2003 12:28 pm

### Solved 10137

Hey everybody i read in one of the posts to uses long long, so i replaced all my floats and doubles with long double, and i got AC immeduitely without anu other modification to my code [/cpp]
waseem50
New poster

Posts: 2
Joined: Wed Jul 23, 2003 12:28 pm

### 10137 Why WA ?

[cpp]
#include <iostream.h>

int main()
{
int a = 1,i;
long double amount[1000];
long double temp,total,rata,hasil;

while (a != 0)
{
cin >> a;
total = hasil = 0;
if (a != 0)
{
for (i=0;i<a;i++) {
cin >> temp;
amount[i] = temp;
}

for (i=0;i<a;i++)
total += amount[i];
rata = total/a;
rata = rata*100;
rata = (int)rata;
rata = rata/100;

for (i=0;i<a;i++){
if (amount[i] < rata) hasil = hasil + (rata-amount[i]);
}
cout << "\$" << hasil << endl;
}
}
return 0;
}[/cpp]

Why Wrong answer ? Is there test case that make my source code not valid ?[/c]
zilnus
New poster

Posts: 9
Joined: Sat Mar 08, 2003 11:59 am

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

\$0

Which is the wrong amount, and is missing the .00
gvcormac
Problemsetter & Reviewer

Posts: 194
Joined: Fri Mar 15, 2002 2:00 am

### 10137 - The Trip

I don't know! This is not giving me the sample 2nd output!
But why?

This should be the correct answer! [cpp]#include<iostream.h>
#include<iomanip.h>

int main()
{
int n = 0;

while(cin >> n)
{
if(n == 0)
return 0;

double trip[1000] = {0};
double sum = 0,ans = 0;

for(int i=0;i<n;++i)
{
cin >> trip[i];
sum += trip[i];
}

double avg = sum/double(n);

for(int i=0;i<n;++i)
if(trip[i] > avg)
ans += trip[i]-avg;

cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(2);

cout << '\$' << ans << endl;
}

return 0;
}[/cpp]
My own quote:

We are here as Adam and Eve were here!
Eva
New poster

Posts: 14
Joined: Thu Apr 10, 2003 11:31 am
Location: Bombay

oh! It gives 12.00 but the sample output says 11.99 why
My own quote:

We are here as Adam and Eve were here!
Eva
New poster

Posts: 14
Joined: Thu Apr 10, 2003 11:31 am
Location: Bombay

The problem lies in the fact that you can be one penny off. So where you do

Code: Select all
`ans += trip[i] - avg;`

You're only measuring the distance from those people who spent more than the average. In fact, those people who spent *less* might be closer, and in this case, they are. You need to compute both the total spent over the average and the total spent under, compare the two, and return the lesser of the two.
saintp
New poster

Posts: 2
Joined: Tue Oct 07, 2003 3:19 am

Incidentally, I'm having problems with mine too, of the more ineffable variety. It passes all the tests I throw at it, but I still get WA. Any help would be much appreciated:

Code: Select all
`/*  @JUDGE_ID:  36781AT 10137 C++ */#include <iostream>using namespace std;int min(int num1, int num2) {  if (num1 < num2) {    return num1;  } else {    return num2;  }}int max(int num1, int num2) {  if (num1 > num2) {    return num1;  } else {    return num2;  }}int calcBest(int cents[], int avg, int numPpl) {  int top = 0, bottom = 0, middle = 0;  for (int i = 0; i < numPpl; i++) {    if (cents[i] > avg) {      top += cents[i] - avg;    } else if (cents[i] < avg) {      bottom += avg - cents[i];    }  }  if (top == 0) {    return calcBest(cents, --avg, numPpl);  } else if (bottom == 0) {    return calcBest(cents, ++avg, numPpl);  }  return min(top, bottom);}int main() {  int cents[1000];  int numPpl, avg, best, top, bottom;  double temp;  while ((cin >> numPpl) && (numPpl != 0)) {    top = avg = 0;    bottom = 10000000;    for (int i = 0; i < numPpl; i++) {      cin >> temp;      avg += (int) (temp * 10000 + 1e-9) / numPpl;      cents[i] = (int) (temp * 100 + 1e-9);      top = max(top, cents[i]);      bottom = min(bottom, cents[i]);    }    if (abs(top - bottom) <= 1) {      best = 0;    } else {      avg = (avg + 50) / 100;      best = calcBest(cents, avg, numPpl);    }    cout << "\$" << (best / 100) << "." << (best % 100) / 10 << (best % 10) << endl;  }  return 0;}`

Here are my tests:

3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
3
6.17
5.00
4.03
12
123.12
6.13
9.44
89.08
278.78
223.78
78.45
912.89
554.76
547.57
1781.89
907.07
2
4.99
15.00
1
10.00
15
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
5
5000.00
11.11
11.11
11.11
11.11
15
0.01
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
4
25.00
25.00
25.00
28.00
3
10.01
15.25
18.96
4
25.03
25.00
25.00
25.00
0

I expect that to return:

\$10.00
\$11.99
\$1.10
\$2407.09
\$5.00
\$0.00
\$0.01
\$3991.11
\$0.01
\$2.25
\$4.73
\$0.02

And it does. Any help?
saintp
New poster

Posts: 2
Joined: Tue Oct 07, 2003 3:19 am

PreviousNext