CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(easy) » The Way to a Friends House Is Never Too Long

The Way to a Friends House Is Never Too Long

Problem code: POINTS

  • Submit
  • All Submissions

All submissions for this problem are available.

You are given a set of points in the 2D plane. You start at the point with the least X and greatest Y value, and end at the point with the greatest X and least Y value. The rule for movement is that you can not move to a point with a lesser X value as compared to the X value of the point you are on. Also for points having the same X value, you need to visit the point with the greatest Y value before visiting the next point with the same X value. So, if there are 2 points: (0,4 and 4,0) we would start with (0,4) - i.e. least X takes precedence over greatest Y. You need to visit every point in the plane.

Input

You will be given an integer t(1<=t<=20) representing the number of test cases. A new line follows; after which the t test cases are given. Each test case starts with a blank line followed by an integer n(2<=n<=100000), which represents the number of points to follow. This is followed by a new line. Then follow the n points, each being a pair of integers separated by a single space; followed by a new line. The X and Y coordinates of each point will be between 0 and 10000 both inclusive.

Output

For each test case, print the total distance traveled by you from start to finish; keeping in mind the rules mentioned above, correct to 2 decimal places. The result for each test case must be on a new line.

Example

Input:
3

2
0 0
0 1

3
0 0
1 1
2 2

4
0 0
1 10
1 5
2 2

Output:
1.00
2.83
18.21

For the third test case above, the following is the path you must take:
0,0 -> 1,10  
1,10 -> 1,5
1,5 -> 2,2
= 18.21

Date: 2008-12-01
Time limit: 6s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA JAR C# C#2 ASM D FORT ADA BASH PERL PYTH RUBY PHP LISP sbcl LISP clisp HASK CAML


  • Submit

Comments

  • Login or Register to post a comment.

rooparam @ 17 Jun 2009 05:18 AM

java.util.comparator interface helps here lot ... i don't have to implement any sorting algorithm

jdmetz @ 21 Jun 2009 01:21 AM

sort() works just fine in C

jdmetz @ 21 Jun 2009 01:23 AM

C (cpp) that is - why did my double ' ' get stripped?

universe @ 23 Jun 2009 10:50 PM

The debugger is stating the answer comes out to be correct.
But here on submitting it says wrong answer. Unable to understand what is wrong.

universe @ 23 Jun 2009 11:38 PM

In order to optimize i tried using float, but the thing works with double.

i0exception @ 24 Jun 2009 05:03 PM

@pratik vakil float has lesser precision than double.

james_vinett @ 18 Jul 2009 04:01 AM

there is a number format exception that is thrown on one of the 'test case' integers. i had to wrap the parsing of the 'test case' individually to get a successful run. i guess i should be wrapping them all in try-catch clauses individually anyway. i've gotten lazy...still, it is strange that the test data has an error in it. especially since the format instructions were very specific.

prm2612 @ 30 Jul 2009 12:08 PM

stuck!
unable to generate a test case for which my submission will fail :(
admin or moderator.... plz provide some hint.

admin 2 @ 30 Jul 2009 06:36 PM

Try some more :)

prm2612 @ 31 Jul 2009 12:42 PM

suppose the total distance is 1.256
What should be the output? 1.25 or 1.26?

triplem @ 31 Jul 2009 12:49 PM

1.26.

prm2612 @ 31 Jul 2009 01:08 PM

sorry for the last post.... it should be 1.26 ....evident frm the second sample test case..... i m giving up.... i have tried for all the test cases i can think of.... and none of them fail :(

amrutha @ 13 Aug 2009 11:58 PM

how can I round upto 2 decimal points?

admin 2 @ 14 Aug 2009 12:21 AM

double t = 45.493847;
int x = t*100;
t = x / 100.0;
printf("%.2lf", t);

Something like this?

this is my

akhilesh890 @ 4 Sep 2009 03:25 PM

this is my code :

#include<iostream>
#include<math.h>

using namespace std;
int main()
{
char c;int n,i,*x,*y,j,t,k,z;
float s;
    cin >> t;
for ( z = 0 ; z < t ; z++ )
{    
    i = 0;k=0;
    cin >> n;
    x = new int[n+1];
    y = new int[n+1];
    
    
    for ( i = 0 ; i < n ; i++ )
        cin >> x[i] >> y[i] ;
    
    s=0;    
    for( j = 0 ; j < n - 1 ; j++ )
    
        s = s + sqrt ( pow((y[j+1]-y[j]),2) + pow((x[j+1]-x[j]),2) );            
        
    
    printf("%.2fn", s);
    delete []x;
    delete []y;
}
}          

 

 

 

 

 

can anyone tell the error?its showing wrong answer. 

You don't seem to be obeying

triplem @ 4 Sep 2009 03:58 PM

You don't seem to be obeying any of the rules mentioned in the problem. Try reading it again.

Hi Admin   I've completed and

durga_rise @ 6 Sep 2009 04:30 AM

Hi Admin

 

I've completed and successfully run the code for this problem and it works fine for many test cases with me..It runs fine and exactly as per the problem constraints on my laptop.

 

Java version on my laptop:

___________________________

java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)

 

I'm using Ubuntu:9.04

 

