Codeforces Round #656 (Div. 3)题解

这篇博客详细解析了Codeforces Round #656 (Div. 3)的比赛题目,包括A题Three Pairwise Maximums、B题Restore the Permutation by Merger、C题Make It Good和D题a-Good String的解题思路和代码实现。对于A题,关键在于判断是否存在两个相同的最大值。B题通过遍历和集合判断元素的唯一性。C题需寻找最长满足特定条件的子序列。D题采用暴力枚举求解。遗憾的是,E题和剩余两题因难度未能完成解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces Round #656 (Div. 3)题解

A.Three Pairwise Maximums
解题思路:
依照题意和样例,三个整数x,y,z必须有两个相同且都比第三个数大。
如果x==y则说明a为最大值,此时需要满足a>=z,如果不满足该条件,则无解,因为z=max(b,c),我们不能确定b,c谁比较大,所以我们就假设两个数一样的即可。而当y == z和x == z同理。
代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
int main()
{
int t;
	cin >> t;
	while(t--){
		int a = 0,b = 0,c = 0,x,y,z;
		cin >> x >> y >> z;
		if(x == y) a = x;
		else if(y == z) c = y;
		else if(x == z) b = x;
		if((a && a >= z) || (b && b >= y) || (c && c >= x)){
			cout << "YES" << endl;
			if(a != 0) cout << a << " " << z << " " << z << endl;
			else if(b != 0) cout << b << " " << y << " " << y << endl;
			else if(c != 0) cout << c << " " << x << " " << x << endl;
		}
		else
			cout<<"NO"<<endl;
	}
    return 0;
}

B.Restore the Permutation by Merger
解题思路:
题目说是相对顺序插入的,所以我们可以直接遍历来做。
排列中不能有重复元素,可以把输入的元素存入set。然后判断序列中有没重复元素
代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
int main()
{
	int t,a[55] = {0};
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		set<int> s;
		for(int i = 1;i <= 2 * n;i++)
			cin >> a[i];
		for(int i = 1;i <= 2 * n;i++){
			if(!s.count(a[i])){
				cout << a[i] << " ";
				s.insert(a[i]);
			}
			if(s.size() == n) break;
		}
		cout << endl;
	}
	return 0;
}

C.Make It Good
解题思路:
满足中间大两边小。
要找最长“好序列”,从后往前找,找到第一个不满足从后往前递增条件的元素,然后从这个元素往前找,找到开始升的那个元素就停止。最后答案就是n减去最长“好序列”长度。
代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
int a[200005];
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for(int i = 1;i <= n;i++) 
			cin>>a[i];
        int R = n;
        while(a[R - 1] >= a[R] && R >= 1) 
			R--;
        while(a[R - 1] <= a[R] && R >= 1) 
			R--;
        int ans = R - 1;
        if(ans == -1) ans = 0;
        cout << ans << endl;
    }
    return 0;
}

D. a-Good String
解题思路:
暴力枚举,取最小值。
代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
char s[200005];
int solve(int l,int r,char m)
{
    if(l == r)
    {
        if(s[l] == m) return 0;
        else return 1;
    }
    int mid = (l + r) >> 1,left = 0,right = 0,len = r - l + 1;
    for(int i = l;i <= mid;i++) if(s[i] == m) left++;
    for(int i = mid + 1;i <= r;i++) if(s[i] == m) right++;
    int ans = min(len / 2 - left + solve(mid + 1,r,m + 1),len / 2 - right + solve(l,mid,m + 1));
    return ans;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        char m = 'a';
        cin >> n;
        for(int i = 1;i <= n;i++) cin >> s[i];
        int ans = solve(1,n,m);
        cout << ans << endl;
    }
    return 0;
}

E. Directing Edges
解题思路:
首先可以看出这是一道拓扑的题。但由于不太好维护得建图。建反方向的边,最后再拓扑排。但由于我太菜了,就没做出来。代码就不放了。
剩下两道题也没来得及做。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值