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

SePulTribe
New poster
Posts: 28
Joined: Mon Nov 15, 2004 5:00 pm

Post by SePulTribe »

Actually I find my fault with the programming-challenges judge than with this judge. I had failed attempts with LC-Display(110104/706) and WERTYU(110301/10082) in which both got P.E. and W.A. respectively in programming-challenges but were instantly acceted by this judge. What's your code for The Trip like?

Mohammad Mahmudur Rahman
Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Post by Mohammad Mahmudur Rahman »

SePulTribe wrote:Actually I find my fault with the programming-challenges judge than with this judge. I had failed attempts with LC-Display(110104/706) and WERTYU(110301/10082) in which both got P.E. and W.A. respectively in programming-challenges but were instantly acceted by this judge.
Yes, I have experienced similar situation, too. I had P.E. on LC-Display in this judge. But after getting P.E. on Programming-challenges, I changed the code & got AC in UVa, but Programming-challenges judge is still unmoved with its P.E. I have certain troubles with a few other problems, too. But the problem The Trip was surprisingly accepted in PC with a code which even I don't think to be my most correct version for this problem. :o

Here is my code - [cpp]

/*@begin_of_source_code*/
/* The Trip */

/* AC in PC but WA in UV*/

#include<stdio.h>

#define size 1003

int main(void)
{
long n,i;
double avg,sum,sumup,sumdown,out,data[size];

while(scanf("%ld",&n)==1)
{
if(!n)
break;
sum = 0.0;
for(i=0;i<n;i++)
{
scanf("%lf",&data);
sum += data;
}

avg = sum/n;
avg = (long)(avg * 100 + 0.5);
avg /= 100;

sumup = sumdown = 0.0;

for(i=0;i<n;i++)
{
if(data < avg)
sumdown += (avg - data);
else
sumup += (data - avg);

}

if(sumup<sumdown)
out = sumup;
else
out = sumdown;
printf("$%.2lf\n",out);
}//end case while

return 0;
}

/*@end_of_source_code*/
[/cpp]
You should never take more than you give in the circle of life.

SePulTribe
New poster
Posts: 28
Joined: Mon Nov 15, 2004 5:00 pm

Post by SePulTribe »

Hmm thanks! Your code is actually exactly the same as mine except that you added 0.5 to ur avg. Now it has been accepted by the PC judge. Hmm... how to get it accepted on UVA.

SePulTribe
New poster
Posts: 28
Joined: Mon Nov 15, 2004 5:00 pm

Post by SePulTribe »

Hey thanks!!! I read your post and made changes accordingly and now my prog works! I love you!! :D

SePulTribe
New poster
Posts: 28
Joined: Mon Nov 15, 2004 5:00 pm

Post by SePulTribe »

Mohammad Mahmudur Rahman wrote:I don't really know what's the problem with this problem. I have long been in troubles with this one & tried in various ways all of which resulted in WA. But just now I've submitted the same problem in the programming-Challenge judge where it recieved AC. Can someone tell me what's wrong with this problem in UVa judge? :o
After reading the post by waseem50 I finally got it accepted by UVA! Try it:
[c]#include <math.h>
#include <stdio.h>

long double round(long double x, int precision)
{
return floor(x*pow(10.0,(long double)precision)+0.5)/pow(10.0,(long double)precision);
}

int main(void)
{
int no, ii;
long double total, above, below, right, student[1000];

while (1)
{
scanf("%d\n", &no);
if (!no)
break;
for (ii = 0, total = 0; ii < no; ii++)
{
scanf("%Lf\n", &student[ii]);
total += student[ii];
}
right = round((total / no), 2);
above = below = 0;
for (ii = 0; ii < no; ii++)
{
if (student[ii] < right)
below += right - student[ii];
if (student[ii] > right)
above += student[ii] - right;
}

if (above == 0)
above = below;

if (below == 0)
below = above;

printf("$%.2Lf\n", round(((below < above) ? below : above), 2));
}
return 0;
}
[/c]

Mohammad Mahmudur Rahman
Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Post by Mohammad Mahmudur Rahman »