Could you pls tell me if I can personally send you my src file, So that you can compile and run it, & let me know if I'm making some critical mistake as per Directi program evaluation norms..

 

:)

u know how it makes feel, when it's all correct and still it appears not.

Well, if you are getting a

admin @ 7 Sep 2009 02:16 PM

Well, if you are getting a wrong answer, then your code is not correct and is producing the wrong result for some of the test cases. :)

Dear Admin, I am getting

anupam @ 14 Sep 2009 11:04 AM

Dear Admin,

I am getting TLE everytime. My answers seems to be correct. I have tried using merge sort then again insertion sort but all times I am getting TLE? Can u provide any hint what sorting algo to apply??

it keeps saying run time

harshithsirigeri @ 18 Sep 2009 10:10 AM

it keeps saying run time error i think its the array size of 10000 which i am using giveing the problem, admin can you tell if i am coreect.

@anupam i would say try using

harshithsirigeri @ 18 Sep 2009 10:39 AM
@anupam i would say try using hashtable, but even i havent succeded at solving the problem as it says wrong answer. @admin how do i take input for a point 0 0, do i take it as cin<

You are trying to create a

admin @ 18 Sep 2009 02:23 PM
You are trying to create a 10000X10000 array on the stack. That will give a segmentation fault. Move all your array declarations out of the main function and try.

What will be the answer for a

ravi_m @ 19 Sep 2009 02:01 PM
What will be the answer for a test case 4 0 0 0 2 0 4 0 8 will it be 0,8 -> 0,4 -> 0,8 -> 0,2 -> 0,8 -> 0,0 distance = 28.00 or 0,8 -> 0,4 -> 0,2 -> 0,0 distance = 8.00

I mean (0,0), (0,2), (0,4),

ravi_m @ 20 Sep 2009 12:40 AM
I mean (0,0), (0,2), (0,4), (0,8)

@Ravi Looks like you solved

fortress @ 26 Sep 2009 11:49 PM

@Ravi

Looks like you solved it... mind answering your own question for us?  I had the same question; I think the problem needs to be worded more clearly.  When read literally, I think it would produce 28 for your problem, but I don't understand the motivation for designing the problem that way unless it is solely to try and confuse people about the rules.

@fortress you are right, the

ravi_m @ 27 Sep 2009 02:24 AM
@fortress you are right, the problem description is little bit confusing, but the purpose behind every problem on codechef is that we all try and improve our coding and logical skills and that is why the chef never gives us the actual test cases.

@fortress 8.00 is the correct

ravi_m @ 27 Sep 2009 02:44 AM
@fortress 8.00 is the correct distance ;)

if input is

shaileshbhat @ 2 Oct 2009 03:54 PM

if input is (1,10),(2,19),(1,6),(2,20),(1,8),(1,18). wat ll be visiting order...? is this correct (1,10),(1,8),(1,6),(2,20),(2,19),(1,18)..?

You have to start with

bezgin @ 24 Oct 2009 08:04 AM

You have to start with (1,18). Read the problem statement carefully.

can I declare a 2-D array

bezgin @ 24 Oct 2009 10:10 PM

can I declare a 2-D array "int xy[RANGE][RANGE]" for each test case?

 

I generate input and seems to work fine on my laptop but chef gives runtime error.

No, 10000*10000 ints is about

triplem @ 25 Oct 2009 02:34 AM

No, 10000*10000 ints is about 380mb which is far too much memory.

