Codeforces Round 486 (Div. 3) 补题AK
比赛链接‘
A 模拟
思路
直接哈希去重就行了代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
int T;
int n, k, x;
umap<int, int> mp;
void solve() {
cin >> n >> k;
forn(i, 1, n) cin >> x, mp[x]=i;
if (mp.size() < k) cn;
else {
cy;
int cnt = 0;
for (auto [i, j] : mp) {
cout << j << ' ';
if (++cnt == k) break;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
B 字符串 排序
思路

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
int T;
int n;
string str[N];
bool cmp(string a, string b) {
return a.size() < b.size();
}
int ok = 1;
void solve() {
cin >> n;
forn(i, 1, n) cin >> str[i];
sort(str + 1, str + n + 1, cmp);
forn(i, 2, n) if (str[i].find(str[i - 1]) == -1) ok = 0;
if (!ok) {
cn;
return;
}
cy;
forn(i, 1, n) cout << str[i] << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
C 哈希 思维
思路
先算出每组数的总和sum,那么每一种情况就是sum-a[i],然后全部丢进哈希表1里,表示出现过没有,再整个哈希表2,表示这种情况是出现在第几组里的先对每一种情况判断是否存在,存在的话,就找到在那一组里,再暴力找下标
如果都没找到就输出NO
代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
int T;
int k;
int n[N], sum[N], x;
vector<int> g[N];
umap<int, int> mp, all;
void solve() {
cin >> k;
forn(i, 1, k) g[i].push_back(0);
forn(i, 1, k) {
cin >> n[i];
forn(j, 1, n[i]) cin >> x, g[i].push_back(x), sum[i] += x;
forn(j, 1, n[i]) {
x = sum[i] - g[i][j];
if (all[x] == 1) {
cy;
cout << i << ' ' << j << endl;
int y = mp[x];
forn(ii, 1, n[y]) {
if (sum[y] - g[y][ii] == x) {
cout << y << ' ' << ii << endl;
break;
}
}
return;
}
}
forn(j, 1, n[i]) {
x = sum[i] - g[i][j];
all[x] = 1;
mp[x] = i;
}
}
cn;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
D 数学 枚举
思路

开set搜就行了
代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
ll n;
ll a[N];
set<ll> s;
int main() {
cin >> n;
forn(i, 1, n) cin >> a[i], s.insert(a[i]);
forn(i, 1, n) {
forn(j, 0, 30) {
ll res = 1 << j;
if (s.count(a[i] + res) && s.count(a[i] + res + res)) {
cout << 3 << endl;
cout << a[i] << ' ' << a[i] + res << ' ' << a[i] + res + res;
return 0;
}
}
}
forn(i, 1, n) {
forn(j, 0, 30) {
ll res = 1 << j;
if (s.count(a[i] + res)) {
cout << 2 << endl;
cout << a[i] << ' ' << a[i] + res;
return 0;
}
}
}
cout << 1 << endl;
cout << a[1];
return 0;
}
E 数学 贪心
思路

代码
// LUOGU_RID: 141476032
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
ll n;
string s, x;
ll find(char a, char b) {
s = x;
int f1 = s.rfind(b);
if (f1 == -1) return inf;
s.erase(f1, 1);
int f2 = s.rfind(a);
if (f2 == -1) return inf;
s.erase(f2, 1);
int res = 0;
while (s[res] == '0') res++;
return res + (n - 1 - f1) + (n - 2 - f2);
}
ll ans = inf;
int main() {
cin >> x;
n = x.size();
ans = min({ans, find('0', '0'), find('2', '5'), find('5', '0'), find('7', '5')});
if (ans == inf) ans = -1;
cout << ans;
return 0;
}
F 贪心 线性DP
思路

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e3 + 5;
ll a, n, m;
bool is[N];
int u[N];
int f[N][N], mn[N];
int main() {
cin >> a >> n >> m;
forn(i, 1, n) {
int l, r;
cin >> l >> r;
forn(j, l + 1, r) is[j] = 1;
}
memset(u, inf, sizeof u);
forn(i, 1, m) {
int x, p;
cin >> x >> p;
u[x + 1] = min(u[x + 1], p);
}
u[0] = 0;
memset(f, inf, sizeof f);
memset(mn, inf, sizeof mn);
f[0][0] = mn[0] = 0;
forn(i, 1, a + 1) {
forn(j, 0, i - 1) {
if (is[i]) {
if (j == 0) continue;
f[i][j] = min(f[i][j], f[i - 1][j] + u[j]);
} else {
if (j == 0) f[i][j] = min(f[i][j], mn[i - 1]);
else f[i][j] = min(f[i][j], f[i - 1][j] + u[j]);
}
}
f[i][i] = min(f[i][i], mn[i - 1] + u[i]);
forn(j, 0, i) mn[i] = min(mn[i], f[i][j]);
}
if (f[a + 1][0] == inf) f[a + 1][0] = -1;
cout << f[a + 1][0];
return 0;
}

浙公网安备 33010602011771号