10812 - Beat the Spread!

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

Moderator: Board moderators

10812 - Beat the Spread!

Postby noddy693 » Mon Mar 21, 2005 2:02 am

Hello all,

I'm having a bit of a trouble with problem 10812: Beat the Spread!
Although the problem seems more than easy: x+y = s, x-y = d => ... => x = (s+d)/2 and y = (s-d)/2, finally check if both x and y a are non negative.

The problem is that I keep getting WA and I cannot figure why. Any sugestions?

Thanks in advance.

./noddy
noddy693
New poster
 
Posts: 2
Joined: Mon Mar 21, 2005 1:56 am

Postby mf » Mon Mar 21, 2005 2:09 am

You should check whether all these conditions hold: s >= d, (s + d) even, (s - d) even. When either fails, the answer is "impossible."
Last edited by mf on Mon Mar 21, 2005 8:26 pm, edited 1 time in total.
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

Postby noddy693 » Mon Mar 21, 2005 2:44 am

mf wrote:You should check whether all these conditions hold: s >= d, (s + d) even, (s - d) even. Even either fails, the answer is "impossible."


Many thanks for your help. I forgot to check the even conditions.

./noddy
noddy693
New poster
 
Posts: 2
Joined: Mon Mar 21, 2005 1:56 am

Postby taborda » Mon Mar 21, 2005 6:05 pm

why do we have to check if s+d and s-d are even ?...i can't understand that...in the problem they don't talk about that...can someone xplain me??
taborda
New poster
 
Posts: 6
Joined: Wed Mar 16, 2005 1:57 am

Re

Postby Raj Ariyan » Mon Mar 21, 2005 6:22 pm

Hi taborda,
Lets see an example :-

x=15,y=12
then x+y=27 (odd)
and x-y=3 (odd)
then if u do 27/2=13.5
and 3/2=1.5 all the result is fraction
now we see that there is no two integer(not fraction) which summation is X and difference is Y, thats why we check it for even. So for this input output should be "impossible". Hope it helps. Good Luck.
Some Love Stories Live Forever ....
Raj Ariyan
Learning poster
 
Posts: 70
Joined: Sat Feb 05, 2005 9:38 am
Location: Gurukul

