Codeforces Round 941 (Div. 2) A-D
A. Card Exchange
贪心。
如果有某个数出现
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
int n, k;
cin >> n >> k;
vector<int> c(n + 1);
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
vector<int> cnt(101);
for (int i = 1; i <= n; i++) {
cnt[c[i]]++;
}
bool f = false;
for (int i = 1; i <= 100; i++) {
if (cnt[i] >= k) {
f = true;
}
}
if (f) {
cout << k - 1 << '\n';
} else {
cout << n << '\n';
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
B. Rectangle Filling
思维。
对于网格左上角的方格而言,当且仅当网格最右侧和最下侧都出现了与左上角相同的颜色时可以实现全网格同色,右下角同理,左下和右上已经包括在前两种情况中了,不用重复判断。
时间复杂度:
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<char>> a(n + 1, vector<char>(m + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
bool col = false, row = false;
for (int i = 1; i <= n; i++) {
if (a[i][m] == a[1][1]) {
col = true;
}
}
for (int i = 1; i <= m; i++) {
if (a[n][i] == a[1][1]) {
row = true;
}
}
if (col && row) {
cout << "YES\n";
} else {
col = false, row = false;
for (int i = 1; i <= n; i++) {
if (a[i][1] == a[n][m]) {
col = true;
}
}
for (int i = 1; i <= m; i++) {
if (a[1][i] == a[n][m]) {
row = true;
}
}
if (col && row) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
C. Everything Nim
博弈论。
考虑对原数组排序去重后考虑,若最小石堆石头数大于
时间复杂度:
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(ALL(a));
a.erase(unique(ALL(a)), a.end());
n = a.size() - 1;
int t = 0;
for (int i = 1; i <= n; i++) {
if (a[i] - a[i - 1] != 1) {
t = i;
break;
}
}
if ((t & 1) || (t == 0 && (n & 1))) {
cout << "Alice\n";
} else {
cout << "Bob\n";
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
D. Missing Subsequence Sum
构造、位运算。
先考虑如何构造子序列和包含
可以发现现在无法构造出来的区间为
此处考虑添
最后注意特判一下
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
int n, k;
cin >> n >> k;
int x = 1, s = 0;
vector<int> a(26);
int i;
for (i = 1; i <= 25; i++) {
if (s + x < k) {
a[i] = x;
s += x;
x <<= 1;
} else {
a[i] = k - 1 - s;
break;
}
}
a[++i] = k + 1;
if ((s + (x << 1)) != k) {
a[++i] = (s + (x << 1));
}
if (k == 1) {
a[++i] = 3;
}
while (i < 25) {
x <<= 1;
a[++i] = x;
}
cout << 25 << '\n';
for (int i = 1; i <= 25; i++) {
cout << a[i] << " \n"[i == 25];
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】