Codeforces Round 915 (Div. 2) D
Cyclic MEX
题面翻译
对于一个长为 的排列 ,定义其权值为 ,也就是 中没有出现过的最小自然数的和。
然后你可以对这个排列进行移位操作,问最大权值。
题目描述
For an array , define its cost as .
You are given a permutation of the set . Find the maximum cost across all cyclic shifts of .
is the smallest non-negative integer such that does not occur among .
A permutation of the set is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
输入格式
Each test consists of multiple test cases. The first line contains a single integer ( ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer ( ) — the length of the permutation .
The second line of each test case contain distinct integers ( ) — the elements of the permutation .
It is guaranteed that sum of over all test cases does not exceed .
输出格式
For each test case, output a single integer — the maximum cost across all cyclic shifts of .
样例 #1
样例输入 #1
4
6
5 4 3 2 1 0
3
2 1 0
8
2 3 6 7 0 1 4 5
1
0
样例输出 #1
15
5
31
1
提示
In the first test case, the cyclic shift that yields the maximum cost is with cost .
In the second test case, the cyclic shift that yields the maximum cost is with cost .
一个很久的题目了,最近在补之前的DE,所以被我翻出来了。
思路就是统计变化产生的代价,只要尝试去思考每一次循环带来的代价的变化就可以写出来了。
考虑在一个队列里面放上这个数组当前位置提供的价值,然后我们每次循环就是把队首弹出,然后给队尾压入一个n,同时把前所有价值大于之前的数字都变成a[1] 。然后这个状态的答案就是队列里面所有数字相加。
感性理解一下,这个把前面大于a[1]的数字都变成的操作数量不会太多。
可以尝试构造一个很多的情况,然后就会发现出不来。
假如我们有一个数y更新了后面的x个数字,那能够更新的比x还多的自然是比y小的数字,并且还要在原本的数组里面在y的后面,也就是逆序对的情况。但是其实可以发现,逆序对是会导致数组总体的贡献降低的(感性理解),而贡献越小就意味着我们需要更新的数字越少(因为我们的更新是把打的变小),所以这个其实是矛盾的,也就是很难,甚至是无法构造出一个能够逆序对多并且贡献大的情况,也就很难有更新数量多的情况。
超级超级不严谨的证明,都不能叫证明,只能说是通过一些方式把我为什么会有,或者说我应该怎么才能有这种感觉说出来了。
所以直接暴力就好。
不是暴力哦,这样是错误的,要加一个优化,就是要数字相同压缩为一个位置。这样就很明显没问题了。不然会t的。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read() {
char c=getchar();ll a=0,b=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')b=-1;
for(;c>='0'&&c<='9';c=getchar())a=a*10+c-48;return a*b;
}
ll n,a[1000001],que[3000001][2],tail,head,flag[1000001];
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ll T=read();
while(T--)
{
n=read();
for(ll i=1;i<=n;i++)
{
a[i]=read();
flag[i]=0;
}
flag[0]=0;ll now=0;
tail=head=0;
ll ans=0;
for(ll i=1;i<=n;i++)
{
flag[a[i]]=1;
while(flag[now]==1)now++;
que[++tail][0]=now;que[tail][1]=1;
ans+=now;
}
ll Max=ans;
for(ll i=1;i<=n;i++)
{
ans-=que[++head][0];
if(que[head][1]!=1)
{
head--;
que[head+1][1]--;
}
ans+=n;
int ned=0;
while(que[tail][0]>a[i]&&tail>head)
{
ans-=(que[tail][0]*que[tail][1]-a[i]*que[tail][1]);
// que[now]=a[i];
ned+=que[tail][1];
tail--;
}
que[++tail][0]=a[i];
que[tail][1]=ned;
que[++tail][0]=n;
que[tail][1]=1;
Max=max(Max,ans);
}
cout<<Max<<endl;
}
return 0;
}
/*
1
8
0 4 6 2 7 3 1 5
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】