9.17
Wonderful_Anser · · 个人记录
CF2211A
赛时代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 1, x; i <= n; i++) cin >> x;
if (n < 3) cout << n;
else for (int i = 1; i <= n; i++) cout << 2 << " ";
cout << "\n";
}
return 0;
}
很容易看出,我特判的地方有几个问题:
- 只有n等于1的时候才需要特判输出1
- 其他时候直接输出n个2即可
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; for (int i = 1, x; i <= n; i++) cin >> x; if (n < 2) cout << 1; else for (int i = 1; i <= n; i++) cout << 2 << " "; cout << "\n"; } return 0; }
CF2152B
- 核心思路:Krug的最优策略是背向追逐者直线冲向边界
- 若yk < yd,向0边界跑得yd步;若yk > yd,向n边界跑得n - yd步;坐标重合则dy = 0
- 若xk < xd,向0边界跑得xd步;若xk > xd,向n边界跑得n - xd步;坐标重合则dx = 0
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, xk, yk, xd, yd;
cin >> n >> xk >> yk >> xd >> yd;
int dx = 0, dy = 0;
if (xk < xd) dx = xd;
else if (xk > xd) dx = n - xd;
if (yk < yd) dy = yd;
else if (yk > yd) dy = n - yd;
cout << max(dx, dy) << "\n";
}
int main() {
int T;
cin >> T;
while (T--) solve();
return 0;
}
CF1506G
-
题意:给定T组数据,每次输入字符串s,将字符串删除重复的元素,让每个字符串有的元素都只出现一次,求出字典序最大的一个字符串
-
一个字符串的字典序只有一个所以答案具有唯一性
-
贪心从左往右构造目标字符串t,因为越往左越大,t也就越大,假设已经是第i位了,已经构造的字符串为t,当 s[i]>t[t.size()-1],且有大于1个,换交换,直到:
- 栈为空
- t末尾的字典序更大
- t末尾的字符之后不出现
-
用t末尾的字符,栈
-
找后面有没有字符可以预处理优化
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; bool vis[N]; int last[N]; int main() { int T; cin >> T; while (T--) { string s; cin >> s; memset(vis, false, sizeof vis); memset(last, false, sizeof last); int n = s.size(); for (int i = 0; i < n; i++) last[s[i] - 'a'] = i; string stk; for (int i = 0; i < n; i++) { char c = s[i]; if (vis[c - 'a']) continue; while (stk.size() && stk.back() < c && last[stk.back() - 'a'] > i) { vis[stk.back() - 'a'] = false; stk.pop_back(); } stk.push_back(c); vis[c - 'a'] = true; } cout << stk << "\n"; } return 0; }