You are not logged in. Please login at www.codechef.com to post your questions!

×

plz check [MAXDIFF]

what was the error in this code???

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

asked 15 Apr '13, 20:00

hitesh091's gravatar image

3★hitesh091
198102535
accept rate: 0%


after scanning b and c, add if(c>(b-c)) c=b-c; That should get you AC :)

link

answered 15 Apr '13, 20:23

aichemzee's gravatar image

4★aichemzee
42061224
accept rate: 3%

This is the logic I also used and wanted to share. More precisely, if(2*K>N){ K = N - K; } Meanwhile try to use some meaningful variable names like the ones given in codechef questions.

(19 Apr '13, 08:36) shiwakant4★
1

@aichemzee suggested the changes to @hitesh091 code who used such weird names.

(19 Apr '13, 14:28) anton_lunyov ♦6★

^exactly. As in general you could do k=Math.min(k,n-k); to get AC.

(19 Apr '13, 15:54) aichemzee4★

There's 2 possibilities, and you've got to check them both.

eg. K = 2, for list [1,2,3,...,98,99,100]

either:

[1,2] [3,...,98,99,100]

or:

[99,100] [1,2,3,...,98]

link

answered 15 Apr '13, 20:05

evandrix's gravatar image

3★evandrix
165148
accept rate: 0%

@evandrix: still not clear sir...

(15 Apr '13, 20:21) hitesh0913★

@evandrix: not clear sir..

link

answered 15 Apr '13, 20:13

hitesh091's gravatar image

3★hitesh091
198102535
accept rate: 0%

1

@hitesh091: Take for example the case
K=2 and list is 1 1 1 1 3
the two possible arrangements would be (1)+(1,1,1,3) and (1,1,1,1)+(3)
you have to take the one with maximum difference.
here, in first case the difference is 5 and in the second case, difference is 1. so, you select the first arrangement.

(15 Apr '13, 20:30) manasvi20012★
(15 Apr '13, 21:09) hitesh0913★
1

@hitesh091: yes, there is an error in your logic. the error being, you are only considering first k small elements.
Now consider this example.
let K=4 and the list be 1 1 1 1 9.
the ways your program is running is, it is picking first 4 elements for the kid, thus making the distribution 4 for the kid and 9 for the chef. Difference = 5.
but the correct way of doing this would be giving the last 4 elements to the chef and first element to the kid thus the distribution would be 1 for kid and 12 for the chef. Difference = 11.

(15 Apr '13, 21:32) manasvi20012★
(15 Apr '13, 21:32) manasvi20012★

in addition to that if you would have done
if(k>(n/2))
k=n-k;
you would have got it accepted.

(16 Apr '13, 09:55) manasvi20012★

thanks.. now i got it.. :)

(16 Apr '13, 19:25) hitesh0913★
showing 5 of 6 show all

Hi Can anyone check this code http://www.codechef.com/viewsolution/2059067 and tell me why am I getting "wrong answer". As I check, the output and input format and answers are exactly the same. Thanks

link

answered 19 Apr '13, 21:55

code_un_code's gravatar image

0★code_un_code
11
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,490
×858
×58

question asked: 15 Apr '13, 20:00

question was seen: 1,233 times

last updated: 19 Apr '13, 21:55