zoukankan      html  css  js  c++  java
  • Codeforces 900C. Remove Extra One(暴力)

    You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.

    We remind that in a sequence of numbers a1, a2, ..., ak the element ai is a record if for every integer j (1 ≤ j < i) the following holds: aj < ai.

    Input

    The first line contains the only integer n (1 ≤ n ≤ 105) — the length of the permutation.

    The second line contains n integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation. All the integers are distinct.

    Output

    Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.

    Examples
    input
    1
    1
    output
    1
    input
    5
    5 1 2 3 4
    output
    5
    Note

    In the first example the only element can be removed.

    题意:

    如果一个数比前面的数都大,那么就产生一个贡献

    现在要你去掉一个数,使得剩下数字串总贡献最大,求这个数

    如果几个数字一样,输出较大的

    题解:

    这题似乎可以用树状数组手糊,但标算更加高妙

    因为最大值可以删去,所以我们关心的不仅只有最大值,还有次大的

    一组数字,没有修改的话原本的贡献是相同的,所以问题就变成了去掉一个数最多能增加多少贡献

    这该怎么记录呢?

    如果有一个数比当前最大数大,那么去掉它会产生负贡献

    如果比最大值大,比次大值小,那么去掉最大值会增加一个贡献

    所以建一个cnt数组,cnt[i]表示去掉其所增加的贡献

    扫一遍取最大值即可

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int n,a[100010],cnt[100010];
    
    int main()
    {
        int max1=0,max2=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]); 
        }
        for(int i=1;i<=n;i++)
        {
            if(a[i]>max1)
            {
                max2=max1;
                max1=a[i];
                cnt[a[i]]--;
            }
            else
            {
                if(a[i]>max2)
                {
                    cnt[max1]++;
                    max2=a[i];
                }
            }
        }
        int ans,max3=-100000;
        for(int i=1;i<=n;i++)
        {
            if(cnt[i]>max3)
            {
                max3=cnt[i];
                ans=i;
            }
        }
        printf("%d
    ",ans);
    } 

     

  • 相关阅读:
    POJ 2175 Evacuation Plan 费用流 负圈定理
    POJ 2983 Is the Information Reliable? 差分约束
    codeforces 420B Online Meeting
    POJ 3181 Dollar Dayz DP
    POJ Ant Counting DP
    POJ 1742 Coins DP 01背包
    中国儒学史
    产品思维30讲
    Java多线程编程核心技术
    编写高质量代码:改善Java程序的151个建议
  • 原文地址:https://www.cnblogs.com/stxy-ferryman/p/8279154.html
Copyright © 2011-2022 走看看