ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #761 (Div. 2)

2021-12-17 22:34:34  阅读:159  来源: 互联网

标签:cout int 761 Codeforces -- indexs Div include indext


A. Forbidden Subsequence

A. Forbidden Subsequence

题意:

这道题的意思是给定两个字符串s,t,其中t只由abc组成,且长度为三,我们需要对s进行排列需,使得在 t 不是 s 的子序列的同时,s 的字典序列最小。

思路分析:

我们可以先对s进行排序,然后在判断 t 是否为其子序列,如果是,把最后在 s 中出现 t 子序列,并返回该子序列的第二个字符出现的位置,然后将其往后面的挪动,如果 s 的长度小于 t 时,直接输出。

代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int Subsequence(char* s, char* t){
	int arr[3];
	int indexarr = 2;
	int indexs = strlen(s) - 1, indext = strlen(t) - 1;
	for (; indexs >= 0; indexs--){
		if (s[indexs] == t[indext]){
			arr[indexarr--] = indexs;
			indext--;
		}
		if (s[indexs] == t[indext + 1]){
			arr[indexarr + 1] = indexs;
		}
		if (indext == -1)break;
	}
	//cout << arr[0] << arr[1] << arr[2] << endl;
	if (indext == -1)return arr[1];
	return -1;
}

int main(){
	int T;
	cin >> T;
	while (T--){
		char s[1005], t[4];
		cin >> s >> t;
		int len = strlen(s);
		sort(s, s + len);
		//cout << s << endl;
		int index = Subsequence(s, t);
		while (index != -1){
			char tmp = s[index];
			for (int i = index + 1; i < len; i++){
				if (s[i] > tmp){
					s[index] = s[i];
					s[i] = tmp;
					break;
				}
			}
			//cout << s << endl;
			index = Subsequence(s, t);
		}
		cout << s << endl;

	}
	return 0;
}

B. GCD Problem

B. GCD Problem

题意:

这道题的意思是,给定一个数字n,找出三个数a,b,c,三个数的和等于n,且c = gcd(a,b)。

思路分析:

我们可以发现,总存在c = 1,能够满足给定条件下的答案,同时我们还需推测出,当n % 2 == 0 时,a = n - 3,b = n - 2,c = 1。
当n % 4 == 1时,a = n / 2 - 1,b = n / 2 + 1,c = 1。
当n % 4 == 3时,a = n / 2 - 2,b = n / 2 + 2,c = 1。

代码如下:

#include <bits/stdc++.h>

using namespace std;

void solve(){
	int n;
	cin >> n;
	if(n % 2 == 0){
		cout << "2 " << n-1-2 << " 1\n";
	}
	else{
		int cnt = (n - 1) / 2;
		if(cnt % 2 == 0){
			cout << cnt - 1 << " " << cnt + 1 << " " << "1\n";
		}
		else{
			cout << cnt - 2 << " " << cnt + 2 << " " << "1\n";
		}
	}
}

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

标签:cout,int,761,Codeforces,--,indexs,Div,include,indext
来源: https://blog.csdn.net/qq_53060585/article/details/122005973

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有