练习2题解
A题:
#include <bits/stdc++.h> #define I_can_AK int main using uint = unsigned int; using ll = long long; using ull = unsigned long long; using i128 = __int128; const int N = 1e6 + 5; void Solve(){ int a, b; std::cin >> a >> b; if(a >= 0 && b >= 0){ if((a+b)%2==0 && std::abs(a-b)<=1){ std::cout << "NO" << "\n"; } else if(std::abs(a-b)>1){ std::cout << "PING" << "\n"; } else{ std::cout << "YES" << "\n"; } }else{ std::cout << "PING" << "\n"; } return ; } I_can_AK(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; std::cin >> T; while(T--) Solve(); return 0; }
B题:
#include <bits/stdc++.h> #define I_can_AK int main using uint = unsigned int; using ll = long long; using ull = unsigned long long; using i128 = __int128; using pii = std::pair<int, int>; std::string s, a; void Solve(){ std::cin >> s >> a; int n = s.size(), m = a.size(); s = s+s; int ans = 0; for(int i = 0; i < n; i++){ int cnt = 0; for(int j = 0; j < m; j++){ if(s[i+j]==a[j]) cnt++; } if(cnt==m) ans++;// 完全相同 } std::cout << ans << "\n"; return ; } I_can_AK(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; //std::cin >> T; while(T--) Solve(); return 0; }
C题:
#include <bits/stdc++.h> #define I_can_AK int main using uint = unsigned int; using ll = long long; using ull = unsigned long long; using i128 = __int128; using pii = std::pair<int, int>; void Solve(){ //初始化 std::string key; std::cin >> key; //穷举 int ret = 1e9; for (int i = 0; i < 26; i++) //目标元素 { int r = 0; //本轮需要转化的次数 //如果存在 for (long long j = 0; j < key.size(); j++) //转化字符 { r += std::min(std::abs((key[j] - 'a') - i), 26 - std::abs((key[j] - 'a') - i)); } //取最小值 ret = std::min(ret, r); } //输出 std::cout << ret; return ; } I_can_AK(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; //std::cin >> T; while(T--) Solve(); return 0; }
D题:
#include <bits/stdc++.h> #define I_can_AK int main using uint = unsigned int; using ll = long long; using ull = unsigned long long; using i128 = __int128; using pii = std::pair<int, int>; const int N = 505; int n, k, m; std::string s; void Solve(){ int n, m, k; std::cin >> n >> m >> k; int board[N][N] = {0}; // 棋盘,记录领土标号 int cnt = 0; // 标号计数 std::map<std::string, pii> pos; // 记录军队位置 std::map<std::string, int> power; // 记录军队战力(即领土数量) std::map<std::string, std::vector<int>> domain; // 记录军队占领领土的标号 std::map<int, std::string> belong; // 记录领土归属 while (k--){ std::string name; int x, y; std::cin >> name >> x >> y; if (board[x][y] == 0){// 投放位置为空地 board[x][y] = ++cnt; belong[cnt] = name; pos[name] = {x, y}; power[name] = 1; domain[name].push_back(cnt); } else if (belong[board[x][y]] < name){// 投放位置有弱于自己的军队 std::string old = belong[board[x][y]]; pos.erase(old); power.erase(old); domain.erase(old); belong[board[x][y]] = name; pos[name] = {x, y}; power[name] = 1; domain[name].push_back(board[x][y]); } } std::map<char, pii> move = {{'W', {-1, 0}}, {'S', {1, 0}}, {'A', {0, -1}}, {'D', {0, 1}}}; int q; std::cin >> q; while (q--){ std::string name; char op; std::cin >> name >> op; // 势力不存在或已消亡 if (pos.find(name) == pos.end()){ std::cout << "unexisted empire." << "\n"; continue; } pii cur = pos[name]; pii nxt = {cur.first + move[op].first, cur.second + move[op].second}; // 目标点越界 if (nxt.first < 1 || nxt.first > n || nxt.second < 1 || nxt.second > m){ std::cout << "out of bounds!" << "\n"; continue; } // 目标点为空地 if (board[nxt.first][nxt.second] == 0){ std::cout << "vanquish!" << "\n"; board[nxt.first][nxt.second] = board[cur.first][cur.second]; pos[name] = nxt; power[name]++; domain[name].push_back(board[nxt.first][nxt.second]); continue; } // 目标点为自己的领土 else if (belong[board[nxt.first][nxt.second]] == name){ std::cout << "peaceful." << "\n"; pos[name] = nxt; continue; } // 目标点为敌方领土 else{ std::string enemy = belong[board[nxt.first][nxt.second]]; // 我方战胜敌方 if (power[name] > power[enemy] || (power[name] == power[enemy] && name > enemy)){ std::cout << name << " wins!" << "\n"; pos.erase(enemy); power[name] += power[enemy]; power.erase(enemy); while (!domain[enemy].empty()) // 敌方领土归我方所有 { int tmp = domain[enemy].back(); domain[enemy].pop_back(); belong[tmp] = name; domain[name].push_back(tmp); } domain.erase(enemy); board[nxt.first][nxt.second] = board[cur.first][cur.second]; pos[name] = nxt; continue; } // 我方战败 else{ std::cout << enemy << " wins!" << "\n"; pos.erase(name); power[enemy] += power[name]; power.erase(name); while (!domain[name].empty()) // 我方领土归敌方所有 { int tmp = domain[name].back(); domain[name].pop_back(); belong[tmp] = enemy; domain[enemy].push_back(tmp); } domain.erase(name); continue; } } } return ; } I_can_AK(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; //std::cin >> T; while(T--) Solve(); return 0; }
E题:
#include <bits/stdc++.h> #define I_can_AK int main using uint = unsigned int; using ll = long long; using ull = unsigned long long; using i128 = __int128; using pii = std::pair<int, int>; int n, k; int a[10]; void cal(int op1, int op2){ if(op1==0){ int h = std::max(a[op2],a[op2+1]); a[op2] = h+3; a[op2+1] = h+1; } if(op1==90){ int h = std::max({a[op2]+2,a[op2+1]+1,a[op2+2]+1}); a[op2] = h; a[op2+1] = h; a[op2+2] = h; } if(op1==180){ int h = std::max(a[op2]+1,a[op2+1]+3); a[op2] = h; a[op2+1] = h; } if(op1==270){ int h = std::max({a[op2],a[op2+1],a[op2+2]}); a[op2] = h+1; a[op2+1] = h+1; a[op2+2] = h+2; } } void Solve(){ int n; std::cin >> n; while(n--){ int op1, op2; std::cin >> op1 >> op2; cal(op1,op2); } for(int i=1;i<=8;i++){ std::cout<< a[i] << " "; } } I_can_AK(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; //std::cin >> T; while(T--) Solve(); return 0; }
F题:
#include <bits/stdc++.h> #define I_can_AK int main using uint = unsigned int; using ll = long long; using ull = unsigned long long; using i128 = __int128; const int N = 1e6 + 5; void Solve(){ int n; char v[200]; std::cin >> n >> v; std::cout << std::max(v[0], v[n-1]); return ; } I_can_AK(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; //std::cin >> T; while(T--) Solve(); return 0; }