Codeforces Round 870 (A-D)
Posted huaiii
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round 870 (A-D)相关的知识,希望对你有一定的参考价值。
A
\\(n^2\\)暴力推,遍历可能撒谎的人数(0-n),然后\\(O(n)\\)check就行了。
仔细看逻辑其实\\(O(1)\\)就能check,草率了,后面再修正
点击查看代码
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define per(i, r, l) for(int i = r; i >= l; i--)
#define debug(tar, l, r) rep(ii, l, r) cout << tar[ii] << " \\n"[ii==r]
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e5+5;
const int mod = 1e9+7;
int n, a[N];
bool chk(int x)
rep(i, 1, n-x)
if(a[i] > x) return false;
rep(i, n-x+1, n)
if(a[i] <= x) return false;
return true;
void solve()
cin >> n;
rep(i, 1, n) cin >> a[i];
sort(a+1, a+1+n);
rep(i, 0, n)
if(chk(i))
cout << i << "\\n";
return ;
cout << "-1\\n";
int main()
std::ios::sync_with_stdio(false);
int tt; cin >> tt; while(tt--)
solve();
return 0^0;
B
回文对应的两个数的差值一定是\\(x\\)的倍数,把所有情况取个gcd即可
点击查看代码
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define per(i, r, l) for(int i = r; i >= l; i--)
#define debug(tar, l, r) rep(ii, l, r) cout << tar[ii] << " \\n"[ii==r]
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e5+5;
const int mod = 1e9+7;
int n, b[N];
void solve()
cin>> n;
rep(i, 1, n) cin >> b[i];
int ans = 0;
rep(i, 1, n/2)
int dif = abs(b[i]-b[n-i+1]);
ans = __gcd(ans, dif);
cout << ans << "\\n";
int main()
std::ios::sync_with_stdio(false);
int tt; cin >> tt; while(tt--)
solve();
return 0^0;
C
n除1外的最小的因子小于等于m即可
点击查看代码
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define per(i, r, l) for(int i = r; i >= l; i--)
#define debug(tar, l, r) rep(ii, l, r) cout << tar[ii] << " \\n"[ii==r]
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e6+5;
const int mod = 1e9+7;
int n, m;
bool no[N];
vector <int> p;
void init()
rep(i, 2, 1000000)
if(!no[i]) p.push_back(i);
for(auto& pp:p)
if(pp*i > 1000000) break;
no[pp*i] = 1;
void solve()
cin >> n >> m;
if(n > 1 && n <= m)
cout << "NO\\n";
return ;
for(auto& pp:p)
if(pp*pp > n) break;
if(n%pp == 0)
if(pp <= m)
cout << "NO\\n";
return ;
cout << "YES\\n";
int main()
std::ios::sync_with_stdio(false);
init();
int tt; cin >> tt; while(tt--)
solve();
return 0^0;
D
做法是尺取,不知道会不会假了
将\\(b_i1+b_i2+b_i3-(r-l)\\)转换成\\((b_l+l)+(b_r-r)+b_i\\),其中\\(l < i < r\\)
对\\(b_i+i\\)求前缀最值,对\\(b_i-i\\)求后缀最值,中间\\(b_i\\)用st表,然后尺取
点击查看代码
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define per(i, r, l) for(int i = r; i >= l; i--)
#define debug(tar, l, r) rep(ii, l, r) cout << tar[ii] << " \\n"[ii==r]
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e5+5;
const int mod = 1e9+7;
int n, b[N], p[N], s[N], f[N][30];
void pre()
rep(i, 1, n) f[i][0] = b[i];
for (int k = 1; (1 << k) <= n; ++k)
for (int i = 1; i + (1 << k) - 1 <= n; ++i)
f[i][k] = max(f[i][k - 1], f[i + (1 << (k - 1))][k - 1]);
int ask(int l, int r)
int k = log2(r - l + 1);
return max(f[l][k], f[r - (1 << k) + 1][k]);
void solve()
cin >> n;
rep(i, 1, n) cin >> b[i];
pre();
p[0] = s[n+1] = -1e9;
rep(i, 1, n) p[i] = max(p[i-1], b[i]+i);
per(i, n, 1) s[i] = max(s[i+1], b[i]-i);
int l = 1;
int ans = 0;
for(int i = 3; i <= n; i++)
while(l+1 <= i-2 && p[l+1]+ask(l+2, i-1) >= p[l]+ask(l+1, i-1)) l++;
ans = max(ans, p[l]+ask(l+1, i-1)+s[i]);
cout << ans << "\\n";
int main()
std::ios::sync_with_stdio(false);
int tt; cin >> tt; while(tt--)
solve();
return 0^0;
Codeforces Edu Round 59 A-D
A. Digits Sequence Dividing
注意特殊情况,在\(n = 2\)时除非\(str[1] >= str[2]\),否则可以把第一个数划分下来,剩下的数直接当成一组,一定满足条件。
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
const int N = 310;
int n;
char s[N];
int main()
int T; scanf("%d", &T);
while(T--)
scanf("%d%s", &n, s + 1);
if(n == 2)
if(s[1] >= s[2]) puts("NO");
else printf("YES\n2\n%c %c\n", s[1], s[2]);
else
printf("YES\n2\n%c ", s[1]);
for(int i = 2; i <= n; i++) putchar(s[i]);
puts("");
return 0;
B. Digital root
通过打表找规律发现的…看了题解,证明还是很NB的...
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
int main()
int T; scanf("%d", &T);
while(T--)
LL k, x; scanf("%lld%lld", &k, &x);
printf("%lld\n", (k - 1) * 9 + x);
return 0;
C. Brutality
用堆维护连续子段最大和即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 200010;
int n, k, a[N];
LL ans = 0;
char s[N];
priority_queue<int, vector<int>, greater<int> > q;
int main()
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) scanf("%d", a + i);
scanf("%s", s + 1);
for(int i = 1; i <= n; i++)
if(s[i] != s[i - 1])
while(!q.empty()) ans += q.top(), q.pop();
q.push(a[i]);
else
q.push(a[i]);
while(q.size() > k) q.pop();
while(!q.empty()) ans += q.top(), q.pop();
printf("%lld\n", ans);
return 0;
D. Compression
实质上是把这图压缩到最小的点阵图,用\(bitset\)优化复杂度,暴力水过。
#include <cstdio>
#include <iostream>
#include <bitset>
using namespace std;
const int N = 5210;
bitset<N> a[N];
int n;
//密集恐惧症
int main()
ios::sync_with_stdio(false);
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j += 4)
char x; cin >> x;
//16进制转2进制
if(x == '1') a[i][j + 3] = 1;
else if(x == '2') a[i][j + 2] = 1;
else if(x == '3') a[i][j + 2] = a[i][j + 3] = 1;
else if(x == '4') a[i][j + 1] = 1;
else if(x == '5') a[i][j + 1] = a[i][j + 3] = 1;
else if(x == '6') a[i][j + 2] = a[i][j + 3] = 1;
else if(x == '7') a[i][j + 1] = a[i][j + 2] = a[i][j + 3] = 1;
else if(x == '8') a[i][j] = 1;
else if(x == '9') a[i][j] = a[i][j + 3] = 1;
else if(x == 'A') a[i][j] = a[i][j + 2] = 1;
else if(x == 'B') a[i][j] = a[i][j + 2] = a[i][j + 3] = 1;
else if(x == 'C') a[i][j] = a[i][j + 1] = 1;
else if(x == 'D') a[i][j] = a[i][j + 1] = a[i][j + 3] = 1;
else if(x == 'E') a[i][j] = a[i][j + 1] = a[i][j + 2] = 1;
else if(x == 'F') a[i][j] = a[i][j + 1] = a[i][j + 2] = a[i][j + 3] = 1;
for(int x = n; x >= 2; x--)
if(n % x) continue;
bool ep = true;
for(int i = 1; i <= n; i += x)
for(int j = i + 1; j < i + x; j++)
ep = ep && (a[j] == a[i]);
if(!ep) break;
if(!ep) break;
for(int j = 1; j <= n; j += x)
for(int k = j + 1; k < j + x; k++)
ep = ep && (a[i][k] == a[i][k - 1]);
if(!ep) break;
if(!ep) break;
if(!ep) break;
if(ep) cout << x; return 0;
cout << 1;
return 0;
以上是关于Codeforces Round 870 (A-D)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #757 div.2 A-D题解
Codeforces Round #735 (Div. 2) 题解(A-D)