B. Parade

Codeforces
IDCF733B
Time1000ms
Memory256MB
Difficulty
math
English · Original
Chinese · Translation
Formal · Original
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. 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. 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_|. **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_. 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. ## Input The first line contains single integer _n_ (1 ≤ _n_ ≤ 105) — the number of columns. 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. ## Output 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. Consider that columns are numbered from 1 to _n_ in the order they are given in the input data. If there are several answers, print any of them. [samples] ## Note 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. 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. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
很快,伯兰将举行一场庆祝战胜外星入侵者的阅兵式。但不幸的是,所有士兵都在战争中阵亡,现在军队完全由新兵组成,其中许多人甚至不知道应该从哪条腿开始行进。平民群众也不清楚新兵应该从哪条腿开始行进,因此唯一重要的是有多少士兵能保持步调一致。 将有 #cf_span[n] 个方阵参加阅兵,第 #cf_span[i] 个方阵包含 #cf_span[li] 名从左腿开始行进的士兵和 #cf_span[ri] 名从右腿开始行进的士兵。 阅兵的美感由以下公式计算:设 #cf_span[L] 为所有从左腿开始行进的士兵总数,#cf_span[R] 为所有从右腿开始行进的士兵总数,则美感等于 #cf_span[|L - R|]。 你最多可以进行一次操作:选择一个方阵,并命令该方阵中的所有士兵交换开始行进的腿——即原本从左腿开始的士兵现在从右腿开始,反之亦然。形式上,你可以选择至多一个下标 #cf_span[i],并交换 #cf_span[li] 和 #cf_span[ri] 的值。 请找出一个方阵的编号,使得对该方阵中的士兵交换开始行进的腿后,阅兵的美感最大化;或者确定当前美感已达到最大,无需任何操作。 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 105])——方阵的数量。 接下来的 #cf_span[n] 行每行包含一对整数 #cf_span[li] 和 #cf_span[ri](#cf_span[1 ≤ li, ri ≤ 500])——分别表示第 #cf_span[i] 个方阵中从左腿和右腿开始行进的士兵人数。 请输出一个整数 #cf_span[k] —— 需要让士兵交换开始行进腿的方阵编号,若当前美感已最大,则输出 #cf_span[0]。 假设方阵按输入顺序编号为 1 到 #cf_span[n]。 如果有多个答案,输出任意一个即可。 在第一个例子中,如果不进行任何操作,从左腿开始行进的士兵总数为 #cf_span[5 + 8 + 10 = 23],从右腿开始的为 #cf_span[6 + 9 + 3 = 18],此时美感为 #cf_span[|23 - 18| = 5]。 如果对第三个方阵进行交换操作,则从左腿开始行进的士兵总数变为 #cf_span[5 + 8 + 3 = 16],从右腿开始的变为 #cf_span[6 + 9 + 10 = 25],此时美感为 #cf_span[|16 - 25| = 9]。 无法通过其他操作获得更大的美感,因此能达到的最大美感为 9。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 105])——方阵的数量。接下来的 #cf_span[n] 行每行包含一对整数 #cf_span[li] 和 #cf_span[ri](#cf_span[1 ≤ li, ri ≤ 500])——分别表示第 #cf_span[i] 个方阵中从左腿和右腿开始行进的士兵人数。 ## Output 请输出一个整数 #cf_span[k] —— 需要让士兵交换开始行进腿的方阵编号,若当前美感已最大,则输出 #cf_span[0]。假设方阵按输入顺序编号为 1 到 #cf_span[n]。如果有多个答案,输出任意一个即可。 [samples] ## Note 在第一个例子中,如果不进行任何操作,从左腿开始行进的士兵总数为 #cf_span[5 + 8 + 10 = 23],从右腿开始的为 #cf_span[6 + 9 + 3 = 18],此时美感为 #cf_span[|23 - 18| = 5]。 如果对第三个方阵进行交换操作,则从左腿开始行进的士兵总数变为 #cf_span[5 + 8 + 3 = 16],从右腿开始的变为 #cf_span[6 + 9 + 10 = 25],此时美感为 #cf_span[|16 - 25| = 9]。 无法通过其他操作获得更大的美感,因此能达到的最大美感为 9。
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of columns. For each column $ i \in \{1, \dots, n\} $, let $ l_i, r_i \in \mathbb{Z}^+ $ denote the number of soldiers starting with the left and right leg, respectively. Define: - $ L = \sum_{i=1}^n l_i $: total soldiers starting with left leg. - $ R = \sum_{i=1}^n r_i $: total soldiers starting with right leg. - $ \Delta = |L - R| $: current beauty. **Constraints** 1. $ 1 \leq n \leq 10^5 $ 2. $ 1 \leq l_i, r_i \leq 500 $ for all $ i \in \{1, \dots, n\} $ **Objective** Let $ \Delta' $ be the beauty after swapping $ l_i $ and $ r_i $ in a single column $ i $. Swapping in column $ i $ changes: - $ L \to L - l_i + r_i $ - $ R \to R - r_i + l_i $ - New difference: $ D_i = |(L - l_i + r_i) - (R - r_i + l_i)| = |L - R + 2(r_i - l_i)| $ Find $ k \in \{0, 1, \dots, n\} $ such that: - If $ k = 0 $: $ \Delta $ is maximal (no swap improves beauty). - If $ k \geq 1 $: $ D_k > \Delta $, and $ k $ maximizes $ D_k $. Output $ k $ such that $ D_k = \max_{i \in \{0,1,\dots,n\}} D_i $, where $ D_0 = \Delta $.
Samples
Input #1
3
5 6
8 9
10 3
Output #1
3
Input #2
2
6 5
5 6
Output #2
1
Input #3
6
5 9
1 3
4 8
4 5
23 54
12 32
Output #3
0
API Response (JSON)
{
  "problem": {
    "name": "B. Parade",
    "description": {
      "content": "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 kno",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF733B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 kno...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "很快,伯兰将举行一场庆祝战胜外星入侵者的阅兵式。但不幸的是,所有士兵都在战争中阵亡,现在军队完全由新兵组成,其中许多人甚至不知道应该从哪条腿开始行进。平民群众也不清楚新兵应该从哪条腿开始行进,因此唯一重要的是有多少士兵能保持步调一致。\n\n将有 #cf_span[n] 个方阵参加阅兵,第 #cf_span[i] 个方阵包含 #cf_span[li] 名从左腿开始行进的士兵和 #cf_span[ri]...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of columns.  \nFor each column $ i \\in \\{1, \\dots, n\\} $, let $ l_i, r_i \\in \\mathbb{Z}^+ $ denote the number of soldiers starting with the le...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments