## 10137 - The Trip

Moderator: Board moderators

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

### 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.

powerboy
New poster
Posts: 8
Joined: Mon May 12, 2003 6:13 am
Contact:

### 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 = (int) (temp * 100);
sum += array;
}

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

int mySum = 0;

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

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

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

carneiro
Learning poster
Posts: 54
Joined: Sun May 18, 2003 1:19 am
Location: Rio de Janeiro, Brazil
Contact:
you are forgetting the cases in which there are more costs above the average than below (or vice-versa)
[]s
Mauricio Oliveira Carneiro

powerboy
New poster
Posts: 8
Joined: Mon May 12, 2003 6:13 am
Contact:
Hi, I did take the case into consideration.
Can you give a testing case?

carneiro
Learning poster
Posts: 54
Joined: Sun May 18, 2003 1:19 am
Location: Rio de Janeiro, Brazil
Contact:
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

powerboy
New poster
Posts: 8
Joined: Mon May 12, 2003 6:13 am
Contact:
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.

ggll
New poster
Posts: 13
Joined: Tue Jun 10, 2003 5:15 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.

waseem50
New poster
Posts: 2
Joined: Wed Jul 23, 2003 12:28 pm
Contact:

### 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;
cin.ignore();
}
for(i=0;i<number;i++)
sum+=(arr);
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>0) result1+=average-arr;
else result2+=arr-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
Contact:

### 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]

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

### 10137 Why WA ?

[cpp]
#include <iostream.h>

int main()
{
int a = 1,i;
long double amount;
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 = temp;
}

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

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

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

gvcormac
Problemsetter & Reviewer
Posts: 194
Joined: Fri Mar 15, 2002 2:00 am
Contact:
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

Eva
New poster
Posts: 14
Joined: Thu Apr 10, 2003 11:31 am
Location: Bombay
Contact:

### 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 = {0};
double sum = 0,ans = 0;

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

double avg = sum/double(n);

for(int i=0;i<n;++i)
if(trip > avg)
ans += trip-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
Contact:
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!

saintp
New poster
Posts: 2
Joined: Tue Oct 07, 2003 3:19 am
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;
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?