Codeforces Round 903 (Div. 3)

在这里插入图片描述

Dashboard - Codeforces Round 903 (Div. 3) - Codeforces

A. Don’t Try to Count

题意:给出a,b两个字符串,可以对a进行操作,每次将a复制一份加到尾部,最少需要多少次操作可以让b为a的子串,若不可能输出-1

思路:暴力操作a字符串,将a字符串尽可能的扩大,每次操作后判断其中是否存在b的子串,一定注意a的范围,盲目操作会炸。

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		cin >> n >> m;
		string a, b; cin >> a >> b;
		int flag = 0, i = 0;
		while(a.size() < 10000){
			if(a.find(b) != a.npos) {
				flag = 1;
				cout << i << endl;
				break;
			}
			a += a;
			i ++;
		}
		
		if(!flag) cout << "-1" << endl;
	}
	return 0;
}

B. Three Threadlets

题意:三根线,能剪三刀,剪完后每根线需要是整数,是否可以剪成每段长度相等的

思路:先排序,枚举几种情况

  • a1==a3:三根线本来就一样长,YES
  • a1==a2:只能对最长的操作,切13刀,分成24份,看分完后是否与前面相等
  • a2==a3:只能两根线一根一刀
  • 三根线都不相等:a3两刀,a2一刀

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
int a[4];
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		for(int i = 1; i <= 3; i ++) cin >> a[i];
		sort(a + 1, a + 3 + 1);
		if(a[1] == a[3]) cout << "YES" << endl;
		else if(a[1] == a[2]){
			if(a[3] % 3 == 0 && a[3] / 3 == a[2]) cout << "YES" << endl;
			else if(a[3] % 4 == 0 && a[3] / 4 == a[2]) cout << "YES" << endl;
			else if(a[3] % 2 == 0 && a[3] / 2 == a[2]) cout << "YES" << endl;
			else cout << "NO" << endl;
		}
		else if(a[2] == a[3]){
			if(a[3] % 2 == 0 && a[3] / 2 == a[1]) cout << "YES" << endl;
			else cout << "NO" << endl;
		}
		else{
			if(a[3] % 3 == 0 && a[2] % 2 == 0 && a[3] / 3 == a[2] / 2 && a[3] / 3 == a[1])
			cout << "YES" << endl;
			else 
			cout << "NO" << endl;
		}
	}
	return 0;
}

C. Perfect Square

题意:给定正方形矩阵字符串,顺时针旋转90度,一次操作可令一个位置的字母字典序+1,若令矩阵旋转前后相等,

最少操作多少次

思路:每个字符会到达四个位置,取四个位置的字母最大字典序,因为只能增加字典序不能减小

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
string s;
char a[M][M];
 
int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
 
int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		cin >> n;
		for(int i = 1; i <= n; i ++){
			cin >> s;
			for(int j = 0; j < n; j ++){
				a[i][j + 1] = s[j]; 
			}
		}
		int ans = 0;
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= n; j ++){
				ans += (max({a[i][j], a[j][n - i + 1], a[n - i + 1][n - j + 1], a[n - j + 1][i]}) - a[i][j]);
			}	
		}
		cout << ans << endl;
	}
	return 0;
}

D. Divide and Equalize

题意:给出一个数组a,可进行一种操作,若ai % x == 0,则ai /= x,ai+1 *= x,是否可以讲数组中所有元素相等

思路:将每个元素分解质因数,map记录,看每一个质因数的个数是否为n的倍数

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
int a[N];

int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}

int main(){
	fast();

	cin >> T;
	while(T --){
		cin >> n;
		for(int i = 0; i < n; i ++)
			cin >> a[i];
		map<int, int> mp;
		for(int i = 0; i < n; i ++){
			int cnt = a[i];
			for(int j = 2; j <= cnt / j; j ++){
				while(cnt % j == 0){
					mp[j] ++;
					cnt /= j;
				}
			}
			if(cnt > 1) mp[cnt] ++;
		}
		bool flag = true;
		for(auto i: mp){
			if(i.second % n != 0){
				flag = false;
				break;
			}
		}
		if(flag) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}

E. Block Sequence

题意:定义一种完美序列,由一系列块组成,每一块的第一个元素为块内的个数,紧跟的元素数量为这个个数,可以删除任意元素,最少需要多少次操作。

思路:dp,从后往前判断从i到n需要达到完美序列的操作次数,不断向前,若当前点不足以达成完美序列,则当前点一定需要删除,否则在当前点可以形成符合条件的块的情况下,取以当前块的操作次数为后面紧邻的块的操作次数与删除当前点需要的操作次数的最小值

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;

int gcd(int a, int b){
	if(b) while((a %= b) && (b %= a));
	return a + b;
}

int main(){
	fast();
	
	T = 1;
	cin >> T;
	while(T --){
		int a[N], dp[N];
	   cin >> n;
	   for(int i = 0; i < n + 5; i ++)
	   	dp[i] = 0;
	   for(int i = 1; i <= n; i ++)
	   	cin >> a[i];

	   dp[n + 1] = 0;
	   for(int i = n; i > 0; i --){
			dp[i] = dp[i + 1] + 1;
			if(a[i] <= n - i){
				dp[i] = min(dp[i], dp[i + a[i] + 1]);
			}
	   }
	   cout << dp[1] << endl;
//	   for(int i = n; i > 0; i --)
//	   cout << dp[i] << " ";
//	   cout << "+++++" << endl;
	}
	return 0;
}

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| ^_^ |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑AC,永无bug缠身       
*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值