专栏首页算法修养CodeForces 733B Parade

CodeForces 733B Parade

B. Parade time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output 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 inputCopy 3 5 6 8 9 10 3 outputCopy 3 inputCopy 2 6 5 5 6 outputCopy 1 inputCopy 6 5 9 1 3 4 8 4 5 23 54 12 32 outputCopy 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.

c++

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>

using namespace std;
const int maxn=1e5;
int n;
int a[maxn+5][2];
int main()
{
    scanf("%d",&n);
    int L=0,R=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i][0],&a[i][1]);
        L+=a[i][0];R+=a[i][1];
    }
    int ans=abs(L-R);
    int l=L,r=R;
    int res=0;
    for(int i=1;i<=n;i++)
    {
        l+=(a[i][1]-a[i][0]);
        r+=(a[i][0]-a[i][1]);
        if(abs(l-r)>ans)
        {
            res=i;
            ans=abs(l-r);
        }
        l=L;
        r=R;
    }
    printf("%d\n",res);
    return 0;

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • CodeForces 157A Game Outcome

    A. Game Outcome time limit per test 2 seconds memory limit per test 256 me...

    ShenduCC
  • ZOJ 3713 In 7-bit

    Very often, especially in programming contests, we treat a sequence of non-white...

    ShenduCC
  • POJ-1276-Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions:...

    ShenduCC
  • ACM-ICPC 2019 山东省省赛 M Sekiro

    Sekiro: Shadows Die Twice is an action-adventure video game developed by FromSof...

    风骨散人Chiam
  • CornerNet: 成对关键点物体检测 | CSDN博文精选

    5、用于Grouping Corners的 embedding vector的工作原理

    AI科技大本营
  • spark 2.3 导致driver OOM的一个SparkPlanGraphWrapper源码的bug

    长话短说,我们部门一个同事找到我,说他的spark 2.3 structured streaming程序频繁报OOM,从来没有坚持过超过三四天的,叫帮看一下。 ...

    老白
  • dokuwiki配置

    首先,看这里。 http://www.mamicode.com/info-detail-648674.html

    超级大猪
  • ASP.NET的最新安全漏洞Important: ASP.NET Security Vulnerability

    微软在9月17号中午正式对外公布了ASP.NET平台下的安全漏洞,即Microsoft Security Advisory (2416728)。

    javascript.shop
  • 【Gym 100733D】Little thief Shi(取数,DP)

    Shi realized that he was almost out of money, even renting Shitalian lands. Shi ...

    饶文津
  • 3D人体姿态估计

    Coarse-to-Fine Volumetric Prediction for Single-Image 3D Human Pose

    用户1148525

扫码关注云+社区

领取腾讯云代金券