首页 > 其他分享 >Codeforces Round 915 (Div. 2) D

Codeforces Round 915 (Div. 2) D

时间:2024-03-27 13:22:27浏览次数:18  
标签:le Codeforces test tail cost que permutation 915 Div

Cyclic MEX

题面翻译

对于一个长为 \(n\) 的排列 \(p\),定义其权值为 \(\sum_{i=1}^n \operatorname{mex}_{j=1}^ip_j\),也就是 \(p_1\sim p_i\) 中没有出现过的最小自然数的和。

然后你可以对这个排列进行移位操作,问最大权值。

题目描述

For an array $ a $ , define its cost as $ \sum_{i=1}^{n} \operatorname{mex} ^\dagger ([a_1,a_2,\ldots,a_i]) $ .

You are given a permutation $ ^\ddagger $ $ p $ of the set $ {0,1,2,\ldots,n-1} $ . Find the maximum cost across all cyclic shifts of $ p $ .

$ ^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m]) $ is the smallest non-negative integer $ x $ such that $ x $ does not occur among $ b_1,b_2,\ldots,b_m $ .

$ ^\ddagger $ A permutation of the set $ {0,1,2,...,n-1} $ is an array consisting of $ n $ distinct integers from $ 0 $ to $ n-1 $ in arbitrary order. For example, $ [1,2,0,4,3] $ is a permutation, but $ [0,1,1] $ is not a permutation ( $ 1 $ appears twice in the array), and $ [0,2,3] $ is also not a permutation ( $ n=3 $ but there is $ 3 $ in the array).

输入格式

Each test consists of multiple test cases. The first line contains a single integer $ t $ ( $ 1 \le t \le 10^5 $ ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 10^6 $ ) — the length of the permutation $ p $ .

The second line of each test case contain $ n $ distinct integers $ p_1, p_2, \ldots, p_n $ ( $ 0 \le p_i < n $ ) — the elements of the permutation $ p $ .

It is guaranteed that sum of $ n $ over all test cases does not exceed $ 10^6 $ .

输出格式

For each test case, output a single integer — the maximum cost across all cyclic shifts of $ p $ .

样例 #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 $ [2,1,0,5,4,3] $ with cost $ 0+0+3+3+3+6=15 $ .

In the second test case, the cyclic shift that yields the maximum cost is $ [0,2,1] $ with cost $ 1+1+3=5 $ .

洛谷 CF

一个很久的题目了,最近在补之前的DE,所以被我翻出来了。
思路就是统计变化产生的代价,只要尝试去思考每一次循环带来的代价的变化就可以写出来了。

考虑在一个队列里面放上这个数组当前位置提供的价值,然后我们每次循环就是把队首弹出,然后给队尾压入一个n,同时把前所有价值大于之前\(a[1]\)的数字都变成a[1] 。然后这个状态的答案就是队列里面所有数字相加。
感性理解一下,这个把前面大于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
*/

标签:le,Codeforces,test,tail,cost,que,permutation,915,Div
From: https://www.cnblogs.com/HLZZPawa/p/18098764

相关文章

  • CodeForces 1936E Yet Yet Another Permutation Problem
    洛谷传送门CF传送门首先设\(a_i=\max\limits_{j=1}^ip_j\),\(b_i=\max\limits_{j=1}^iq_j\)。直接容斥,钦定有多少值不同的\(a_i\)使得\(a_i=b_i\)。然后再把钦定的每种值转化成每种值第一次使得\(a_i=b_i\)的位置\(i\)。也就是说我们现在要钦定一些位置,......
  • Div4 VP总结
    CodeforcesRound799(Div.4)E(最长子区间)基本思路求满足s的最长子区间。错误思路分析想用双指针左右贪心模拟题目要求删前或后的数(但在面对前后两个相等的时候,删前删后没有无后效性)简单暴力枚举子区间长度(显然在n=1e5的时候t了)正确思路虽然也是暴力枚举子区间,但有做......
  • css实现弹出的div显示在屏幕中间
    主要代码如下:.info{width:90vw;height:102vw;display:block;position:fixed;z-index:999;top:50%;left:50%;transform:translate(-50%,-50%);border-radius:14px;}.info-header{......
  • Windows Packet Divert(WinDivert)是一个适用于Windows 10、Windows 11和Windows Server
    WindowsPacketDivert(WinDivert)是一个适用于Windows10、Windows11和WindowsServer的用户模式数据包捕获和重定向工具。WinDivert允许用户模式应用程序捕获/修改/丢弃发送到/从Windows网络堆栈的网络数据包。总之,WinDivert可以:捕获网络数据包过滤/丢弃网络数据包嗅探......
  • Codeforces Round 936 (Div. 2) E
    SofiaandStrings题面翻译\(t\)组数据。每一次测试,有长度为\(n\)的序列\(s\),长度为\(m\)的序列\(t\)。你可以对\(s\)进行两种操作:删除\(s_i,1\lei\le|s|\)(\(s\)从\(1\)开始标号).将\(s_l,s_{l+1},\dots,s_r\)排序(\(1\lel\ler\le|s|\))。上面\(|s|......
  • CF1935 Codeforces Round 932 (Div. 2)
    C.MessengerinMAC给两个数组a,b和一个整数L.寻找一个关于a,b下标的序列p,使得\(\suma_{p_i}+\sum|b_{p_i}-b_{p_{i+1}}|\leqL\)SolutionKey1:按照b从小到大排序一定是最优的.Key2:固定\(b_l\),\(b_r\),那么\(\sum^r_l|b_{p_i}-b_{p_{i+1}}|=b_r-b_l......
  • codeforces div3 935
    Problem-E-Codeforces题解:一道二分糖题首先我们先在原序列跑一遍题目给的二分,然后跑出最后的l点我们稍微思考一下,这个l这个点一定小于或者等于x为什么呢一个在这个二分里,如果最后的点是大于x的那么必定被r拿走,因为判断上l只能接收比x小的地点所以我们知道l以后,要么就是l=......
  • cfEduRound163div2--D题解
    D-TandemRepeats?题意:做法:因为字符串长度较少,可以考虑枚举。or--动态规划voidsolve(){//D枚举//枚举!!!!!!!!!!stringstr;cin>>str;intn=str.size(),ans=0;for(inti=1;i<=n/2;i++){//枚举一半!!!intcnt=0;for(intj=0;......
  • SMU Winter 2024 div2 ptlks的周报Week 6(3.18-3.24)
    不难想到,要求环的期望,只需求出所有可能的环的长度总和和不相邻点对的组数。而边数确定,则只需求环的总长。对于两个不相邻的点x,y,所形成的环的长度等于两点深度之差加一,\(\vertdp[x]-dp[y]\vert+1\),不妨令x为根节点,则只需求所有节点的深度之和,再减去相邻的点,最后对树进行换根dp,输出......
  • codeforces div_2 936 题解报告
    codeforcesdiv_2936题解报告比赛链接:https://codeforces.com/contest/1946A.MedianofanArray做法tag:签到题目翻译给定一个长度为\(n\)的数组\(a\),记数组中非降序排列中第\({\lceil\fracn2\rceil}\)个数是数组a的中位数。我们可以以下操作。选择一个数\(i\in[......