<!-- @page { margin:

bermanrl @ 10 Nov 2009 03:18 AM

<!-- @page { margin: 0.79in } P { margin-bottom: 0.08in } -->

Hi,

I have defined a number of reasonably small test cases to run and all succeeded. I then set up a test case generator to comply with codechef specifications. The number of tests are user defined at the command line but all other parameters are generated by random number selection conforming with the directions set forth. The output from this program becomes the input for the POINTS program. Randomly selecting x number of tests within the test file and solving those by hand, I could insure the answers are correct. They were. I am still, after 5 runs getting a runtime error of NZEC. Since I am using Python the types of errors to produce this result are finite. I think they possibly reflect an error on one of the test cases.

I would like to know if some error was built into the test cases. If this is correct, I think you have violated your own standards. What I have always liked best about CodeChef is the directions for each problem are exceedingly well defined. We know the inputs and we know exactly what criteria govern their creation.  Therefore, if an error has been deliberately introduced this is a violation of your standards and should certainly not be used under the assumption that a 'good programmer' knows to code for invalid data. That is true but in a controlled environment such as codechef or SPOJ this is an invalidated exercise and either the test data should be recreated and firmly tested or the problem should be dropped.

Again, if I am in error, I apologize for my rant and would like to submit to you both my solution and my test deck because suddenly this has become a most unusual and perplexing situation.



Thank you,



Robert



The problem has been solved

triplem @ 10 Nov 2009 07:27 AM

The problem has been solved by 133 people, and there is nothing wrong with the input. If you are getting a runtime error then something is wrong with your solution, rather than codechef 'violating their standards'.

I'm storing the sum in a long

hiptobecubic @ 15 Nov 2009 08:07 AM

I'm storing the sum in a long double, so I'm loathe to think that I'm losing precision there. Still, I'm coming up with WA. What a puzzle.

I've solved the problem, but

redvirus @ 17 Nov 2009 07:59 PM

I've solved the problem, but it is giving me wrong answer. It might be some case that i'm omitting some of the test cases. Can anybody suggest me any test case. Some important one which can exhaustively be applicable for the whole problem.

 

Thanks in advance...!!!

Here's a testcase generator

jcomeau_ictx @ 29 Nov 2009 08:02 AM

Here's a testcase generator that might help. It helped me find a bug in my program. Good luck!

#!/usr/bin/python
import sys, random
testcases, points = 20, 100000
print testcases
for i in range(testcases):
print ''
print points
for point in xrange(points):
print random.randint(0, 10000), random.randint(0, 10000)

oops, sorry, bad formatting,

jcomeau_ictx @ 29 Nov 2009 08:04 AM

oops, sorry, bad formatting, here goes again:

#!/usr/bin/python
import sys, random
testcases, points = 20, 100000
print testcases
for i in range(testcases):
print ''
print points
for point in xrange(points):
print random.randint(0, 10000), random.randint(0, 10000)


For those who are having

Allar @ 13 Dec 2009 10:53 AM

For those who are having problems and are getting Wrong Answer, are you compiling in Windows? I was, and I got 3 Wrong Answers on this even though all my program seemed to be correct. Compile on Linux (I use Ubuntu) and it might show you whats wrong with your program, at least it did for me.

I am getting Runtime Error

prm2612 @ 27 Dec 2009 09:55 AM

I am getting Runtime Error while submitting my Java code. The error code as sent in the email to me is NZEC. From http://www.codechef.com/wiki/status-codes#Q._Why_do_I_get_an_error_calle... link, I came to know that it can be because of some unhandled exception thrown by a Java program.

I have tried testing my program with boundary conditions on my own. I am unable to somehow crash my program. Can I get the exception class??? or any kind of hint???

ok i got the issue.... just a

prm2612 @ 27 Dec 2009 10:17 AM

ok i got the issue.... just a case to take into consideration.... there can be same set of points more than once in the input :)

I'm using the Comparable

sau @ 27 Dec 2009 11:47 AM

I'm using the Comparable interface in java to sort my points. However, my result turns out to be Time limit exceeded...I was wondering if anyone had successfully used this approach or whether I should try another more efficient sorting algorithm, because I see no other way of optimizing my program.

Scanners are very very very

triplem @ 27 Dec 2009 01:29 PM

Scanners are very very very slow. Never use them.

Hi everybody, In my C

viggy_prabhu @ 14 Jan 2010 04:04 PM

Hi everybody,

In my C program, I am using math.h for sqrt. So when i am compiling it in my system, I need to give use the following command , "gcc -lm test.c" , only then the program runs. However when I am submitting here, I am getting "wrong answer". Is it because I am using math.h, that I am getting the "wrong answer". If so, what do yo suggest me to use for sqrt function?

Regards,

Vignesh

No, your code is not giving a

triplem @ 14 Jan 2010 04:14 PM

No, your code is not giving a compiler error. Wrong answer means you are printing out the wrong thing, which means there is a test case that you are giving the wrong answer for. Not that the sqrt function isn't working.

i don't understand the

Dalchand @ 16 Jan 2010 01:38 PM

i don't understand the problem for last case why can't i go directly to 2,2 from 0,0 ... the problem statement says only u can't go to a point having less value of X ... so i guess i can go anywhere except that???

oops didnt read last line :)

Dalchand @ 16 Jan 2010 01:41 PM

oops didnt read last line :)

missed return statement in

cinash @ 24 Mar 2010 04:02 PM

missed return statement in main...!!! and wasted time..

where do i get more

kalos @ 24 Mar 2010 04:18 PM

where do i get more information on this

i checked with the above test

gunjanbansal @ 13 May 2010 03:40 PM