10812 So easy... =(

Postby gateKeeper » Thu Jun 09, 2005 11:48 pm

This problem is giving me some WA's and I dont know how to solve this. This problem is very easy, I know, but my code is wrong somewhere. I'll leave the code I'm using, here. I hope someone can give me an hint. This problem doesn't deserve so much time. Thanks...

Code: Select all
#include "stdio.h"

int main()
{
   unsigned long int num, i, first, sec, x, y;

   scanf("%lu", &num);

   for(i=0; i<num;i++)
   {
      scanf("%lu", &first);
      scanf("%lu", &sec);


      x=(first+sec)/2;
      y=(first-sec)/2;

      if(first<=sec || (first+sec)%2 || (first-sec)%2 || x<0 || y<0)
      {
         printf("impossible\n");
         continue;
      }

      
      printf("%lu %lu\n", y, x);
   }
   return 0;
}

gateKeeper
New poster
 
Posts: 2
Joined: Sat Jun 04, 2005 10:07 pm

Postby Antonio Ocampo » Fri Jun 10, 2005 4:10 am

Antonio Ocampo
Experienced poster
 
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Re: 10812 So easy... =(

Postby CDiMa » Fri Jun 10, 2005 9:14 am

gateKeeper wrote:
Code: Select all
if(first<=sec || (first+sec)%2 || (first-sec)%2 || x<0 || y<0)
{
   printf("impossible\n");
   continue;
}

Checking (first<=sec) discards possible solutions...

Ciao!!!

Claudio
CDiMa
Experienced poster
 
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

10812...WA??

Postby asif_rahman0 » Sun Jun 11, 2006 5:38 pm

where is the fault in my code??
plz help.

Code: Select all
removed
Last edited by asif_rahman0 on Sun Jun 11, 2006 8:32 pm, edited 1 time in total.
asif_rahman0
Experienced poster
 
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

Re: 10812...WA??

Postby Martin Macko » Sun Jun 11, 2006 8:07 pm

asif_rahman0 wrote:where is the fault in my code??
plz help.

There is already a topic on this problem. See http://online-judge.uva.es/board/viewtopic.php?t=7782. Never create a new topic on a problem if there is already one on that problem.

Anyway, your solution assumes something that's not written in the problem statement. What if s=d? Read the problem description more carefully.
User avatar
Martin Macko
A great helper
 
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Postby asif_rahman0 » Sun Jun 11, 2006 8:31 pm

thnx martin:).

but just before u posted i got it accepted:D

sorry for posting it again as a new post.:(
asif_rahman0
Experienced poster
 
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

Postby Steven Luck » Mon Dec 11, 2006 5:30 am

I still don't get it. I've tried all possible corrections, but none get AC.
I suppose that both the sum & difference must be either even or odd. I still get WA.
e.g.:

1. 15 12 (Odd & Even)
x + y = 15
x - y = 12
------------- +
x = (15 - 12) / 2
(doesn't fulfil)

2. 14 11 (Even & Odd)
x + y = 14
x - y = 11
------------- +
x = (14 - 11) / 2
(doesn't fulfil)

3. 15 11 (Odd & Odd)
x + y = 15
x - y = 11
------------- +
x = (15 - 11) / 2
(fulfils)

4. 14 10 (Even & Even)
x + y = 14
x - y = 10
------------- +
x = (14 - 10) / 2
(fulfils)

Here's my code:
Code: Select all
#include<stdio.h>

void main()
{
   int i, s, d, n, a, c;
   while(scanf("%d", &n)==1)
   {
      for(i=0; i<n; i++)
      {
         scanf("%d %d", &s, &d);
         a = (s - d) / 2;
         if((s%2==0 && d%2!=0) || (s%2!=0 && d%2==0) || (s-d)<0) printf("impossible\n");
         else
         {   if(a < s-a){ c = a; a = s - a; }
            printf("%d %d\n", a, c);
         }
      }
   }
}
"The Only Thing For The Triumph of Evil is For a Good Man to Do Nothing"
Steven Luck
New poster
 
Posts: 13
Joined: Sat Dec 02, 2006 7:51 am
Location: Indonesia

Postby Jan » Mon Dec 11, 2006 10:21 am

Suppose the input is 20 0. Now according to your code...

Code: Select all
         scanf("%d %d", &s, &d); /* s = 20, d = 0 */
         a = (s - d) / 2; /* So, a = 10 */
         if((s%2==0 && d%2!=0) || (s%2!=0 && d%2==0) || (s-d)<0)
             printf("impossible\n"); /* s and d both are even and s-d > 0 */
         else
         {   if(a < s-a){ c = a; a = s - a; } /* a = 10, s-a = 10, so a is not less than (s-a) */
            printf("%d %d\n", a, c); /* Now c is not initialized */
         }

The output should be 10 10. Hope it helps.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby Steven Luck » Tue Dec 12, 2006 6:27 pm

Got it! Thanx a lot, Jan :)
"The Only Thing For The Triumph of Evil is For a Good Man to Do Nothing"
Steven Luck
New poster
 
Posts: 13
Joined: Sat Dec 02, 2006 7:51 am
Location: Indonesia

Re: whats wrong??- Beat the Spread!

Postby sazzadcsedu » Fri Dec 26, 2008 11:01 pm

plz someone help me.
why wrong ans!!!
here my code.
Code: Select all
  #include<stdio.h>

 
        int main()

     {  int s1,s2;
 
        int  s,d;
        int numinput,i;

        scanf("%d",&numinput);

      for(i=1;i<=numinput;i++)
      {
      scanf("%d %d",&s,&d);

        if(d>=s){

        printf("impossible\n"); 
       }
        else if
            ((s+d)%2!=0 || (s-d)%2!=0 || (s+d)<0 || (s-d)<0)

        printf("impossible\n");

        else 
     {   s1=((s+d)/2);
        s2=s-s1;
     
        printf("%d %d\n",s1,s2);
     }
   
   }   
      return 0;
   }       
sazzadcsedu
Experienced poster
 
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.

Next

Return to Volume CVIII

Who is online

Users browsing this forum: No registered users and 1 guest

cron