Codeforces 900C - Remove Extra One(思维好题)

Remove Extra One
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given a permutation pp 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,...,aka1, a2, ..., ak the element aiai is a record if for every integer j(1j<i)j(1 ≤ j < i) the following holds: aj<aiaj < ai.

input
The first line contains the only integer n(1n105)n(1 ≤ n ≤ 105) — the length of the permutation.
The second line contains nn integers p1,p2,...,pn (1pin)(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.

解题思路

先给一波官方题解:

In this problem you have to find an element after which removal the number of records is maximum possible.
Let ri be an array consisting of 0 and 1 depending on whether the ithi−th element was a record initially or not. We can compute it easily in O(N)O(N).
Let xixi be the difference between the number of records after removal the i-th element and initial number of records.
Let’s think of how does removal of ai influence the array riri. First of all, riri becomes 0. rj(j<i)rj(j < i) do not change in this case. Some of rj(j>i)rj(j > i), change from 0 to 1. These elements are not records initially, but become records after the removal. These elements are elements which have only one greater element in front of them — aiai.
Here follows an O(n2)O(n2) solution. Let’s fix aiai — the element we are going to remove. Let xi=rixi= − ri  +  the number of such jj that j>i, ai>ajai > aj, and for all k(ki,k<j)k(k ≠ i, k < j) ak<aj.ak < aj. We can compute this just looping through all jj and keeping the maximum over all elements but the ith.
Now note that it’s not required to fix aiai. rjrj can become 1 from 0 only when a certain element from the left is removed. Let’s loop through all ajaj and determine if there is an element to the left such that it is greater than ajaj, but all other elements are less than ajaj. We can check this using ordered set. If there is such a aiai, then increase xixi by 1. After the loop the array xixi is fully computed, so we can just find the element which brings the maximum number of records, and minimum among such.

一个元素比它前面的所有元素都大,称此元素为recordrecord

首先用一个标记数组r[i]r[i]表示第i个元素是(1)否(0)是recordrecord,在O(N)O(N)时间内可以求出。

然后考虑移除一个元素ii后,能影响哪些record

自身。还有:在他之后的,某些原本不是recordrecord的,因为ii的移除,成为record的。

这些recordrecord(称为jj)有这样的特点:比从1到j1ii外的元素都大,但不比i大。

记从1到j1j−1ii外的元素的最大值为k,则可表示为k<j<=ik<j<=i。此不等式为本题“题眼”。

这样就有了官方的O(N2)O(N2)解法:枚举要移除的元素ii->检查i及在他之后的jj,计算record增量->得移除后能使recordrecord最大的元素。

然而BB半天,1e51e5数据,显然超时。

这时候想想“题眼”,那个神奇的不等式。

同样设(删除jj后的)增量数组x[j]O(N)O(N)顺推并维护kki不就可以了?

如果j>ij>i,说明jjrecordx[j]x[j]−−,并更新jjikki

如果k<j=<ik<j=<i,“题眼”,x[i]++x[i]++(注意是ii),并更新kjj

最后的问题,如果存在相同的元素,映射不唯一,一首凉凉?

回头审题,distinct,计划通,代码非常简单。

小坑点,第一个元素是不算recordrecord的。

AC代码

#include<bits/stdc++.h>
using namespace std;

int a[100050];

int main()
{
    int n;
    while(cin>>n){
        memset(a,0,sizeof(a));
        int x,m2nd=0,m1st=0;
        for(int i=1;i<=n;i++){
            cin>>x;
            if(x>m1st){
                m2nd=m1st;
                m1st=x;
                a[x]--;
            }
            else if(x>m2nd){
                m2nd=x;
                a[m1st]++;
            }
        }
        int ma=-0x3f3f3f3f,mi;
        for(int i=1;i<=n;i++)
            if(a[i]>ma)
                ma=a[i],mi=i;
        printf("%d\n",mi);
    }
    return 0;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值