CodeForces Round 925 Div.3 A-F 题解

2 篇文章 0 订阅

A

题目

此题尽量让后面的更大,前面的更小。

我们尽量让第 3 3 3 位更大,如果 3 3 3 位取到 z还有 2 2 2 及以上时,就往第 2 2 2 位取,第二位取满后,就取第 1 1 1 位。分情况讨论,很容易写出代码。

AC Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
int q, t;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> q;
	while (q --) {
		cin >> t;
		if (t < 28) {
			cout << "aa" << char(t - 2 + 'a' - 1);
			cout << '\n';
		}
		else if (t < 53){
			cout << "a";
			t -= 27;
			cout << char(t + 'a' - 1) << 'z';
			cout << '\n';
		}
		else {
			t -= 52;
			cout << char(t + 'a' - 1) << "zz";
			cout << '\n';
		}
	}
	
	return 0;
}

B

题目

我们用一个变量记录能给后面倒的水的单位数。我们从左往右遍历:

  • 如果当前遍历到的水量大于平均水量,就把多的添加到变量里。
  • 如果当前便利的水量小于平均水量,且变量里面单位数能够把当前遍历到的水量添加到平均值,就添加,变量值减小当前水量差平均值的部分。否则,就表明无解,退出。

如果遍历完都没有退出,就表明有解,输出答案,走人。

AC Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
int t;
int n, a[200100];
long long sum;
void Main() {
	cin >> n;
	sum = 0;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i];
		sum += a[i];
	}
	sum /= n;
	long long sum1 = 0;
	for (int i = 1; i <= n; i ++) {
		if (a[i] > sum) {
			sum1 += a[i] - sum;
		}
		else {
			if (sum1 >= (sum - a[i])) {
				sum1 -= (sum - a[i]);
			}
			else {
				cout << "NO" << '\n';
				return ;
			}
		}
	}
	cout << "YES" << '\n';
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t --) {
		Main();
	}
	return 0;
}

C

题目

我们选择每一种填充颜色,因为要覆盖整个序列,所以只有前缀和后缀才关系到我们的代价,对于当前数,一直找,知道找到数组前缀和后缀里的所有元素都和当前数相等,剩下的就是我们要覆盖的。对于每一个找过的元素,记录下来,不再讨论。

AC Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
int q, n, a[200100];
bool vis[200100];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> q;
	while (q --) {
		cin >> n;
		vector<int> v; map<int, int> m;
		for (int i = 1; i <= n; i ++) cin >> a[i];
		for (int i = 1; i <= n; i ++) {
			v.push_back(a[i]);
		}
		v.erase(unique(v.begin(), v.end()), v.end());
		sort(v.begin(), v.end());
		for (int i = 0; i < (int)v.size(); i ++) {
			m[v[i]] = i;
		}
		for (int i = 1; i <= n; i ++) {
			a[i] = m[a[i]];
		}
		int ans = 0x3f3f3f3f;
		memset(vis, 0, sizeof(vis));
		for (int i = 1; i <= n; i ++) {
			if (!vis[a[i]]) {
				vis[a[i]] = 1;
				int l = 1, r = n;
				while (a[l] == a[i] && l <= n) l++;
				while (a[r] == a[i] && r >= l) r--;
				ans = min(ans, r - l + 1);
			}
		}
		cout << ans << '\n';
	}
	return 0;
}

D

题目

我们发现,对于每两个数 a a a b b b,如果这两个数符合题目要求,就表明:
a a a 除以 y y y 的余数等于 b b b 除以 y y y 的余数, a a a 除以 x x x 的余数加上 b b b 除以 x x x 的余数要么为 0 0 0,要么为 x x x

有了这个结论,就可以开始编码。我们可以将数按除以 y y y 的余数排序,这样保证除以 y y y 的余数相等的数都挨在一起。再找到每一个连续的除以 y y y 相等的区间,对于每一个除以 x x x 的余数为 p p p 的数,让答案加上数列里除以 x x x 余数为 x − p x - p xp 的数的个数,如果 p = x − p p=x-p p=xp,说明加上了自己,要减去 1 1 1,此外,如果 p = 0 p=0 p=0,我们就要找除以 x x x 余数为 0 0 0 的个数,同样要减去一。

注意:要开长整型,注意边界问题。

