The Forbidden Permutation

本文介绍了如何使用C++解决禁止排列的问题。首先检查是否存在满足条件的好串,如果不满足则遍历所有情况寻找最小不满足条件。关键在于判断pos[a[i]]与pos[a[i+1]]的关系,并确定d的取值范围,确保满足题目的约束条件。做题时要注意仔细阅读样例,避免误解题目要求。
#include <bits/stdc++.h>
#define int long long
using namespace std;

int n, m, d;

void solve()
{
	cin >> n >> m >> d;
	vector<int> p(n + 1);
	vector<int> pos(n + 1);
	for (int i = 1; i <= n; i ++)
	{
		cin >> p[i]; // 排列
		pos[p[i]] = i;
	}
	vector<int> a(m + 1);
	int mmin = n + 1;
	int mmax = - n - 1;
	for (int i = 1; i <= m; i ++) cin >> a[i]; // 数组
	for (int i = 1; i < m; i ++)
	{
		if (pos[a[i]] >= pos[a[i + 1]] || pos[a[i + 1]] > pos[a[i]] + d)
		{
			cout << 0 << "\n";
			return ;
		}
		mmin = min(pos[a[i + 1]] - pos[a[i]], mmin);
		mmax = max(pos[a[i + 1]] - pos[a[i]], mmax); // 距离小于d
	}
	if (d + 1 - mmax <= mmin && d <= n - 2) cout << d - mmax + 1<< "\n"; // d + 1 大, mmax 小,所以d + 1 - mmax, 注意边界条件
	else cout << mmin << "\n";
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t;
	cin >> t;
	while (t --)
	{
		solve();
	}
	return 0;
}

 先上代码。

思路:首先遍历一遍,如果有一个不满足,则是好串,直接退出,输出0

不是的话,遍历所有的情况,寻找到不满足的最小情况 ,分别是 pos[a[i]] >= pos[a[i + 1]]  和 pos[a[i]] > pos[a[i]] + d 这两种,情况。对于第二种情况,可以找到 d 的取值范围, a[i] , a[i + 1] 的距离是确定的 那 pos[a[i]] - pos[a[i + 1]] == d + 1 就是满足的最小情况,然后 pos[a[i]] 和 pos[a[i + 1]] 的最远距离是 n - 1 所以 d <= n - 2 必须满足。代码就是像上面那样。

我做这个题的时候看样例没有看全, 一直以为是 pos[a[i]] 的值 就是 p[a[i]] 的值导致怎么也理解不了,以后做题要仔细了,发现一直不懂的就多看几遍题。

链接:https://ac.nowcoder.com/acm/contest/108299/L 来源:牛客网 题目描述 It's a loving community! There are 𝑛 n residents in the community, and each resident 𝑖 ( 1 ≤ 𝑖 ≤ 𝑛 ) i (1≤i≤n) in the community has a unique resident 𝑎 𝑖 ( 1 ≤ 𝑖 ≤ 𝑛 ) a i ​ (1≤i≤n) in the community whom he/she loves so much. Each two residents love different residents. A resident can love himself / herself. It is guaranteed that 𝑛 n is even. One day, a bad thing happens: They need to choose 2 2 residents to be forbidden to get married forever. And to prevent such a thing from happening in the future, the rest 𝑛 − 2 n−2 residents decide to get married as 𝑛 2 − 1 2 n ​ −1 couples, each couple consisting of 2 2 persons (of course). It makes no sense that a couple consists of resident 𝑥 x and resident 𝑦 y while neither 𝑥 x loves 𝑦 y nor 𝑦 y loves 𝑥 x, so such a thing never happens. So, as the planner, you need to figure out how you can arrange this. You want to know the number of different marriage plans. Two marriage plans are considered different if at least one of the following conditions is satisfied: In one plan, a person 𝑖 i is married, and in the other, he/she is not. In one plan, a person 𝑖 i is married to 𝑗 j, and in the other, he/she is not married to 𝑗 j. As the number of plans can be quite enormous, output it modulo 998 244 353 998 244 353. 输入描述: Each test contains multiple test cases. The first line contains the number of test cases 𝑇 ( 1 ≤ 𝑇 ≤ 1 0 4 ) T (1≤T≤10 4 ) . Each test case consists of two lines. The first line contains 1 1 integer 𝑛 ( 4 ≤ 𝑛 ≤ 5 × 1 0 5 ) n (4≤n≤5×10 5 ), the number of residents in the community. It's guaranteed that 𝑛 n is even. The second line contains 𝑛 n integers 𝑎 1 , 𝑎 2 , … , 𝑎 𝑛 ( 1 ≤ 𝑎 𝑖 ≤ 𝑛 ) a 1 ​ ,a 2 ​ ,…,a n ​ (1≤a i ​ ≤n), where 𝑎 𝑖 a i ​ represents the one that the resident 𝑖 i loves. It is guaranteed that if 𝑖 ≠ 𝑗 ( 1 ≤ 𝑖 , 𝑗 ≤ 𝑛 ) i  ​ =j (1≤i,j≤n), 𝑎 𝑖 ≠ 𝑎 𝑗 a i ​  ​ =a j ​ . It is guaranteed that ∑ 𝑛 ∑n over all test cases in one test will not exceed 5 × 1 0 5 5×10 5 . 输出描述: For each test case, output 1 1 integer: the number of different marriage plans modulo 998 244 353 998 244 353. 示例1 输入 复制 2 4 1 3 4 2 6 3 4 5 6 2 1 输出 复制 3 9
07-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值