Thanks ^ ((1+2+3+...... inf)!) 2 u. :D . The conversion from long 2 long long & double 2 long double works. It was such a tedious problem & annoyed me so much ! :evil: Thanks again.
You should never take more than you give in the circle of life.

Mohammad Mahmudur Rahman
Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Post by Mohammad Mahmudur Rahman »

Finally it works. Special thanks 2 waeem50 & also 2 SePulTribe.
You should never take more than you give in the circle of life.

SePulTribe
New poster
Posts: 28
Joined: Mon Nov 15, 2004 5:00 pm

Post by SePulTribe »

Haha yeah no prob. I know that feeling of frustration! =)

zhenh
New poster
Posts: 10
Joined: Mon Oct 18, 2004 3:52 pm

I found something ...

Post by zhenh »

I once take the inputs in this way:
scanf("%d",&n); /* n */
scanf("%Lf",&x); /* money */ ....
==> WA

now:
scanf("%d\n",&n);
scanf("%Lf\n",&x); .....
==> Accepted...

Everything else are exactly the same, only the CR characters in the format strings were added.

Does anybody have any ideas about why?

Grazyna
New poster
Posts: 16
Joined: Wed Apr 20, 2005 2:44 pm
Location: Thessaloniki,Greece

10137

Post by Grazyna »

I read all the posts about the problem. I considered the extra pennies , extreme cases etc. I am affraid that I miss something essential about c++ programming and portability. Thanks in advance for any help.

Code: Select all

/* @JUDGE_ID: xx 10137 C++  "The Trip" */

#include <iostream>

#include <iomanip>

using namespace std ;

int main(){
	short sub ;
	long people=1 , coun , residue , average , nmore , nless ;
	long money[1001] , sum , more , less ;
	long double hold , result ;
	while (people!=0){
		sum = 0 ;
		cin >> people ;
		if (people==0) break ;
		for(coun=1 ; coun<=people ; ++coun){
			cin >> hold ;
			money[coun] = (int)(hold*100) ;
			sum += money[coun] ;}
		residue =sum%people ;
		average = (int)((sum-residue)/people) ;
		if (residue==0)  sub = 0 ;
		else sub = 1 ;
		more = 0 ;
		nmore = 0 ;
		less = 0 ;
		nless = 0 ;
		for(coun=1 ; coun<=people ; ++coun)
			if (money[coun]>average){
				more += money[coun] - average - sub ;
				++nmore ;}
			else {
				less += average - money[coun] ;
				++nless ;}
		cout.setf(ios::fixed) ;
		cout.precision(2) ;
		if (residue > nmore) 
			result = static_cast<double>(more)/100 ;
		else 
			result = static_cast<double>(less)/100 ;
		cout << '$' << result << endl ;
		}
return 0;}

ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy »

long double is not necessary, I use double and get AC.
But waseem50's test IO data has really helped me. Thank you anyway.
I stay home. Don't call me out.

The^Guard
New poster
Posts: 6
Joined: Wed Nov 23, 2005 11:16 pm

10137: The Trip

Post by The^Guard »

Hey... I guess I'm having problem passing one of the test cases of this problem... Please give me a hint as to what I'm missing in this task. My solution is fairly simple: I'm finding the arithmetic mean, and then the differences...

Here is my code:

VAR N: integer;
A: ARRAY[1..1000] of double;
Answer: double;

procedure ReadInfo;
VAR I: integer;
begin
FOR I:=1 TO N DO ReadLn(A);
end;


procedure WriteOutput;
begin
WriteLn('$', Answer:4:2);
end;


procedure Solve;
VAR I: integer;
AMean: double;
A1, A2: double;

begin
AMean:=0; A1:=0; A2:=0;

FOR I:=1 TO N DO
AMean:=AMean+A;

AMean:=Round((AMean / N)*100)/100;

FOR I:=1 TO N DO
IF A > AMean
then A1:=A1 + A-AMean
else A2:=A2 + Amean-A;

IF A1<A2 then
Answer:=A1 else Answer:=A2;
end;


begin
ReadLn(N);

