Codeforces Round 898 (Div. 4)
A. Short Sort
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
for( string s ; t ; t -- ){
cin >> s;
if( s == "abc" or s == "acb" or s == "cba" or s == "bac" )
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
B. Good Kid
最小值加一
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n , res = 1 ;
cin >> n;
vector<int> a(n);
for( auto &i : a )
cin >> i;
sort( a.begin(), a.end() );
a[0] ++;
for( auto i : a ) res *= i;
cout << res << "\n";
return;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
for( cin >> t; t ; t -- )
solve();
return 0;
}
C. Target Practice
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<string> p = {"1111111111",
"1222222221",
"1233333321",
"1234444321",
"1234554321",
"1234554321",
"1234444321",
"1233333321",
"1222222221",
"1111111111"};
void solve() {
string s;
int res = 0;
for (int i = 0; i < 10; i++) {
cin >> s;
for (int j = 0; j < 10; j++) {
if (s[j] == 'X')
res += p[i][j] - '0';
}
}
cout << res << "\n";
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
for (cin >> t; t; t--)
solve();
return 0;
}
D. 1D Eraser
简单dp
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, k;
string s;
cin >> n >> k >> s;
vector<int> a(n + 1), f(n + 1, INT_MAX);
for (int i = 1; i <= n; i++)
a[i] = a[i - 1] + (s[i - 1] == 'B');
f[0] = 0;
for (int i = 1; i < k ; i++) {
if (a[i] == 0) f[i] = 0;
else f[i] = 1;
}
for (int i = k; i <= n; i++) {
if (a[i] == a[i - 1]) f[i] = min(f[i], f[i - 1]);
f[i] = min(f[i], f[i - k] + (a[i] > a[i - k]));
}
cout << f[n] << "\n";
return;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
for (cin >> t; t; t--)
solve();
return 0;
}
E. Building an Aquarium
二分答案
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (auto &i: a) cin >> i;
auto check = [m, a](int h) {
int cnt = 0;
for (int i: a) cnt += max(0ll, h - i);
return cnt <= m;
};
int l = 0, r = 2e9 + 5, res = 0;
for (int mid; l <= r;) {
mid = (l + r) / 2;
if (check(mid)) res = mid, l = mid + 1;
else r = mid - 1;
}
cout << res << "\n";
return;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
for (cin >> t; t; t--)
solve();
return 0;
}
F. Money Trees
首先预处理每个点向后最远能到达的位置,然后前缀和维护一下果子的数量,最后二分长度即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, k;
cin >> n >> k;
vector<int> a(n + 1), h(n + 1), lst(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) a[i] += a[i - 1];
for (int i = 1, t; i <= n; i = t) {
for (t = i + 1; t <= n; t++)
if (h[t - 1] % h[t] != 0) break;
for (int j = i; j < t; j++)
lst[j] = t - 1;
}
auto check = [k,n,a,lst]( int x ){
for( int l = 1 , r = x ; r <= n ; l ++ , r ++ ){
if( r > lst[l] ) continue;
if( a[r] - a[l-1] <= k ) return true;
}
return false;
};
int l = 1, r = n, res = 0;
for (int mid; l <= r;) {
mid = ( l + r ) / 2 ;
if( check(mid) ) res = mid , l = mid + 1;
else r = mid - 1;
}
cout << res << "\n";
return;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
for (cin >> t; t; t--)
solve();
return 0;
}
G. ABBC or BACB
一个B可以把两侧的A全部消掉,所以只有当B的数量等于A的段数减一时,会有一段A无法被消掉,此时舍弃最小的一段。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
using vi = vector<int>;
void solve() {
string s;
cin >> s , s += 'B';
int cntB = -1, res;
vector<int> a;
for (int t = 0; auto i: s) {
if (i == 'A') t++;
else {
cntB++;
if (t > 0) a.push_back(t);
t = 0;
}
}
res = accumulate(a.begin(), a.end(), 0);
if (cntB == a.size() - 1) {
sort(a.begin(), a.end());
res -= a.front();
}
cout << res << "\n";
return;
}
int32_t main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int t;
for (cin >> t; t; t--)
solve();
return 0;
}
H. Mad City
题目保证联通,所以图一定是基环树,要想实现永远追不上,这 b 要先跑到环上,所以a 要赶在b 到环上之前把他截住。所以找到 b 所在链和环的交点,判断谁先到交点即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
constexpr int inf = 1e9;
void solve() {
int n, a, b;
cin >> n >> a >> b;
vector<vi> e(n + 1);
vi deg(n + 1), vis(n + 1);
for (int i = 1, u, v; i <= n; i++) {
cin >> u >> v;
e[u].push_back(v), e[v].push_back(u);
deg[u]++, deg[v]++;
}
queue<int> q;
for (int i = 1; i <= n; i++)
if (deg[i] == 1) q.push(i), vis[i] = 1;
for (int u; !q.empty();) {
u = q.front(), q.pop();
for (auto v: e[u]) {
if (vis[v]) continue;
if (--deg[v] == 1) vis[v] = 1, q.push(v);
}
}
auto bfs = [e, n](int u) {
vi dis(n + 1, inf);
queue<int> q;
q.push(u), dis[u] = 0;
for (int x; !q.empty();) {
x = q.front(), q.pop();
for (auto y: e[x]) {
if (dis[y] <= dis[x] + 1) continue;
dis[y] = dis[x] + 1, q.push(y);
}
}
return dis;
};
vi d = bfs(b);
int t = -1;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
if (t == -1 or d[i] < d[t]) t = i;
}
d = bfs(t);
if( d[a] <= d[b] ) cout << "NO\n";
else cout << "YES\n";
return;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
for (cin >> t; t; t--)
solve();
return 0;
}