[关闭]
@xunuo 2017-01-18T21:25:55.000000Z 字数 2746 阅读 971

Parade


 time limit per test 1 second  memory limit per test 256 megabytes

暴力

来源:codeforces 733 B Parade


Description

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 li soldiers, who start to march from left leg, and ri 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 li and ri.

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 li and ri (1 ≤ li, ri ≤ 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.

Examples

input
3
5 6
8 9
10 3
output
3

input
2
6 5
5 6
output
1

input
6
5 9
1 3
4 8
4 5
23 54
12 32
output
0

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.

题意:
分为l和r两组,交换其中一组让两组的差的绝对值最大,输出交换的是第几组。

解题思路:

直接暴力枚举!!!

完整代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int l[100010],r[100010];
  7. int main()
  8. {
  9. int n;
  10. while(scanf("%d",&n)!=EOF)
  11. {
  12. int suml,sumr;
  13. suml=sumr=0;
  14. for(int i=0;i<n;i++)
  15. {
  16. scanf("%d%d",&l[i],&r[i]);
  17. suml=suml+l[i];
  18. sumr=sumr+r[i];
  19. }
  20. int num=0,s;
  21. s=abs(suml-sumr);//abs(求绝对值)
  22. int j=0,sumli,sumri;
  23. for(int i=0;i<n;i++)
  24. {
  25. sumli=suml-l[i]+r[i];//交换l[i]和r[i];
  26. sumri=sumr-r[i]+l[i];
  27. int si=abs(sumli-sumri);
  28. if(si>s)
  29. {
  30. s=si;
  31. num=i+1;
  32. }
  33. }
  34. printf("%d\n",num);
  35. }
  36. return 0;
  37. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注