codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

733B.md (3129B)


      1 Source: https://codeforces.com/problemset/problem/733/B
      2 
      3 B. Parade
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
     22 
     23 There will be *n* columns participating in the parade, the *i*-th column consists of *l**i* soldiers, who start to march from left leg, and *r**i* soldiers, who start to march from right leg.
     24 
     25 The beauty of the parade is calculated by the following formula: if *L* is the total number of soldiers on the parade who start to march from the left leg, and *R* is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |*L* - *R*|.
     26 
     27 No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index *i* and swap values *l**i* and *r**i*.
     28 
     29 Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
     30 
     31 Input
     32 
     33 The first line contains single integer *n* (1 ≤ *n* ≤ 105) — the number of columns.
     34 
     35 The next *n* lines contain the pairs of integers *l**i* and *r**i* (1 ≤ *l**i*, *r**i* ≤ 500) — the number of soldiers in the *i*-th column which start to march from the left or the right leg respectively.
     36 
     37 Output
     38 
     39 Print single integer *k* — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
     40 
     41 Consider that columns are numbered from 1 to *n* in the order they are given in the input data.
     42 
     43 If there are several answers, print any of them.
     44 
     45 Examples
     46 
     47 Input
     48 
     49 ```
     50 3  
     51 5 6  
     52 8 9  
     53 10 3
     54 ```
     55 
     56 Output
     57 
     58 ```
     59 3
     60 ```
     61 
     62 Input
     63 
     64 ```
     65 2  
     66 6 5  
     67 5 6
     68 ```
     69 
     70 Output
     71 
     72 ```
     73 1
     74 ```
     75 
     76 Input
     77 
     78 ```
     79 6  
     80 5 9  
     81 1 3  
     82 4 8  
     83 4 5  
     84 23 54  
     85 12 32
     86 ```
     87 
     88 Output
     89 
     90 ```
     91 0
     92 ```
     93 
     94 Note
     95 
     96 In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
     97 
     98 If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
     99 
    100 It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.