题目总结
CF 1933 B
题目描述
给定一个数组
- 删除一个元素。
- 使一个元素加一。
求最少进行多少次操作可以使
思路
分类讨论:
-
-
+ $\exists A_i \equiv 1(\operatorname{mod} 3)$,则可以把 $A_i$ 删掉,所以答案为 $1$。 + 否则,只能加两次 $1$,所以答案为 $2$。 -
时间复杂度
细节
数据千万条,清空第一条。
多测不清空,爆零两行泪。
清空不规范,超时总相伴。
代码
#include<bits/stdc++.h>
using namespace std;
int t, n, cnt[3], sum;
void Solve() {
cin >> n;
cnt[0] = cnt[1] = cnt[2] = sum = 0;
for(int i = 1, x; i <= n; ++i) {
cin >> x;
cnt[x % 3]++;
sum = (sum + x % 3) % 3;
}
cout << (!sum ? 0 : (sum == 2 ? 1 : (cnt[1] ? 1 : 2))) << "\n";
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--) {
Solve();
}
return 0;
}
CF 1933 C
题目描述
给定三个正整数
思路
枚举
时间复杂度
细节
无。
代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int t;
ll a, b, l, ans;
ll Pow(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1) {
res *= a;
}
a *= a;
b >>= 1;
}
return res;
}
bool check(ll x) {
for(ll i = 0; Pow(b, i) <= x; ++i) {
if(Pow(b, i) == x) {
return 1;
}
}
return 0;
}
bool C(ll x) {
for(ll i = 0; Pow(a, i) <= x; ++i) {
if(x % Pow(a, i) == 0 && check(x / Pow(a, i))) {
return 1;
}
}
return 0;
}
void Solve() {
cin >> a >> b >> l;
ans = 0;
for(ll i = 1; i * i <= l; ++i) {
if(l % i == 0) {
ans += C(l / i) + (l / i != i ? C(i) : 0ll);
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--) {
Solve();
}
return 0;
}
CF 1933 D
题目描述
给定一个数组
思路
可以发现,如果满足要求只有两种情况:
-
A_1 \le A_2 \le \dots \le A_N 且 A_1 \operatorname{mod} A_2 \operatorname{mod} \dots \operatorname{mod} A_N \ne 0 -
时间复杂度
细节
无。
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
int t, n, a[MAXN];
bool check() {
int m = a[1];
for(int i = 2; i <= n; ++i) {
m %= a[i];
if(!m) {
return 0;
}
}
return 1;
}
bool Check() {
for(int i = 1; i <= n; ++i) {
if(a[i] % a[1]) {
return 1;
}
}
return 0;
}
void Solve() {
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
cout << (check() || Check() ? "YES\n" : "NO\n");
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--) {
Solve();
}
return 0;
}
CF 1933 E
题目描述
有
思路
对于每次询问,二分最大的
时间复杂度
细节
无。
代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 100001;
int t, n, q, a[MAXN], sum[MAXN];
ll Calc(int l, int r, int u) {
return 1ll * (u + 1 - (sum[r - 1] - sum[l - 1] + 1) + u + 1 - (sum[r] - sum[l - 1])) * a[r] / 2ll;
}
int erfen(int L, int u) {
int l = L, r = n;
while(l < r) {
int mid = (l + r + 1) >> 1;
if(Calc(L, mid, u) > 0) {
l = mid;
}else {
r = mid - 1;
}
}
return l;
}
void Solve() {
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
cin >> q;
for(int i = 1, l, u; i <= q; ++i) {
cin >> l >> u;
cout << erfen(l, u) << " ";
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--) {
Solve();
}
return 0;
}
CF 1933 F
题目描述
在一个
思路
由于每秒钟让石头都移动太麻烦了,所以我们可以换一个参考系:让石头不动,往下走转化为往下走两格,往上变为不动,往右变成往右下走。由于不移动没有作用,所以不需要这种转移。可是这样就会导致终点每秒钟往下走一格。所以我们可以先 BFS 一遍,但是不做最后一列(只求不做)。注意到最后一列没有石头,所以可以直接计算出答案为
时间复杂度
细节
无。
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1001, INF = MAXN * MAXN + 1;
struct Node {
int x, y;
};
int t, n, m, dist[MAXN][MAXN], ans = INF;
bool a[MAXN][MAXN], vis[MAXN][MAXN];
queue<Node> que;
void Record(int x, int y, int dis) {
if(vis[x][y]) {
return;
}
vis[x][y] = 1;
dist[x][y] = dis;
if(y == m - 1) {
return;
}
que.push({x, y});
}
void bfs() {
Record(0, 0, 0);
while(!que.empty()) {
auto [x, y] = que.front();
int dis = dist[x][y];
que.pop();
if(!a[(x + 1) % n][y] && !a[(x + 2) % n][y]) {
Record((x + 2) % n, y, dis + 1);
}
if(y < m - 1 && !a[(x + 1) % n][y + 1]) {
Record((x + 1) % n, y + 1, dis + 1);
}
}
}
int Calc(int x, int s) {
int y = (n - 1 + s) % n;
return s + min((x - y + n) % n, (y - x + n) % n);
}
void Solve() {
cin >> n >> m;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
fill(vis[0], vis[n - 1] + m, 0);
fill(dist[0], dist[n - 1] + m, INF);
bfs();
ans = INF;
for(int i = 0; i < n; ++i) {
ans = min(ans, Calc(i, dist[i][m - 1]));
}
cout << (ans == INF ? -1 : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--) {
Solve();
}
return 0;
}
CF 1933 G
题目描述
有一个
其中
思路
写个暴力程序就可以发现只有可能是这
1 1 2 2
2 2 1 1
1 1 2 2
2 2 1 1
1 2 1 2
1 2 1 2
2 1 2 1
2 1 2 1
1 2 1 2
2 1 2 1
2 1 2 1
1 2 1 2
1 2 2 1
2 1 1 2
1 2 2 1
2 1 1 2
2 1 1 2
1 2 2 1
2 1 1 2
1 2 2 1
2 1 2 1
1 2 1 2
1 2 1 2
2 1 2 1
2 1 2 1
2 1 2 1
1 2 1 2
1 2 1 2
2 2 1 1
1 1 2 2
2 2 1 1
1 1 2 2
并且都是以
细节
无。
代码
#include<bits/stdc++.h>
using namespace std;
const int a[8][5][5] = {
{{1, 1, 2, 2},
{2, 2, 1, 1},
{1, 1, 2, 2},
{2, 2, 1, 1}},
{{1, 2, 1, 2},
{1, 2, 1, 2},
{2, 1, 2, 1},
{2, 1, 2, 1}},
{{1, 2, 1, 2},
{2, 1, 2, 1},
{2, 1, 2, 1},
{1, 2, 1, 2}},
{{1, 2, 2, 1},
{2, 1, 1, 2},
{1, 2, 2, 1},
{2, 1, 1, 2}},
{{2, 1, 1, 2},
{1, 2, 2, 1},
{2, 1, 1, 2},
{1, 2, 2, 1}},
{{2, 1, 2, 1},
{1, 2, 1, 2},
{1, 2, 1, 2},
{2, 1, 2, 1}},
{{2, 1, 2, 1},
{2, 1, 2, 1},
{1, 2, 1, 2},
{1, 2, 1, 2}},
{{2, 2, 1, 1},
{1, 1, 2, 2},
{2, 2, 1, 1},
{1, 1, 2, 2}},
};
int t, n, m, q, ans;
void Solve() {
cin >> n >> m >> q;
cout << "8\n";
ans = (1 << 8) - 1;
for(int i = 1, x, y; i <= q; ++i) {
string s;
cin >> x >> y >> s;
x--, y--;
int res = 0;
if(s == "circle") {
for(int i : {0, 1, 2, 3, 4, 5, 6, 7}) {
if(a[i][x % 4][y % 4] == 1) {
res += (1 << i);
}
}
}else {
for(int i : {0, 1, 2, 3, 4, 5, 6, 7}) {
if(a[i][x % 4][y % 4] == 2) {
res += (1 << i);
}
}
}
ans &= res;
cout << __builtin_popcount(ans) << "\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--) {
Solve();
}
return 0;
}