MOO
meaning:
给你T组字符串,只包含’M’和’O’,你可以删除首尾的数字或反转他们(M变O,O变M),如果可以通过这两个操作将字符串变成”MOO”就输出需要的最少步数,不能就输出-1
idea:
直接对子串进行枚举,计算操作需要的次数并找到最小值,如果字符串长度=3时,那中间字符必须为O,因为我们不能修改中间,只能修改两边(也就是想办法把中间的字符变成O)否则输出-1
code:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int q, ans;
string s;
int main() {
cin >> q;
while(q--){
cin >> s;
ans = INF;
for (int i = 2; i < s.size(); i++) {
if (s[i - 1] == 'O') {
ans = min(ans, (s[i - 2] != 'M') + (s[i] != 'O'));
}
}
cout << (ans == INF ? -1 : ans + (int)s.size() - 3) << '\n';
}
return 0;
}HUNGRY
meaning:
有N天给奶牛送餐,第d[i]天会送b[i]捆干草。从第1天开始,有干草时就每天吃一捆,求前T天吃多少捆干草。
idea:
要么剩下的干草都被吃完然后没干草吃,要么有足够的干草每天吃一捆,对这两种情况进行判断,计算出吃掉的干草数量。如果下次送餐晚于这个时间则更新到送餐时间,期间没有干草吃,再累加有干草吃的天数。
code:
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, t, d, b;
cin >> n >> t;
int ans = 0, cnt = 0, temp = 0;
for(int i = 1; i <= n; i++){
cin >> d >> b;
if(cnt - d + temp >= 0){
ans += d - temp;
cnt -= d - temp;
}
else{
ans += cnt;
cnt = 0;
}
temp = d;
cnt += b;
}
if(cnt - (t - temp + 1) >= 0){
ans += (t - temp + 1);
}
else{
ans += cnt;
}
cout << ans;
return 0;
}HPS
meaning:
给定N种手势and胜负关系,A和B猜拳,同时出两个手势再同时保留一个,对于M局游戏给定B的手势组合,求A获胜手势组合数量
idea:
a只要留下必胜手势就可以赢了,否则胜负情况只可能有两种一个必败一个必赢,留下必赢的手势;两个手势都可能败,随机留下一个,A只有留下相应手势才能获胜,他也不必胜
code:
#include <bits/stdc++.h>
using namespace std;
const int N = 3001;
int a[N][N], n, m;
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
char c;
cin >> c;
a[i][j] = (c != 'L') + (c == 'W') - 1, a[j][i] = -a[i][j];
}
}
for (int i = 1, x, y; i <= m; i++) {
cin >> x >> y;
int c = 0;
for (int j = 1; j <= n; j++) {
c += a[j][x] == 1 && a[j][y] == 1;
}
cout << c * n * 2 - c * c << '\n';
}
return 0;
}FARM
meaning:
N株植物,
idea:
code:
FEB
meaning:
idea:
code: