C. Shinju and the Lost Permutation(思维+构造)

Codeforces Round #779 (Div. 2)

Problem

在这里插入图片描述

Input

6
1
1
2
1 2
2
2 2
6
1 2 4 6 3 5
6
2 3 1 2 3 4
3
3 2 1

Output

YES
YES
NO
NO
YES
NO

Solution

  1. 首先观察可以发现,当且仅当 c 数组中 1 出现的次数为 1时,才可能有解,否则一定无解
  2. We can rotate the array such that c1=1 (initial state) because we don’t have to construct the permutation, so if there exists a permutation with p1=n, the answer is YES.
    这是codeforces英文题解中的解释,下面我进一步解释:
    • c数组中1的含义:表示此次cyclic shift之后,p数组中第一个数 p1 = n

    • 如果排列的首位是 n, 则一定存在一种排列满足 c 数组,也就是一定有解(充分条件,因为我们只需要判断是否有解即可,无需构造出对应的排列)

    • 为什么首位是n的情况下一定有解,以下是构造方法:

      One way to build a satisfying permutation: After rotating the array c[], write the numbers 1 to n one-by-one from the position(s) with highest c[i] to lowest c[i]. If there exists multiple positions with the same value, go from the smallest index to the largest.
      (我也看不明这个段英文说了个什么。。结合着下面的例子看这段英文就好理解了)
      哈哈哈

    • This is the sufficient condition(充分条件). It can be shown that, if ci+1 − ci ≤ 1 for all 1 ≤ i < n, then there exists a permutation that satisfies.

Code

#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define all(a) a.begin(), a.end() 

void solve()
{
	int n; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	auto x = find(all(a), 1);
	if (x == a.end())
	{
		cout << "NO\n";
		return;
	}

	rotate(a.begin(), x, a.end());
	for (int i = 1; i < n; i++)
		if (a[i] - a[i - 1] > 1 || a[i] == 1)//翻转之后,首位是1,若后面又出现1,则直接return掉即可。
		{
			cout << "NO\n";
			return;
		}

	cout << "YES\n";
}

int main()
{
	IOS;
	int T; cin >> T;
	while (T--)
	{
		solve();
	}

	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

to cling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值