Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题)
1406A. Subset Mex
https://codeforces.com/contest/1406/problem/A
Example
input
4
6
0 2 1 5 0 1
3
0 1 2
4
0 2 0 1
6
1 2 3 4 5 6
output
5
3
4
0
Note
In the first test case,
In the second test case,
In the third test case,
In the fourth test case,
题意:
给定一个集合,并定义
如:
求 集合分为两部分的最大值:
思路:
通过维护两个变量从0开始,如果有0、1、2、3...这样的直接慢慢向上叠加
#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
ll n, a[N];
void solve() {
cin >> n;
for (int i = 0; i < n; ++i)cin >> a[i];
sort(a, a + n);
ll m = 0, k = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == m)m++;
else if (a[i] == k)k++;
}
cout << m + k << endl;//m、k相当于两个集合中的非负最小值
}
int main() {
//freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll _; cin >> _;
while (_--)solve();
}
1406B. Maximum Product
https://codeforces.com/contest/1406/problem/B
Example
input
4
5
-1 -2 -3 -4 -5
6
-1 -2 -3 1 2 -1
6
-1 0 0 0 -1 -1
6
-9 -7 -5 -3 -2 1
output
-120
12
0
945
Note
In the first test case, choosing
In the second test case, choosing
In the third test case, choosing
In the fourth test case, choosing
题意:
给定 大小为n的一个数组,求下标
思路:
一开始以为不能排序,搞得卡了好久。
先对所给数组进行排序,这样必然倒数5个最大,又因为存在负数的关系,所以也许
#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
ll n, a[N];
void solve() {
cin >> n; ll ans[4] = {};
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
ans[0] = a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4];
ans[1] = a[n] * a[n - 1] * a[n - 2] * a[1] * a[2];
ans[2] = a[n] * a[1] * a[2] * a[3] * a[4];
sort(ans, ans + 3); cout << ans[2] << endl;
}
int main() {
//freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll _; cin >> _;
while (_--)solve();
}
1406C. Link Cut Centroids
https://codeforces.com/contest/1406/problem/C
题目太长这里不粘贴了。
题意:
思路:
DFS搜索,详细待补。请先阅读代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n;
int p1, p2, p3, c[N];
vector<int>g[N];
void dfs(int u, int fa) {
c[u] = 1;
for (auto v:g[u]) if (v != fa) {
dfs(v, u), c[u] += c[v];
}
if (!p3) {
if (c[u] == 1) p1 = fa, p2 = u;
if (n - c[u] == c[u]) p3 = fa;
}
}
signed main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
cin >> n;
p1 = p2 = p3 = 0;
for (int i = 1; i <= n; i++) g[i].clear(), c[i] = 0;
for (int i = n; --i;) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
cout << p1 << ' ' << p2 << '\n' << p2 << ' ' << (p3 ? p3 : p1) << '\n';
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 全程不用写代码,我用AI程序员写了一个飞机大战