algorithm - Run time Error on URI Online Judge -


i trying following problem on uri oj

after buying many adjacent farms @ west region of santa catarina, star family built single road passes farms in sequence. first farm of sequence named star 1, second star 2, , on. however, brother lives in star 1 has got mad , decided make star trek in order steal sheep proprieties of siblings. crazy. when passes farm star i, steals 1 sheep (if there any) farm , moves on either star + 1 or star - 1, depending on whether number of sheep in star was, respectively, odd or even. if there not star wants go, halts trek. mad brother starts star trek in star 1, stealing sheep own farm.

input

the first input line consists of single integer n (1 ≤ n ≤ 106), represents number of stars. second input line consists of n integers, such ith integer, xi (1 ≤ xi ≤ 10^6), represents initial number of sheep in star i.

output

output line containing 2 integers, first represents number of stars attacked mad brother , second represents total number of non-stolen sheep.

i tried solve following problem using brute force. every time submit, receiving command terminated signal (11: sigsegv). searched error occurs when trying access memory segment not exist, can improve code, , error occurring. tried looking , came fact can have python list of size 536870912 safely, guessing 10^6 being smaller safe.

n = int(raw_input()) x = map(int, raw_input().split()) total, i, stolen, visited = sum(x), 0, 0, set() while in range(n) , x[i]:     visited.add(i)     stolen += 1     x[i] -= 1     if (x[i] + 1) % 2 == 1:         += 1     else:         -= 1 print(str(len(visited)) + ' ' + str(total - stolen)) 

max size of python list

here sample runs of program

input

8
1 3 5 7 11 13 17 19

expected output

8 68

output

8 68

input

8
1 3 5 7 11 13 16 19

expected output

7 63

output

7 63

i don't know python cannot finding bug, think algorithm buggy.

the problem says:

if there not star wants go, halts trek.

however seems algorithm stops if there no sheep left on farm.

anyway problem looks quite simple in form inserted think range of first parameter not

(1 ≤ n ≤ 106)

but rather

(1 ≤ n ≤ 106)

in case size of list problem... if limit available memory code.

but don't need store input parameters! can calculate stolen sheep quite on fly:

  1. you start first farm.

2.1. if there number of sheep here none of following farms visited , 1 sheep stolen here.

2.2. if there odd number of sheep here 1 sheep stolen right now, , 1 more when mad star comes (at time there number of sheep here , in of previous farms mad start won't visit more.) have handle case when there 1 sheep @ farm because in case 1 sheep can stolen here.

  1. handle next farm

at end have handle 1 more special case: if there number of sheep in each of farms 1 sheep stolen each farm, because in case mad star never turns back. (and there @ least 1 sheep in each farm)

here simple algorithm pseudo code:

int n= read(); boolean visit = true; int visited = 0; int total = 0; int stolen = 0; while(--n >0 ) {   int x = read();   total+=x;   if(visit) {     visited++;     if(x%2 == 1) {       stolen+=min(x,2);     } else {       stolen+=1;       visit = false;     }   } } if(visit) {   print(n + ' ' = (total-n)); } else {   print(visited + ' ' + (total-stolen)); } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -