CodeForces Round 925 Div.3 A-F 题解

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;
}

相关推荐

  1. CodeForces Round 925 Div.3 A-F 题解

    2024-02-16 04:22:01       29 阅读
  2. cf923Div3F

    2024-02-16 04:22:01       22 阅读
  3. Codeforces Round 935 (Div.3 E F)

    2024-02-16 04:22:01       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-16 04:22:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-16 04:22:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-16 04:22:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-16 04:22:01       20 阅读

热门阅读

  1. 数据结构入门(3)1:顺序表接口实现

    2024-02-16 04:22:01       30 阅读
  2. 蓝桥杯:日期统计讲解(C++)

    2024-02-16 04:22:01       29 阅读
  3. C++11 thread_local

    2024-02-16 04:22:01       31 阅读
  4. 「数据结构」优先级队列

    2024-02-16 04:22:01       33 阅读
  5. 0|1分数规划

    2024-02-16 04:22:01       26 阅读
  6. Pycharm配置运行selenium教程

    2024-02-16 04:22:01       27 阅读
  7. 微服务设计:Spring Cloud 链路追踪概述

    2024-02-16 04:22:01       29 阅读
  8. 代码随想录二刷——二叉树day18

    2024-02-16 04:22:01       34 阅读
  9. 代码随想录算法训练营|day32

    2024-02-16 04:22:01       34 阅读
  10. 2024.02.11

    2024-02-16 04:22:01       31 阅读