Codeforces Round 848 (Div. 2)C

B. The Forbidden Permutation

一定要注意题目中说的是对于all i满足才算不好的,我们做的时候只要破坏一个i这个a就不算好的了,被这一点坑了,没注意到all。

#include <bits/stdc++.h>
 
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int p[N], a[N]; 

void run()
{	
	memset(p, 0, sizeof p);
	int n, m, d;	scanf("%d%d%d", &n, &m, &d);
	for(int i = 1; i <= n; ++ i)
	{
		int x;	scanf("%d", &x);
		p[x] = i;
	}
	for(int i = 1; i <= m; ++ i)	scanf("%d", &a[i]);
	int ans = 1e9;
	for(int i = 1; i + 1 <= m; ++ i)
	{
		if(p[a[i + 1]] < p[a[i]] || p[a[i + 1]] > p[a[i]] + d)
		{
			cout << 0 << endl;
			return;
		}
		if(p[a[i + 1]] > p[a[i]])	ans = min(ans, p[a[i + 1]] - p[a[i]]);
		if(p[a[i + 1]] <= p[a[i]] + d)
			if(d + 2 <= n)	ans = min(ans, p[a[i]] + d + 1 - p[a[i + 1]]);
	}
	printf("%d\n", ans);
}
 
int main()
{
//	freopen("1.in", "r", stdin);
	int t;cin >> t;
	while(t --)	run();	
	return 0;  
}
posted @   cxy8  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
点击右上角即可分享
微信分享提示