AC Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
int t, n, x, y;
struct node{
	int a, mod_x, mod_y;
};
node a[200100];
bool cmp(node a, node b) {
	return a.mod_y < b.mod_y;
}
long long ans;
map<int, int> cnt;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t --) {
		ans = 0;
		cin >> n >> x >> y;
		for (int i = 1; i <= n; i ++) cin >> a[i].a;
		for (int i = 1; i <= n; i ++) {
			a[i].mod_x = a[i].a % x;
			a[i].mod_y = a[i].a % y;
		}
		sort(a + 1, a + n + 1, cmp);
		int i = 1;
		while (i <= n) {
			int l = i, r = i;
			while (a[l].mod_y == a[r].mod_y && r <= n) r++;
			r--;
			if (l == r) {
				i = r + 1;
				continue;
			}
			for (int j = l; j <= r; j++) {
				cnt[a[j].mod_x]++;
			}
			for (int j = l; j <= r; j++) {
				if (a[j].mod_x == 0) {
					ans += cnt[0];
					ans--;
				}
				else {
					ans += cnt[x - a[j].mod_x];
					if (a[j].mod_x * 2 == x) ans--;
				}
			}
			i = r + 1;
			cnt.clear();
		}
		ans /= 2;
		cout << ans << '\n';
	}
	return 0;
}

E

题目

我们发现,对于每一个末尾有 p p p 0 0 0 的数,翻转会让这个数少 p p p 位,对于安娜,翻转一个数会让最终的数长度减少这个数后缀 0 0 0 的个数,而对于萨沙,合并一个数会让这个数后缀 0 0 0 无法消除。

所以安娜尽量选择后缀 0 0 0 更多的翻转,萨沙尽量选择后缀 0 0 0 更多的数消除。统计每个数后缀 0 0 0 的个数,然后从大到小排序,因为安娜是先手,所以她会翻转第 1 1 1 大,第 3 3 3 大,第 5 5 5 大,最后统计答案位数,判断输出,完结撒花。

AC Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
int t;
int n, m;
struct node{
	string a; int cnt;
};
node a[200100];
bool cmp(node a, node b) {
	return a.cnt > b.cnt;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t --) {
		cin >> n >> m;
		int sum = 0;
		for (int i = 1; i <= n; i ++) cin >> a[i].a;
		for (int i = 1; i <= n; i ++) {
			for (int j = a[i].a.size() - 1; j >= 0; j --) {
				if (a[i].a[j] != '0') {
					a[i].cnt = a[i].a.size() - j - 1;
					break;
				}
			}
			sum += a[i].a.size();
		}
		sort(a + 1, a + n + 1, cmp);
		for (int i = 1; i <= n; i += 2) {
			sum -= a[i].cnt;
		}
//		for (int i = 1; i <= n; i ++) {
//			cout << a[i].cnt << ' ';
//		}
//		cout << '\n';
		if (sum > m) {
			cout << "Sasha";
		}
		else {
			cout << "Anna";
		}
		cout << '\n';
	}
	return 0;
}

F

题目

我们发现,对于每一张截图,除开第一个数字,如果 a a a b b b 靠前,说明序列中 a a a b b b 靠前。如有一张截图中 a a a b b b 靠前,另一张截图中 b b b a a a 靠前(均除开第一个数字),说明无解。暴力枚举 a a a b b b 是不行的。我们发现这个关系可以看做一张图中的边,而每一个数字就是一个点。如果这张图中有环,说明无解。

对于每一张截图,除开第一个数字,对于每一个剩下的数字,我们往后面的一个数字建一条有向边,不需要建多条,因为其他的关系可以通过这些边推导出来。然后在这张图上判断一遍是否有环即可。可以用 DFS 或拓补排序判断。我推荐拓补,稳定的 O ( n ) O(n) O(n)

AC Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
struct edge{
	int u, v, nxt;
};
edge ed[400100];
int edcnt, head[200100];
void addedge(int u, int v){
	edcnt++;
	ed[edcnt].u = u;
	ed[edcnt].v = v;
	ed[edcnt].nxt = head[u];
	head[u] = edcnt;
}
int t;
int n, k;
int a[200100];
int deg[200100];
queue<int> q;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t --) {
		edcnt = 0;
		memset(head, 0, sizeof(head));
		memset(deg, 0, sizeof(deg));
		cin >> n >> k;
		for (int i = 1; i <= k; i ++) {
			for (int j = 1; j <= n; j ++) {
				cin >> a[j];
			}
			for (int j = 2; j < n; j ++) {
				addedge(a[j], a[j + 1]);
				deg[a[j + 1]]++;
			}
		}
		for (int i = 1; i <= n; i ++) {
			if (!deg[i]) {
				q.push(i);
			}
		}
		int cnt = 0;
		while (q.size()) {
			int now = q.front();
			q.pop();
			cnt++;
			for (int i = head[now]; i; i = ed[i].nxt) {
				int v = ed[i].v;
				deg[v]--;
				if (!deg[v]) {
					q.push(v);
				}
			}
		}
		if (cnt == n) {
			cout << "YES\n";
		}
		else {
			cout << "NO\n";
		}
	}
	return 0;
}
  • 15
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值