i checked with the above test cases , program is running fine and also tested it on sample test cases genterated by the python script given above (for sorting). still i am getting wrong answer :( , any hints ?? or some corner cases that i may be missing

You may have another bug, but

triplem @ 13 May 2010 03:46 PM

You may have another bug, but floats aren't very precise; they are only accurate to about 7dp. Adding 100000 of them will leave your answer only accurate to about 1dp; you should really always be using doubles for accuracy.

got it , was using float

gunjanbansal @ 13 May 2010 03:49 PM

got it , was using float instead of double :(

http://www.codechef.com/views

vastutsav @ 16 May 2010 09:58 PM

http://www.codechef.com/viewsolution/243257

plz provide some hint at where i m goin wrong.i m getting runtime error

thanks

i am getting time limit

scorpio90 @ 7 Jun 2010 11:48 PM
i am getting time limit exceeded....even though i hav used scanf and print f...plz help

here is my code


//#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

int main(){
int cases,points,final;
vector<int> x,chkx;
vector<int> y,chky,v;
int x1,y1,k,max,size,t=0;
vector<int> answerx[100];
vector<int> answery[100];

scanf("%d",&cases);
for(int i=0;i<cases;i++){
scanf("%d",&points);
for(int j=0;j<points;j++){
scanf("%d",&x1);
scanf("%d",&y1);

chkx.push_back(x1);
x.push_back(x1);
y.push_back(y1);
}
int flag=0;
sort(chkx.begin(),chkx.end());
for(int flag=0;flag<points;flag++){
for(int j=0;j<chkx.size();j++){

if(chkx[0]==x[j]){
v.push_back(j);
}
}
k=0;
max=0;
size=v.size();

if (v.size()>1){
while(size!=0){
if(y[v[k]]>max){
max=y[v[k]];
final=v[k];
}

k++;
size--;
}

}
else if(v.size()==1){
final=v[0];
}
else{}

answerx[i].push_back(x[final]);
answery[i].push_back(y[final]);

v.erase(v.begin(),v.end());

x.erase(x.begin()+final);
y.erase(y.begin()+final);
chkx.erase(chkx.begin());
}
//t++;
}
double ans;
for(int i=0;i<cases;i++){
ans=0.00;
for(int j=0;j<answerx[i].size()-1;j++){
int xx1,yy1,yy2,xx2;

xx1=answerx[i][j];
yy1=answery[i][j];
xx2=answerx[i][j+1];
yy2=answery[i][j+1];
ans=ans + sqrt((xx1-xx2)*(xx1-xx2)+(yy1-yy2)*(yy1-yy2));
}
printf("%.2fn",ans);
}
system("pause");
return 0;
}


Will the input consist of all

kinshuk2jain @ 15 Jun 2010 12:46 AM

Will the input consist of all of the points sorted, or if i need to sort them in my solution, i would have to do it explicily?

The problem statement doesn't

triplem @ 15 Jun 2010 07:05 AM

The problem statement doesn't say anything about the order the points can appear in, so you shouldn't assume anything.

Could anybody help me out: I

shrutiranjans @ 7 Jul 2010 03:12 PM

Could anybody help me out:

I am getting a runtime error(other), and despite reading the FAQ, I am unable to figure out where I am going wrong.

Link to my code is:

http://www.codechef.com/viewsolution/280103

It has been bugging me. Would be really grateful if someone could help.

Have you tried a test case

triplem @ 7 Jul 2010 03:22 PM

Have you tried a test case with 100000 points?

Have you read the section called 'Other common mistakes' in the FAQ?

(The answer to both is no ;))

@Stephen: I did try out my

shrutiranjans @ 7 Jul 2010 03:42 PM

@Stephen: I did try out my code on a test case generator of 100000 points. Seemed to work fine. Can you please help a little more?

As mentioned in the FAQ, a

triplem @ 7 Jul 2010 03:59 PM

As mentioned in the FAQ, a new line is \r\n, which you aren't handling properly.

I may have been mistaken on the first point, let me look again.

@stephen: Oh no, I am reading

shrutiranjans @ 7 Jul 2010 04:04 PM

@stephen: Oh no, I am reading 30000 characters at a time. I guess my problem is with the newline character. I am fixing it.

@stephen: any suggestion as

shrutiranjans @ 7 Jul 2010 04:21 PM

@stephen: any suggestion as to how to handle rn ?

I meant how to handle the

shrutiranjans @ 7 Jul 2010 04:22 PM

I meant how to handle the newline character?

can anyone tell me what's the

aabidhasan @ 6 Aug 2010 01:20 AM

can anyone tell me what's the problem here...it's giving wrong answer!!! but i couldn't find any problem... please help.

 

#include<stdio.h>

#include<math.h>

 

long x[100010],y[100010];

long i,j,k,t,n,p,q,r,temp1,temp2;

double sum;

 

int main(){

//freopen("input.txt","r",stdin);

scanf("%ld",&t);

for(p=0; p<t; p++){

scanf("%ld",&n);

for(q=0; q<n; q++){

scanf("%ld",&x[q]);

scanf("%ld",&y[q]);

}

 

for(i=0; i<n; i++){

for(j=i; j<n; j++){

if(x[i]>y[j]){

temp1 = x[i];

x[i] = x[j];

x[j] = temp1;

 

temp2 = y[i];

y[i] = y[j];

y[j] = temp2;

}

else if(x[i] == y[j]){

temp1 = y[i];

y[i] = y[j];

y[j] = temp1;

 

temp2 = x[i];

x[i] = x[j];

x[j] = temp2;

 

}

}

sum = 0;

for(i=0; i<n-1; i++){

sum = sum + sqrt(pow((double(x[i+1])-double(x[i])),2) + pow((double(y[i+1])-double(y[i])),2));

}

}

printf("%.2lfn",sum);

}

return 0;

}

i have no other choices but

sbcd90 @ 19 Aug 2010 01:46 PM

i have no other choices but to post my code.plz have a look....i have tried lots of test cases...but my answer is incorrect..plz help

 

 

#include<stdio.h>

#include<math.h>

#define p 100001

long long int a[p][2],u=-1,v=-1,n;

void quicksort(long long int,long long int);

int main()

{

int t,i;

long long int j,p1,q,r,s1,k;

double s;

scanf("%d",&t);

for(i=1;i<=t;i++)

{

k=0;

s=0.0;

scanf("%lld",&n);

for(j=0;j<n-k;)

{

scanf("%lld",&r);

scanf("%lld",&s1);

if((r!=0)||(s1!=0))

{

a[j][0]=r;

a[j][1]=s1;

j=j+1;

}

else if((r==0)&&(s1==0))

{

u=r;

v=s1;

k=k+1;

}

}

n=n-k;

quicksort(0,n-1);

for(j=0;j<(n-1);j=j+1)

{

p1=a[j][0]-a[j+1][0];

q=a[j][1]-a[j+1][1];

s=s+sqrt(pow(p1,2)+pow(q,2));

}

if((u==0)&&(v==0))

s=s+sqrt(pow(a[0][0],2)+pow(a[0][1],2));

printf("%.2fn",s);

}

return(0);

}

void quicksort(long long int left,long long int right)

{

long long int pivot,i,j;

long long int temp;

if(left<right)

{

i=left;

j=right+1;

pivot=a[left][0];

do

{

do

{

i=i+1;

}while(a[i][0]<pivot);

do

{

j=j-1;

}while(a[j][0]>pivot);

if(i<j)

{

temp=a[i][0];

a[i][0]=a[j][0];

a[j][0]=temp;

temp=a[i][1];

a[i][1]=a[j][1];

a[j][1]=temp;

}

}while(i<j);

temp=a[left][0];

a[left][0]=a[j][0];

a[j][0]=temp;

temp=a[left][1];

a[left][1]=a[j][1];

a[j][1]=temp;

quicksort(left,j-1);

quicksort(j+1,right);

}

for(j=0;j<right;j++)

{

if(a[j][0]==a[j+1][0])

{

if(a[j][1]<a[j+1][1])

{

temp=a[j][1];

a[j][1]=a[j+1][1];

a[j+1][1]=temp;

}

}

}

}

frnds can anybody help me out

Ankit.Sablok19 @ 22 Aug 2010 02:13 PM

frnds can anybody help me out on this problem

 

its clearly written that

You start at the point with the least X and greatest Y value, and end at the point with the greatest X and least Y value.

and for the last case the anser turns out to be the following order

 

(0,10)->(1,5)->(1,2)->(2,0)

for wich the answer turns out to be

10.33

hi i have used mergesort for

mukulbudania @ 4 Sep 2010 06:39 AM

hi

i have used mergesort for sorting and i am TLE.

@people who hav submitted code...

which algo u used for sorting??

i used quicksort too..

mukulbudania @ 4 Sep 2010 07:01 AM

i used quicksort too..

guys is blank line and new

nirajkant @ 13 Sep 2010 11:09 PM

guys is blank line and new line is same i mean n  tell me  :D

:( Internal Error Occured in

sukhwinder_ @ 20 Oct 2010 02:23 AM

:( Internal Error Occured in the system...

Do any1 know why this error comes up???

Hi, can we have 3 values of Y

dhiman_nikhil @ 12 Nov 2010 05:57 PM

Hi, can we have 3 values of Y for same value of X

Points can be anywhere they

triplem @ 13 Nov 2010 01:45 AM

Points can be anywhere they want.

well guys i have done my code

smartboss194 @ 14 Nov 2010 11:57 PM

well guys i have done my code quite simply & shortly using struct...everythings are ok except the time limit! m damn dead for knowing what the problem really is...please help somebody!here goes my code:

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

typedef struct
{
int x;
int y;
}point;
point points[10000];
void sort(int n)
{
int temp;
for(int i=(n-1);i>=1;i--)
{
for(int j=0;j<=(i-1);j++)
{

if(points[j].x>points[j+1].x)
{
temp=points[j].x;
points[j].x=points[j+1].x;
points[j+1].x=temp;
temp=points[j].y;
points[j].y=points[j+1].y;
points[j+1].y=temp;

}
else if(points[j].x==points[j+1].x)
{
if(points[j].y<points[j+1].y)
{
temp=points[j].y;
points[j].y=points[j+1].y;
points[j+1].y=temp;
}

}

}

}
}
void countDistance(int n)
{
double result=0.0;
for(int i=0;i<(n-1);i++)
{
result+=sqrt((points[i+1].x-points[i].x)*(points[i+1].x-points[i].x)+(points[i+1].y-points[i].y)*(points[i+1].y-points[i].y));
}
printf("%.2fn",result);
}

int main()
{
int test,iterator;
scanf("%d",&test);
for(int i=0;i<test;i++)
{
scanf("%d",&iterator);
for(int j=0;j<iterator;j++)
{
scanf("%d %d",&points[j].x,&points[j].y);

}

sort(iterator);
countDistance(iterator);

}

return 0;
}

oh...my struct point is

smartboss194 @ 15 Nov 2010 12:00 AM

oh...my struct point is something like point[100000]...either it shows runtime error!

but what making its to be so time consuming?

Your sort uses a nested loop,

triplem @ 15 Nov 2010 01:32 AM

Your sort uses a nested loop, and therefore takes around 100000*100000 iterations. That doesn't have a hope of running quickly. There are much faster sorting methods.

thanks i understood it after

smartboss194 @ 16 Nov 2010 11:45 AM

thanks i understood it after i had closed my pc in Anger! hehehe :P

hi friends i tried few

tarun_kumar @ 22 Nov 2010 08:25 AM

hi friends

i tried few submitted solutions, and i guess everyone is missing this condition: for points having the same X value, you need to visit the point with the greatest Y value before visiting the next point with the same X value.

 

because for the following test case

4

0 0

0 2

0 4

0 8

 

output should be

(0,8)-->(0,4)
(0,4)-->(0,8)
(0,8)-->(0,2)
(0,2)-->(0,8)
(0,8)-->(0,0)

the answer should be: 28

 

but the programs i tried from the submission are giving the answer: 8

 

as it is clearly written in the question that if u are visit a point with the same value of x, we have to visit the point with maximum y. i think by visiting to that point we have to add distance from current point to that point. and then from point with maximum y to the next point.

admin if i m wrong please correct me

hi friendsi tried few

tarun_kumar @ 22 Nov 2010 08:58 AM

hi friends
i tried few problems from the submission and found that they all are not obeying this condition: for points having the same X value, you need to visit the point with the greatest Y value before visiting the next point with the same X value.

if we obey this condition for the following test case

4
0 0
0 2
0 4
0 8

output should be: 28

Explanation:
(0,8)-->(0,4)
(0,4)-->(0,8) *
(0,8)-->(0,2)
(0,2)-->(0,8) *
(0,8)-->(0,0)


but if we do not follow this condition:
(0,8)-->(0,4)
(0,4)-->(0,2)
(0,2)-->(0,0)

output: 8


as it is clearly mentioned that if we have to visit any point with same x we have to visit the point with maximum y. in explanation the visit marked with * are the same visits.
admin please correct me if i m wrong.

The problem is written in

triplem @ 22 Nov 2010 12:54 PM

The problem is written in pretty poor english; taken literally you are correct. The problems intends you to visit points (where x is the same) from the highest y value down to the lowest value.

Stephen: ya you are correct.

tarun_kumar @ 22 Nov 2010 01:48 PM

Stephen: ya you are correct. later i also realised that for coordinates with same we just have to take minimum and max value of y and leave the coordinates in b/w

thanx anyway

I kept getting wrong answer

jhlu87 @ 16 Dec 2010 07:53 AM

I kept getting wrong answer until i changed the way I formatted my output. I still don't know what's wrong with my original output method can someone help?

originally i had this

  1. DecimalFormat twoDForm = new DecimalFormat("#.##");
  2. returnVal = Double.valueOf(twoDForm.format(distance)).toString();
  3. if (returnVal.endsWith(".0")) {
  4. returnVal = returnVal.concat("0");
  5. }
  6. System.out.println(returnVal);
    when i changed it to
    1. returnVal = Double.valueOf(twoDForm.format(distance));
    2. System.out.print(String.format("%.2fn",returnVal));
      it suddenly worked with everything else the same. What was wrong with the first approach?

The toString() method of

triplem @ 16 Dec 2010 08:11 AM

The toString() method of Double will show any value which is at least 10^7 (possible here with the right input) in exponential notation, which is what is causing the wrong answer.

The simplest correct code doesn't involve any DecimalFormat class at all:

System.out.printf("%.2f\n",distance);

ah thanks for the

jhlu87 @ 16 Dec 2010 09:15 AM

ah thanks for the explanation. I realized i could just go with

System.out.printf("%.2fn",distance);

right after i pressed save on the previous comment

To Stephen: When I upload my

rajneesh2k10 @ 29 Dec 2010 03:12 PM

To Stephen:

When I upload my program, it goes on running without showing any message. What do you think, what should have gone wrong??

To Stephen: When I upload my

rajneesh2k10 @ 30 Dec 2010 07:48 AM

To Stephen:

When I upload my program, it goes on running without showing any message. What do you think, what should have gone wrong??

To Admin: When I upload my

rajneesh2k10 @ 31 Dec 2010 07:54 AM

To Admin:

When I upload my program, it goes on running and running without showing any message. What do you think should have gone wrong??

Please help!

BUUUUUUUUUUUZZZZZZZZZZZZZZZZZ

rajneesh2k10 @ 31 Dec 2010 04:26 PM

BUUUUUUUUUUUZZZZZZZZZZZZZZZZZZZZZ!!!!!!!!!!!!!!

 

Is there anyone to answer my query????

Hello everyone... Iam Aafreen

aafreen_ahmed @ 10 Apr 2011 11:51 AM

Hello everyone...

Iam Aafreen student of B.C.A. I am just a begginner in the field of progrramming...and fine myself unable in understanding the logic of many problems...I hav very little knowledge abt programming...can someone tell me how can I learn programming in C and become a better programmer.

Changed float to double,

theslavemaster @ 13 May 2011 10:51 PM

Changed float to double, worked like a charm ;)

Can anyone tell me please

janardhan7 @ 15 May 2011 10:46 AM

Can anyone tell me please what is wrong in my code;

#include<stdio.h>

#include<math.h>

#define d(x,y,a,b) (sqrt((x-a)*(x-a)+(y-b)*(y-b)))

void calc(int [][2],int);

void mergesort(int [][2],int,int);

void merge(int [][2],int,int,int);

void mergesort1(int [][2],int,int);

void merge1(int [][2],int,int,int);

int b[100001][2];

main()

{

int t,i,m,j,k,num[100001][2],o;

scanf("%d",&t);

char waste;

for(o=1;o<=t;o++)

{ do{waste=getchar();}while(waste<'0'||waste>'9');

m=waste-'0';

for(j=1;j<=m;j++)

scanf("%d%d",&num[j][0],&num[j][1]);

mergesort1(num,1,m);

j=1;

while(j<m)

{  i=num[j][0];

for(k=j+1;num[k][0]==i&&k<=m;k++);

mergesort(num,j,k-1);

j=k;

}

 

calc(num,m);

}

return 0;

}

void calc(int num[][2],int m)

{   int i,j=1,k,l;

double total=0;

while(j<m)

{  i=num[j][0];

for(k=j+1;num[k][0]==i&&k<=m;k++);

if(k>j+1&&k<=m)

{total+=fabs(-num[k-1][1]+num[j][1])+d(num[k-1][0],num[k-1][1],num[k][0],num[k][1]);}

else if(k>j+1&&k>m)

{total+=fabs(-num[k-1][1]+num[j][1]);}

else if(k==j+1)

{ total+=d(num[j][0],num[j][1],num[k][0],num[k][1]);}

j=k;

}

double t=total*100+0.5;

int y=floor(t);

t=(double)y/100;

printf("%.2lfn",t);

}

void mergesort1(int num[][2],int i,int j)

{

int mid;

if(i<j)

{ mid=(i+j)/2;

mergesort1(num,i,mid);

mergesort1(num,mid+1,j);

merge1(num,i,mid,j);

}

}

void merge1(int num[][2],int low,int mid,int high)

{

int i,j,k,l;

i=low;

j=mid+1;

for(k=low;i<=mid&&j<=high;)

if(num[i][0]<num[j][0])

{ b[k][0]=num[i][0];

b[k++][1]=num[i++][1];

}

else

{ b[k][0]=num[j][0];

b[k++][1]=num[j++][1];

}

 

if(i>mid)

for(l=j;l<=high;l++)

{ b[k][0]=num[l][0];

b[k++][1]=num[l][1];}

else if(j>high)

for(l=i;l<=mid;l++)

{ b[k][0]=num[l][0];

b[k++][1]=num[l][1];}

 

for(l=low;l<=high;l++)

{ num[l][0]=b[l][0];

num[l][1]=b[l][1];}

}

 

void mergesort(int num[][2],int i,int j)

{

int mid;

if(i<j)

{ mid=(i+j)/2;

mergesort(num,i,mid);

mergesort(num,mid+1,j);

merge(num,i,mid,j);

}

}

void merge(int num[][2],int low,int mid,int high)

{

int i,j,k,l;

i=low;

j=mid+1;

for(k=low;i<=mid&&j<=high;)

if(num[i][1]>num[j][1])

{ b[k][0]=num[i][0];

b[k++][1]=num[i++][1];

}

else

{ b[k][0]=num[j][0];

b[k++][1]=num[j++][1];

}

 

if(i>mid)

for(l=j;l<=high;l++)

{ b[k][0]=num[l][0];

b[k++][1]=num[l][1];}

else if(j>high)

for(l=i;l<=mid;l++)

{ b[k][0]=num[l][0];

b[k++][1]=num[l][1];}

 

for(l=low;l<=high;l++)

{ num[l][0]=b[l][0];

num[l][1]=b[l][1];}

}

seriously, guys. try using

anirudh24seven @ 14 Jun 2011 09:28 PM
seriously, guys. try using the python code provided by jcomeau_ictx @ 29 Nov 2009 08:04 AM. really good! found my mistake immediately! thanks jcomeau_ictx.

use double,not float and

netex @ 26 Jun 2011 06:37 AM
use double,not float and you're ok.

is the input sequence sorted

cool_toad @ 17 Aug 2011 07:23 PM
is the input sequence sorted in x or y (or both)?

@cool_toad: No, the input

vijay91 @ 17 Aug 2011 10:44 PM
@cool_toad: No, the input sequence is not sorted

pls any1 say what is the

remogoku @ 19 Aug 2011 02:09 PM
pls any1 say what is the error..?? compiler showing runtime error.. but working fine when i compile.. #include #include int main(void) { int x[40],y[40],i,j,t,temp_x,temp_y,n,temp; float d; scanf("%d",&t); while(t!=0) { printf("n"); scanf("%d",&n); for(i=0; i= x[j]) { if(temp_x == x[j]) { if(temp_y < y[j]) { temp = temp_y; temp_y = y[j]; y[j] = temp; } } else { temp = temp_x; temp_x = x[j]; x[j] = temp; temp = temp_y; temp_y = y[j]; y[j] = temp; } } } x[i] = temp_x; y[i] = temp_y; } d=0; for(i=0; i

@remogoku: You are using

vijay91 @ 19 Aug 2011 03:37 PM
@remogoku: You are using array of just 40 size which is not sufficient since 2<=n<=100000 and i think 'int' also won't work...increase the size of arrays and use long int instead of int and appropriate format specifiers for it.

hey, i did it using

gohil90 @ 15 Sep 2011 12:38 PM
hey, i did it using java.util.Comparator.....it helped a lot to minimizing sorting time.....try that guys....

hey i m getting a TLE....can

anu120987 @ 15 Sep 2011 11:02 PM
hey i m getting a TLE....can someone help me optimize my code link to my code is here....http://www.codechef.com/viewsolution/661937

hey i m getting a TLE....can

anu120987 @ 15 Sep 2011 11:03 PM
hey i m getting a TLE....can someone help me optimize my code link to my code is here....http://www.codechef.com/viewsolution/661937

plz help Why

s1aurabhpal_7 @ 25 Sep 2011 01:55 PM
plz help Why TLE??????????? #include #include void sort(int *a,int *b,int c) { for(int i=0;i-1 && keya<*(a+j)) { *(a+j+1)=*(a+j);*(b+j+1)=*(b+j);j--; } *(a+j+1) = keya;*(b+j+1) = keyb; } for(int i=0;i-1 && keyb>*(b+u) && keya==*(a+u)) { *(a+u+1)=*(a+u);*(b+u+1)=*(b+u);u--; } *(a+u+1) = keya;*(b+u+1) = keyb; } } } float dist(int *a,int *b,int c) { float d=0.0; sort(a,b,c); for(int i=0;i1) { for(int j=0;j

sorry for last comment..but

s1aurabhpal_7 @ 25 Sep 2011 01:56 PM
sorry for last comment..but why TLE??? #include #include void sort(int *a,int *b,int c) { for(int i=0;i-1 && keya<*(a+j)) { *(a+j+1)=*(a+j);*(b+j+1)=*(b+j);j--; } *(a+j+1) = keya;*(b+j+1) = keyb; } for(int i=0;i-1 && keyb>*(b+u) && keya==*(a+u)) { *(a+u+1)=*(a+u);*(b+u+1)=*(b+u);u--; } *(a+u+1) = keya;*(b+u+1) = keyb; } } } float dist(int *a,int *b,int c) { float d=0.0; sort(a,b,c); for(int i=0;i1) { for(int j=0;j

#include #include void

s1aurabhpal_7 @ 25 Sep 2011 02:40 PM
#include #include void sort(int *a,int *b,int c) { for(int i=0;i-1 && keya<*(a+j)) {*(a+j+1)=*(a+j);*(b+j+1)=*(b+j);j--;} *(a+j+1) = keya;*(b+j+1) = keyb;} for(int i=0;i-1 && keyb>*(b+u) && keya==*(a+u)) {*(a+u+1)=*(a+u);*(b+u+1)=*(b+u);u--;} *(a+u+1) = keya;*(b+u+1) = keyb; } } } float dist(int *a,int *b,int c) { float d=0.0; sort(a,b,c); for(int i=0;i1) {for(int j=0;j

I m always getting right

amriteshanand @ 29 Sep 2011 11:49 PM
I m always getting right answer but it says wrong answer here can anyone give me the corner case or tell me what i m missing .. here is my code #include #include int main(void) { int t,n,i,p[10001][2],x,y,prev; float ans; scanf("%d",&t); while(t--) { ans=0; scanf("%d",&n); for(i=0;i<10001;i++) p[i][1]=p[i][0]=-1; for(i=0;ip[x][1]) p[x][1]=y; } prev=-1; for(i=0;i<10001;i++) { if(p[i][1]==-1) continue; if(prev==-1) { prev=i; ans+=p[i][1]-p[i][0]; continue; } ans+=sqrt((p[i][1]-p[prev][0])*(p[i][1]-p[prev][0])+(i-prev)*(i-prev)); ans+=p[i][1]-p[i][0]; prev=i; } printf("%.2fn",ans); } return 0; }

Plz tell what is the problem

mygaurav @ 26 Oct 2011 05:24 PM
Plz tell what is the problem with my code...it is working fine on my system with the test cases, but is giving wrong answer http://www.codechef.com/viewsolution/713847

@mygaurav: total distance can

imnewcoder @ 27 Oct 2011 01:14 AM
@mygaurav: total distance can be very large, use double instead of float.

Thanks imnewcoder :)

mygaurav @ 27 Oct 2011 08:37 AM
Thanks imnewcoder :)

