A-Diversity
题目大意
给你一个长度为nnn的字符串,让你求出最少的需要修改几个字符,才能让这个字符串里面出现kkk个不同的字符。
题解
SBT(当然不是指那一个SBT
如果字符串的长度超过了kkk,那么一定没有解。
否则,就记录出现了哪一些字符,然后k−cntk-cntk−cnt为答案,其中cntcntcnt为出现了多少个不同的字符。
tipsSB的我,最后没有和000取maxmaxmax。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 4;
char s[N];
int d[30];
int n, cnt = 0, k;
int main() {
memset(d, 0, sizeof(d));
scanf("%s%d", s, &k);
int len = strlen(s);
if (k > len) { puts("impossible"); return 0; }
for (int i = 0; i < len; i ++) d[s[i] - 'a' + 1] ++;
for (int i = 1; i <= 26; i ++)
if (d[i] != 0) cnt ++;
cout << max(0, k - cnt) << endl;
return 0;
}
B-Rectangles
题目大意
现有 n×m(1<=n,m<=50)n\times m(1<=n,m<=50)n×m(1<=n,m<=50) 的棋盘, 同行同列的相同颜色( 000 表示白色, 而 111 表示黑色)的棋子被划分为一个集合, 问该棋盘最多可以被划分为多少个集合. (Ps: 一个棋子也可以看作是一个集合, 也就是说集合非空)
题解
暴力枚举上下就可以了。
反正我是开了ull
的。
代码
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 50 + 2;
ull a[N][N], cnt, ans, x = 1;
int n, m;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i ++) {
cnt = 0;
for (int j = 0; j < m; j ++) {
cin >> a[i][j];
if (a[i][j]) cnt ++;
}
ans += (x << cnt) + (x << (m - cnt)) - 2;
}
for (int j = 0; j < m; j ++) {
cnt = 0;
for (int i = 0; i < n; i ++)
if (a[i][j]) cnt ++;
ans += (x << cnt) + (x << (n - cnt)) - 2;
}
cout << ans - n * m << endl;
return 0;
}
C-Removing Columns
题目大意
给你一个n×mn\times mn×m的小写字符矩阵,求删除最少的列,使剩下的字符矩阵的每一行的字符串从上到下的字典序非减。
题解
依旧是暴力枚举,如果需要删除,那么就删除掉。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 3;
char s[N][N];
int a[N];
int n, m, ans = 0;
bool fg = 0;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i ++)
scanf("%s", s[i]);
for (int i = 0; i < m; i ++) {
for (int j = 0; j < n; j ++)
if (s[j][i] < s[j - 1][i] && !a[j])fg = 1;
if (fg) ans ++;
else for (int j = 0; j < n; j ++)
if (s[j][i] > s[j - 1][i]) a[j] = 1;
fg = 0;
}
cout << ans << endl;
return 0;
}
D-Woodcutters
题目大意
在一个数轴上有nnn棵树,每一个树独自占据一个点,每一次砍倒一棵树,你可以选择让这个树向左倒还是向右倒,但是不能砸到其他的树。
求最多可以砍多少棵树。
题解
很明显的贪心,从左到右遍历,如果可以向右倒,那么就向右倒,否则判断是否可以向左倒,每一次记录上一次的倒的情况。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int x[N], h[N];
int n, ans, x1 = -inf, h1 = 0;
int main() {
scanf("%d", &n); ans = n;
for (int i = 1; i <= n; i ++) scanf("%d%d", &x[i], &h[i]);
for (int i = 1; i <= n; i ++) {
if (x1 + h1 >= x[i]) -- ans, h1 = 0;
if (x[i] - h[i] > x1 + h1) h[i] = 0;
h1 = h[i]; x1 = x[i];
}
cout << ans << endl;
return 0;
}
E-Bear and Prime 100
题意
你可以询问最多202020次,系统会回答你,你询问的这个数是否是系统给定这个数的约数。
判断系统给出的数是质数还是合数。
数的范围是x∈[2,100]x\in[2,100]x∈[2,100]
题解
如果我们询问的这个数是一个质数,而且系统判断是yes
,如果这个次数出现两次,则说明那么这个数就不是一个质数。
那么只需要判断到[2,50][2,50][2,50]的范围就可以了。
但是有几种情况无法判断就是质数的平方,特判以下的444个数据就可以了,[4(22),9(32),25(52),49(72)][4(2^2),9(3^2),25(5^2),49(7^2)][4(22),9(32),25(52),49(72)]。
以上这几个数很明显也不是质数。
代码
#include <bits/stdc++.h>
using namespace std;
const int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 4, 9, 25, 49};
string s;
int cnt = 0, i = 0;
int main() {
while (i < 19) {
cout << prime[i ++] << '\n';
cin >> s;
if (s == "yes") cnt ++;
}
cout << (cnt < 2 ? "prime" : "composite");
return 0;
}