WHILE (N<>0) DO
begin
ReadInfo;
Solve;
WriteOutput;

ReadLn(N);
end;
end.

worldguy
New poster
Posts: 4
Joined: Sat May 07, 2005 2:31 pm
Location: China

Post by worldguy »

Thanks. I just made a mistake with the calculation of average. With your help, I got AC.
Mohammad Mahmudur Rahman wrote:
SePulTribe wrote:Actually I find my fault with the programming-challenges judge than with this judge. I had failed attempts with LC-Display(110104/706) and WERTYU(110301/10082) in which both got P.E. and W.A. respectively in programming-challenges but were instantly acceted by this judge.
Yes, I have experienced similar situation, too. I had P.E. on LC-Display in this judge. But after getting P.E. on Programming-challenges, I changed the code & got AC in UVa, but Programming-challenges judge is still unmoved with its P.E. I have certain troubles with a few other problems, too. But the problem The Trip was surprisingly accepted in PC with a code which even I don't think to be my most correct version for this problem. :o

Here is my code - [cpp]

/*@begin_of_source_code*/
/* The Trip */

/* AC in PC but WA in UV*/

#include<stdio.h>

#define size 1003

int main(void)
{
long n,i;
double avg,sum,sumup,sumdown,out,data[size];

while(scanf("%ld",&n)==1)
{
if(!n)
break;
sum = 0.0;
for(i=0;i<n;i++)
{
scanf("%lf",&data);
sum += data;
}

avg = sum/n;
avg = (long)(avg * 100 + 0.5);
avg /= 100;

sumup = sumdown = 0.0;

for(i=0;i<n;i++)
{
if(data < avg)
sumdown += (avg - data);
else
sumup += (data - avg);

}

if(sumup<sumdown)
out = sumup;
else
out = sumdown;
printf("$%.2lf\n",out);
}//end case while

return 0;
}

/*@end_of_source_code*/
[/cpp]

rconnell
New poster
Posts: 1
Joined: Sun Jan 15, 2006 6:22 pm

10137 Assistance

Post by rconnell »

Can anyone please provide some assistance as to why I am getting WA on this problem? Thank you.

Code: Select all

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stdlib.h>
#include <math.h>

using namespace std;

vector<int> students;

int main(int argc, char *argv[])
{
  int n,i,pool = 0;
  while (cin >> n)
  {
    if (n == 0)
    {
      break;
    }
    int tot = 0;
    double val;
    for (i = 0; i < n; i++)
    {
      cin >> val;
      val *= 100;
      students.push_back((int)val);
      tot += (int)val;
    }
    double db = (double)(tot)/(double)n;
    int avg = (tot/n);
    int cl = (int)ceil(db);
    int rem = tot % n;

    for (int i = 1; i <= rem; i++)
    {
      vector<int>::iterator maxiter = max_element(students.begin(), students.end());
      pool += abs(*maxiter - cl);
      students.erase(maxiter);
    }

    for (vector<int>::iterator iter = students.begin(); iter != students.end(); iter++)
    {
      pool += abs(*iter - avg);
    }

    pool /= 2;

    cout << "$" << setiosflags(ios::fixed) << setprecision(2)<< (pool/100.00) << endl;
    pool = 0;
    students.clear();
  }
}

fayyazkl
New poster
Posts: 11
Joined: Thu Mar 16, 2006 8:02 am
Location: Lahore, Pakistan

The Trip 10137

Post by fayyazkl »

You need to find the MINUMUM amount required to be exchanged. For that matter you have to find a way to test ONLY UPTO TWO DECIMAL PLACES as to what is the corrent amount. Secondly your A. Mean should also be rounded upto two decimal places.

Example : in the second test case given in the problem, one person has spent 3.01, so all he needs to add to his spent amount is 5.99, not 6. That makes the net amount 6+5.99 - 11.99.

Hope you got the idea.
There are 10 types of people. Those who can read binary and those who cant ;)
http://acm.uva.es/problemset/usersjudge.php?user=37504

Post Reply

Return to “Volume 101 (10100-10199)”