Codeforces Round #686 (Div. 3) 解题报告

Codeforces Round #686 (Div. 3)

A. Special Permutation

Description

让你输出 1 1 1 ~ n n n的一个排列 a a a,并满足 ∀ i ∈ [ 1 , n ] , a i ≠ i \forall i \in [1, n], a_i \neq i i[1,n],ai=i

Tutorial

如果 n n n为偶数直接倒序输出即可,否则将中间的数字与开头或结尾进行交换

Solution

#include <bits/stdc++.h>
using namespace std;
int main() {
   
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
   
		int n;
		cin >> n;
		if(n % 2 == 0)
			for (int i = n; i >= 1; --i) cout << i << " ";
		else {
   
			cout << n / 2 + 1 << " ";
			for (int i = n - 1; i >= 1; --i) {
   
				if (i == n / 2 + 1) cout << n << ' ';
				else cout << i << " ";
			}
		}
		cout << '\n';
	}
	return 0;
}

B. Unique Bid Auction

Description

给定一个长度为 n n n的序列,让你找到其中出现次数为1中最小的那个数所在的下标

Tutorial

记录一下每个数出现的次数,然后找到次数为1中最小的那个即可

Solution

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int vis[maxn], a[maxn];
int main() {
   
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
   
		int n;
		cin >> n;
		int Min = 0x3f3f3f3f;
		for (int i = 1; i <= n; ++i) vis[i] = 0;
		for (int i = 1; i <= n; ++i) {
   
			int x;
			cin >> x;
			++vis[x];
			a[i] = x;
		}
		for (int i = 1; i <= n; ++i) {
   
			if (vis[i] == 1) {
   
				Min = i;
				break;
			}
		}
		if (Min >= 0x3f3f3f3f) {
   
			cout << -1 << '\n';
			continue;
		}
		for (int i = 1; i <= n; ++i) {
   
			if (a[i] == Min) cout << i << ' ';
		}
		cout << '\n';
	}
	return 0;
}

C. Sequence Transformation

Description

给定一个长度为 n n n的序列,你需要选择一个序列中出现过的数字 x x x,并删除所有不含有 x x x的子序列,问你将序列变至只含有 x x x的最小操作次数是多少

Tutorial

显然连续的相同数字对答案没有贡献,则首先将其从数组中去除,然后对于 a i a_i ai,假设其总共出现了 m m m次,则使得 x = a i x = a_i x=ai的操作次数为
c n t i = m − ( a 1 = = a i ) − ( a n ′ = = a i ) cnt_i=m-(a_1==a_i)-(a_{n'}==a_i) cnti=m(a1==ai)(an==ai)
O ( n ) O(n) O(n)枚举即可

Solution

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], vis[maxn];
int main() {
   
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值