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.
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.
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.
1
1
1
5
5 1 2 3 4
5
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\n",ans); }