codeforces日常训练题

(1)2021-2022 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred)-D - Deletive Editing

题意:

给定两个字符串a,b,可以对a进行任意次数的删除操作:删除某个字母在a中首次出现的位置。问能否通过该操作通过a得到b?

分析:当b中的字母顺序在a中有逆序的时候,就不能,或者a中没有b中的字母,就不能。所以要解决的就是如何去设计这样一个程序。我们可以从后往前枚举a,如果当前位置的ai不等于bj那么就删除掉,并且记录已经删除了,如果在i的位置之前就已经删去了bj那么就是说在a中有逆序了,就不行。

代码:

**代码**
#include <bits/stdc++.h>
using namespace std;
int cnt[30];

void solve() {
	string a, b;
	cin >> a >> b;
	memset(cnt, 0, sizeof cnt);
	int n = a.size() - 1, m = b.size();
	for(int i = m - 1; i >= 0; i--) {
		if(cnt[b[i] - 'A']) {
			puts("NO");
			return;
		}
		while(n >= 0 && a[n] != b[i]) {
			cnt[a[n] - 'A'] = 1;
			n--;
		}
		if(n < 0) {
			puts("NO");
			return;
		} 
		n--;
	}
	puts("YES");
}
int main(){
	int t;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
} 

(2)Educational Codeforces Round 124 (Rated for Div. 2)-B. Prove Him Wrong

题意:

给定一个数n,证明是否存在这样的一个数组,长度为n并且对任意的2*|ai - aj| >= ai + aj。如果存在 输出任意符合要求的数组。

思路:根据得出的关系,假设ai > aj 则可推出公式:ai>=3aj 所以数组从小到大最小是呈3倍增长的关系即3ai=ai+1,那么1 <= ai <= 109,只需要计算3的多少次方最接近109,超过这个数就不存在。这个数为19;

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define x first
#define y second
typedef pair<int,int>PII;
const int N = 2e5 + 10;
int cnt[30];
void solve() {
   int n;
   cin >> n;
   if(n > 19) {
   	cout << "NO" << endl;
   	return;
   }
   int t = 1;
   cout << "YES" << endl;
   while(n) {
   	cout << t << ' ';
   	t *= 3;
   	n--;
   }
   cout << endl;
}

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

(3)Educational Codeforces Round 124 (Rated for Div. 2)-A - Playoff

题意:

给定一个数n,代表有2^n个球员,获胜的条件是:如果x和y对决,如果x+y是奇数,那么数字小的获胜,如果是偶数,则数字大的获胜。第一轮:[1,2]决出胜者,[3,4]决出胜者... 第二轮[1,3][5,7]...决出胜者,问给定n,最后的胜者是谁?

思路:

通过模拟发现,第一轮过后,所有的偶数都将被淘汰,剩下的全是奇数,也就是说从第二轮开始两两对决一定是数字大获胜。那么到最后一定是所有奇数最大的获胜。所以是2^n - 1。

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define x first
#define y second
typedef pair<int,int>PII;
const int N = 2e5 + 10;
int cnt[30];
void solve() {
	int n;
	cin >> n;
	cout << (1 << n) - 1 << endl;
}
 
signed main() {
	int t;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}

(4)Codeforces Round #777 (Div. 2)-C. Madoka and Childish Pranks

题意:

给定一个初始全是0的棋盘,再给定一个目标棋盘,可以进行一个操作:将一个矩阵(规模大于1)染成:横纵坐标之和为偶数 则染成0,否则染成1,并且左上角必须是0。问从初始到目标的操作数可以是多少(并没有要求最小!)。

思路:从右下到左上依次枚举。但是如果整个目标棋盘的左上角为1,则无法达到。如果纵坐标不为1,则都可以通过水平规模为21的矩阵得到,否则只能通过竖直的规模为12的矩阵得到,分别存一下前后两个点的坐标即可。最后输出。

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N][N];

struct node {
	int x1, y1, x2, y2;
}cnt[N*N];
void solve() {
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			cin >> a[i][j];
	if(a[1][1] == '1') {
		cout << -1 << endl;
		return;
	}
	int k = 0;
	for(int i = n; i; i--)
		for(int j = m; j; j--) {
			if(a[i][j] == '0') continue;
			if(j != 1) {
				cnt[k].x1 = i, cnt[k].y1 = j - 1, cnt[k].x2 = i, cnt[k].y2 = j;
				k++;
			}else {
				cnt[k].x1 = i - 1, cnt[k].y1 = j, cnt[k].x2 = i, cnt[k].y2 = j;	
				k++;
			}
		}
	cout << k << endl;
	for(int i = 0; i < k; i++) cout << cnt[i].x1 << ' ' << cnt[i].y1 << ' ' << cnt[i].x2 << ' ' << cnt[i].y2 << endl;
}
int main() {
	int t;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
} 

Codeforces Round #776 (Div. 3)-D - Twist the Permutation

Codeforces Round #773 (Div. 2)

A - Hard Way

思路:

对于一个三角形而言,如果呈倒三角形并且最上面的一条边与x轴平行,那么在这条边上的两点之间的所有点都是不安全的。计算长度即可。

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
typedef pair<int,int>PII;
const int N = 2e5 + 10;
 
void solve() {
	PII w[3];
	for(int i = 0; i < 3; i++) cin >> w[i].fi >> w[i].se;
	int ans = 0;
	for(int i = 0; i < 3; i++)
		for(int j = i + 1; j < 3; j++) {
			if(w[i].se == w[j].se && w[i].se > w[3-i-j].se) {
				ans = abs(w[i].fi - w[j].fi);
			}
		}
	cout << ans << endl;
}
signed main() {
	cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}

B - Power Walking

思路:

贪心的策略,尽量让相同的元素在一个集合。这样就尽可能的减少计算重复元素的数量。

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
typedef pair<int,int>PII;
const int N = 3e5 + 10;
int w[N];
int cnt[N];
void solve() {
	int n;
	cin >> n;
	map<int,int>mp;
	for(int i = 0; i < n; i++) {
		cin >> w[i];
		mp[w[i]]++;
	}
	int k = 0;
	for(auto x : mp) {
		cnt[k++] = x.se;
	}
	for(int i = 1; i <= n; i++) {
		if(k >= i) cout << k << ' ';
		else cout << k + i - k << ' ';
	}
	cout << endl;
}
signed main() {
	cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}

C - Great Sequence

思路:

暴力模拟即可。看清题意,x是给出的。

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
typedef pair<int,int>PII;
const int N = 3e5 + 10;
void solve() {
	int n, x;
	cin >> n >> x;
	map<int,int>mp;
	for(int i = 0, t; i < n; i++){
		cin >> t;
		mp[t]++;
	}
	int ans = 0;
	for(auto [k , v] : mp) {
		if(!mp.count(k * x)) ans += v;
		else {
			mp[k * x] -= v;
			if(mp[k * x] <= 0) {
				ans -= mp[k * x];
				mp.erase(mp.find(k * x));
			}
		}
	}
	cout << ans << endl;
}
signed main() {
	cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}
posted @   飘向远方丶  阅读(91)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示