Codeforces Round 481 (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;
int a[N];
umap<int, int> mp;
vector<int> ans;
void solve() {
cin >> n;
forn(i, 1, n) cin >> a[i];
forn_(i, n, 1) {
if (mp[a[i]] == 0) ans.push_back(a[i]);
mp[a[i]]++;
}
cout << ans.size() << endl;
reverse(ans.begin(), ans.end());
for (auto i : ans) cout << i << ' ';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
B 模拟 贪心
思路
考虑贪心,为了删除次数最少,肯定只能删x,只要连续x长度小于3就行了找到连续的x然后判断长度该删多少,累加答案即可
代码
#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;
char s[N];
int ans;
void solve() {
cin >> n;
forn(i, 1, n) cin >> s[i];
int res = 0;
s[0] = s[n + 1] = '?';
forn(i, 0, n + 1) {
if (s[i] != 'x') {
ans += max(0, res - 2);
res = 0;
} else res++;
}
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
C 二分
思路

代码
#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 T;
ll n, m, a;
ll s[N];
ll find() {
ll l = 0, r = n;
while (l < r) {
ll mid = l + r >> 1;
if (s[mid] >= a) r = mid;
else l = mid + 1;
}
return l;
}
void solve() {
cin >> n >> m;
forn(i, 1, n) cin >> a, s[i] = s[i - 1] + a;
forn(i, 1, m) {
cin >> a;
ll f = find();
cout << f << ' ' << a - s[f - 1] << endl;
}
}
int main() {
//cin >> T;
T = 1;
while (T--) {
solve();
}
return 0;
}
D 思维 枚举
思路

代码
#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;
int a[N];
int ans = inf;
void solve() {
cin >> n;
forn(i, 1, n) cin >> a[i];
if (n == 1 || n == 2) {
cout << 0;
return;
}
int x = a[1], y = a[2];
forn(i, -1, 1) forn(j, -1, 1) {
a[1] = x + i, a[2] = y + j;
int d = a[2] - a[1];
int ok = 1, res = 0, lst = a[1];
if (i != 0) res++;
if (j != 0) res++;
forn(k, 2, n) {
int da = a[k] - lst;
if (da == d) lst = a[k];
else if (da == d + 1) res++, lst = a[k] - 1;
else if (da == d - 1) res++, lst = a[k] + 1;
else ok = 0;
}
if (ok) ans = min(ans, res);
}
if (ans == inf) ans = -1;
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
E 数学
思路
就是解不等式组,答案就是区间长度,注意特判负数为0 代码
#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, w;
ll a[N];
ll l, r;
void solve() {
cin >> n >> w;
forn(i, 1, n) cin >> a[i];
l = 0, r = w;
ll res = 0;
forn(i, 1, n) {
res -= a[i];
l = max(l, res);
r = min(r, w + res);
}
cout << max(0ll, r - l + 1) << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
F 二分 排序
思路

代码
#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, m;
int a[N], b[N];
int ans[N];
void solve() {
cin >> n >> m;
forn(i, 1, n) cin >> a[i], b[i] = a[i];
sort(b + 1, b + n + 1);
forn(i, 1, n) {
int l = lower_bound(b + 1, b + n + 1, a[i]) - b;
ans[i] = l - 1;
}
while (m--) {
int x, y;
cin >> x >> y;
if (a[x] < a[y]) ans[y]--;
if (a[x] > a[y]) ans[x]--;
}
forn(i, 1, n) cout << ans[i] << ' ';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}
G 模拟 贪心
思路
水题啊肯定从最早的考试开始准备,就先以d为关键字排个序
就考虑贪心,从s开始填到d-1,如果填的数量少于c说明不符合题意,输出-1

代码
#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, m;
struct node {
int s, d, c;
int id;
} a[N];
bool cmp(node x, node y) {
return x.d < y.d;
}
int ans[N];
void solve() {
cin >> n >> m;
forn(i, 1, m) cin >> a[i].s >> a[i].d >> a[i].c, a[i].id = i, ans[a[i].d] = m + 1;
sort(a + 1, a + m + 1, cmp);
forn(i, 1, m) {
int s = a[i].s, d = a[i].d, c = a[i].c, id = a[i].id;
if (d - s < c) {
cout << -1;
return;
}
int cnt = 0;
forn(j, s, d - 1) {
if (ans[j] != 0) continue;
cnt++;
ans[j] = id;
if (cnt == c) break;
}
if (cnt < c) {
cout << -1;
return;
}
}
forn(i, 1, n) cout << ans[i] << ' ';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin >> T;
T = 1;
while (T--) solve();
return 0;
}

浙公网安备 33010602011771号