【CodeForces】CodeForces Round #517 (Div. 1 + Div. 2) 题解

本文提供了CodeForces Round #517 (Div. 1 + Div. 2)比赛的题解,包括Div.2的A-E题和Div.1的A-E题。涉及到的算法包括循环计算、独立方案枚举、分层DP、暴力搜索优化等,每道题目都详细阐述了思路要点和时间复杂度,并给出了相应的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【比赛链接】

【题解链接】

**【Div.2 A】**Golden Plate

【思路要点】

  • 直接循环计算答案即可。
  • 时间复杂度 O ( K ) O(K) O(K)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int main() {
	int n, m, k;
	read(n), read(m), read(k);
	int ans = 0;
	for (int i = 1; i <= k; i++) {
		int x = n - 4 * (i - 1);
		int y = m - 4 * (i - 1);
		ans += x * 2 + y * 2 - 4;
	}
	writeln(ans);
	return 0;
}

**【Div.2 B】**Curiosity Has No Limits

【思路要点】

  • 首先,每一位的方案是相互独立的,我们可以分开考虑。
  • 当确定 x , x &amp; y , x ∣ y x,x\&amp;y,x|y x,x&y,xy 时,合法的 y y y 至多只有 1 1 1 个。
  • 因此枚举开头位置,构造出序列,再检验合法性即可。
  • 时间复杂度 O ( N ) O(N) O(N)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, a[MAXN], b[MAXN], x[MAXN];
int func(int a, int b, int x) {
	if (a == b) return a;
	else if (a == 0) return 0;
	else return 1 - x;
}
bool tryans(int f) {
	x[1] = f;
	for (int i = 1; i <= n - 1; i++)
		x[i + 1] = func(a[i] / 2, b[i] / 2, x[i] / 2) * 2 + func(a[i] % 2, b[i] % 2, x[i] % 2);
	for (int i = 1; i <= n - 1; i++) {
		if ((x[i] | x[i + 1]) != a[i]) return false;
		if ((x[i] & x[i + 1]) != b[i]) return false;
	}
	return true;
}
void answer() {
	printf("YES\n");
	for (int i = 1; i <= n; i++)
		printf("%d ", x[i]);
	exit(0);
}
int main() {
	read(n);
	for (int i = 1; i <= n - 1; i++)
		read(a[i]);
	for (int i = 1; i <= n - 1; i++)
		read(b[i]);
	if (tryans(0)) answer();
	if (tryans(1)) answer();
	if (tryans(2)) answer();
	if (tryans(3)) answer();
	printf("NO\n");
	return 0;
}

**【Div.2 C/Div.1 A】**Cram Time

【思路要点】

  • x x x 表示最大的使得 x ( x + 1 ) 2 ≤ a + b \frac{x(x+1)}{2}≤a+b 2x(x+1)a+b 的整数,答案显然有上界为 x x x
  • 若我们保证第一天用完刚好 a a a 小时或完成所有 x x x 个任务,那么答案将一定会取到上界。只需要从大到小考虑每个任务,优先将其安排在第一天即可。
  • 时间复杂度 O ( a + b ) O(\sqrt{a+b}) O(a+b )

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
vector <int> a, b;
int main() {
	int n, m; read(n), read(m);
	int sum = n + m, tot = 0;
	while (sum >= tot + 1) {
		tot++;
		sum -= tot;
	}
	for (int i = tot; i >= 1; i--)
		if (n >= i) {
			n -= i;
			a.push_back(i);
		} else b.push_back(i);
	writeln(a.size());
	for (auto x : a)
		printf("%d ", x);
	printf("\n");
	writeln(b.size());
	for (auto x : b)
		printf("%d ", x);
	printf("\n");
	return 0;
}

**【Div.2 D/Div.1 B】**Minimum path

【思路要点】

  • 分层 D P DP DP ,令点 ( i , j ) (i,j) (i,j) 为第 i + j − 1 i+j-1 i+j1 层的点。
  • 首先确定答案的第 i i i a n s i ans_i ans
### Codeforces Round 260 Div. 1 题目及题解 #### A. Vasya and Multisets 在这道题目中,Vasya有一个由n个整数组成的序列。目标是通过将这些数分成若干组,使得每组中的所有数都相同,并且尽可能减少分组的数量。 为了实现这一目的,可以利用贪心算法来解决这个问题。具体来说,在遍历输入数据的同时维护当前最大频率计数器,对于每一个新遇到的不同数值增加一个新的集合[^1]。 ```cpp #include <bits/stdc++..h> using namespace std; void solve() { int n; cin >> n; unordered_map<int, int> freq; for (int i = 0; i < n; ++i) { int x; cin >> x; freq[x]++; } int maxFreq = 0; for (auto& p : freq) { maxFreq = max(maxFreq, p.second); } cout << maxFreq << "\n"; } ``` #### B. Pashmak and Graph 此问题涉及图论领域的一个经典最短路径计算案例。给定一张带权无向图以及起点S和终点T,要求求出从S到T经过至少一条边后的最小花费总和。 Dijkstra算法适用于此类场景下的单源最短路径查询任务。初始化距离表dist[]为无穷大(INF),仅设置起始节点的距离为零;随后借助优先队列选取未访问过的最近邻接顶点u更新其相邻结点v至目前为止所知的最佳到达成本min{dist[u]+w(u,v)}直至找到终止条件即抵达目的地t或处理完毕所有可达区域内的候选者为止。 ```cpp typedef pair<long long,int> pli; const long long INF = LLONG_MAX / 3; struct Edge { int to, cost; }; vector<Edge> G[MAX_V]; long long d[MAX_V]; bool dijkstra(int s, int t){ priority_queue<pli,vector<pl>,greater<pl>> que; fill(d,d+MAX_V,INF); d[s]=0; que.push(pli(0,s)); while(!que.empty()){ pli p=que.top();que.pop(); int v=p.second; if(d[v]<p.first) continue; for(auto e:G[v]){ if(d[e.to]>d[v]+e.cost){ d[e.to]=d[v]+e.cost; que.push(pli(d[e.to],e.to)); } } } return d[t]!=INF; } ``` #### C. DZY Loves Colors 这是一道关于颜色染色的问题。给出长度为N的一维网格,初始状态下每个格子都有一个默认的颜色编号。现在有M次操作机会改变某些位置上的色彩值,最终目的是统计整个条带上共有几种不同的色调存在。 采用离散化技术预处理原始输入并记录下各段连续同色区间的端点坐标范围,之后针对每一次修改请求动态调整受影响部分的信息结构体(如线段树),最后依据累积的结果得出答案。 ```cpp // 假设已经实现了上述提到的数据结构 SegmentTree 和 update 函数 SegmentTree st; for(int i=1;i<=m;++i){ int l,r,c; scanf("%d%d%d",&l,&r,&c); update(l,r,c); } printf("%lld\n",st.query()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值