WA WA WA,.. Can sum1 help..

monikadaryani @ 7 Nov 2011 04:28 PM
WA WA WA,.. Can sum1 help.. @ admin.. wat is the prob wid the code.. http://www.codechef.com/viewsolution/727355

what's wrong with my code i

krats @ 15 Dec 2011 11:42 AM
what's wrong with my code i am getting WA here is the link http://www.codechef.com/viewsolution/757420 please help

#include #include int

ashishyogi @ 18 Dec 2011 11:30 AM
#include #include int main() { int t; int x[10000]; int y[10000]; scanf("%d",&t); int r,n,s; for(r=0;rx[s+1]) { temp=x[s]; //sorted according to precedence; x[s]=x[s+1]; x[s+1]=temp; temp=y[s]; y[s]=y[s+1]; y[s+1]=temp; } else continue; } float sum=0; for(s=0;sy[s]) { temp=y[s]; y[s]=y[s+1]; y[s+1]=temp; sum=sum+sqrt(pow((x[s]-x[s+1]),2)+pow((y[s]-y[s+1]),2)); } printf("n"); } printf("%.2f",sum); printf("n"); } return 0; } wats wrong in this code; its giving me wrong answer admin plz help

Can anyone explain how 0,0

ganesh02 @ 25 Dec 2011 10:58 PM
Can anyone explain how 0,0 -> 1,10 1,10 -> 1,5 1,5 -> 2,2 wil become18.21 I could not understand the logic

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com