23
2023/9/17
birthday
加载过慢请开启缓存,浏览器默认开启
CF 1680E
void solve() {
int n;
cin >> n;
int l = 1, r = n;
vector<vector<int>> a(n + 2, vector<int>(3)), dp(a);
for (int j = 0; j < 2; ++j) {
for (int i = 1; i <= n; ++i) {
char c;
cin >> c;
a[i][j] = (c == '*' ? 1 : 0);
}
}
while (!a[l][0] && !a[l][1]) ++l;
while (!a[r][0] && !a[r][1]) --r;
for (int i = l; i <= r; ++i) {
dp[i][0] = min(dp[i - 1][0] + 1 + a[i][1], dp[i - 1][1] + 2);
dp[i][1] = min(dp[i - 1][1] + 1 + a[i][0], dp[i - 1][0] + 2);
}
cout << min(dp[r][0], dp[r][1]) - 1 << '\n';
}
CF 1728D
void solve() {
string s;
cin >> s;
int n = s.size();
s = ')' + s;
auto check = [&](char x, char y) {
if (x > y) return 1;
if (x < y) return -1;
return 0;
};
vector dp(n + 1, vector<int>(n + 1));
for (int i = 1; i < n; ++i) {
dp[i][i + 1] = (s[i] == s[i + 1]) ? 0 : 1;
}
int f1, f2, f3, f4;
for (int len = 4; len <= n; len += 2) {
for (int l = 1, r = len; r <= n; ++l, ++r) {
dp[l][r] = -1;
// case 1: Alice pick up s[l], Bob pick up s[l + 1]
f1 = dp[l + 2][r] == 0 ? check(s[l], s[l + 1]) : dp[l + 2][r];
// case 2: Alice pick up s[l], Bob pick up s[r]
f2 = dp[l + 1][r - 1] == 0 ? check(s[l], s[r]) : dp[l + 1][r - 1];
// case 1: Alice pick up s[r], Bob pick up s[r - 2]
f3 = dp[l][r - 2] == 0 ? check(s[r], s[r - 1]) : dp[l][r - 2];
// case 1: Alice pick up s[r], Bob pick up s[l]
f4 = dp[l + 1][r - 1] == 0 ? check(s[r], s[l]) : dp[l + 1][r - 1];
dp[l][r] = max(min(f1, f2), min(f3, f4));
}
}
if (dp[1][n] == 1) {
cout << "Alice" << '\n';
} else if (dp[1][n] == -1) {
cout << "Bob" << '\n';
} else {
cout << "Draw" << '\n';
}
return;
}
CF 721C
void solve() {
int n, m, T;
cin >> n >> m >> T;
vector<vector<int>> E(n + 1), cost(n + 1);
vector<vector<int>> dp(n + 1, vector<int>(n + 1, INF)), pre(dp);
vector<int> in(n + 1);
for (int i = 1; i <= m; ++i) {
int u, v, t;
cin >> u >> v >> t;
E[u].push_back(v);
cost[u].push_back(t);
in[v]++;
}
queue<int> q;
for (int v = 1; v <= n; ++v) {
if (!in[v]) {
q.push(v);
}
}
pre[1][1] = -1;
dp[1][1] = 0;
// dp[i][j] means the answer when passes j points and the current point is i
while (!q.empty()) {
auto u = q.front();
q.pop();
for (int i = 0; i < E[u].size(); ++i) {
int v = E[u][i], c = cost[u][i];
--in[v];
if (!in[v]) {
q.push(v);
}
for (int t = 2; t <= n; ++t) {
if (dp[v][t] > dp[u][t - 1] + c) {
dp[v][t] = dp[u][t - 1] + c;
pre[v][t] = u;
}
}
}
}
function<void(int, int)> print_trace = [&] (int cur, int t) {
if (pre[cur][t] == -1) {
cout << cur << ' ';
return;
}
print_trace(pre[cur][t], t - 1);
cout << cur << ' ';
};
// reverse iteration
for (int i = n; i >= 1; --i) {
if (dp[n][i] <= T) {
cout << i << '\n';
print_trace(n, i);
break;
}
}
return;
}
CF 453 A
void solve() {
int m, n;
cin >> m >> n;
long double ans = 0;
for (double i = 1; i <= m; i += 1) {
ans += i * (pow(i / m, n) - pow((i - 1) / m, n));
}
cout << ans << '\n';
return ;
}
CF 721C
void solve() {
int n, m, T;
cin >> n >> m >> T;
vector<vector<int>> E(n + 1), cost(n + 1);
vector<vector<int>> dp(n + 1, vector<int>(n + 1, INF)), pre(dp);
vector<int> in(n + 1);
for (int i = 1; i <= m; ++i) {
int u, v, t;
cin >> u >> v >> t;
E[u].push_back(v);
cost[u].push_back(t);
in[v]++;
}
queue<int> q;
for (int v = 1; v <= n; ++v) {
if (!in[v]) {
q.push(v);
}
}
pre[1][1] = -1;
dp[1][1] = 0;
// dp[i][j] means the answer when passes j points and the current point is i
while (!q.empty()) {
auto u = q.front();
q.pop();
for (int i = 0; i < E[u].size(); ++i) {
int v = E[u][i], c = cost[u][i];
--in[v];
if (!in[v]) {
q.push(v);
}
for (int t = 2; t <= n; ++t) {
if (dp[v][t] > dp[u][t - 1] + c) {
dp[v][t] = dp[u][t - 1] + c;
pre[v][t] = u;
}
}
}
}
function<void(int, int)> print_trace = [&] (int cur, int t) {
if (pre[cur][t] == -1) {
cout << cur << ' ';
return;
}
print_trace(pre[cur][t], t - 1);
cout << cur << ' ';
};
// reverse iteration
for (int i = n; i >= 1; --i) {
if (dp[n][i] <= T) {
cout << i << '\n';
print_trace(n, i);
break;
}
}
return;
}
CF 940E
void solve() {
int n, c;
cin >> n >> c;
vector<int> a(n + 1);
ll sum = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
sum += a[i];
}
deque<int> q;
vector<ll> dp(n + 1);
for (int i = 1; i <= n; ++i) {
// we maintain a queue to store the increasing queue
while(q.size() && a[q.back()] >= a[i]) q.pop_back();
q.push_back(i);
while(q.size() && i - q.front() >= c) q.pop_front();
if(i >= c) dp[i] = max(dp[i - 1], dp[i - c] + a[q.front()]);
}
cout << sum - dp[n] << '\n';
return ;
}
CF 580D
void solve() {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n);
vector<vector<ll>> dp(n,
vector<ll>(1 << n)); // the status is sta and ends in i
for (int i = 0; i < n; ++i) {
cin >> a[i];
dp[i][1 << i] = a[i];
}
int u, v, c;
vector<vector<int>> mp(n, vector<int>(n));
for (int i = 0; i < k; ++i) {
cin >> u >> v >> c;
--u, --v;
mp[u][v] = c;
}
for (int sta = 0; sta < (1 << n); ++sta) {
for (int i = 0; i < n; ++i) {
if (!(sta & (1 << i))) continue;
for (int j = 0; j < n; ++j) {
if (i == j || !(sta & (1 << j))) continue;
dp[i][sta] = max(dp[i][sta], dp[j][sta ^ (1 << i)] + mp[j][i] + a[i]);
}
}
}
ll ans = 0;
for (int i = 0; i < (1 << n); ++i) {
if (__builtin_popcount(i) != m) {
continue;
}
for (int j = 0; j < n; ++j) {
ans = max(ans, dp[j][i]);
}
}
cout << ans << '\n';
return;
}
CF 295C
#define total(x, y) ((x * 50 + y * 100))
ll A[N][N][N][N];
ll C[N][N];
ll dp[N << 2][N][N];
void solve() {
ll n, lim;
cin >> n >> lim;
vector<int> a(n + 1);
int cnta = 0, cntb = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] == 50) {
cnta++;
} else {
cntb++;
}
}
for (int i = 0; i < N; ++i) {
for (int j = C[i][0] = 1; j <= i; ++j) {
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
}
}
for (int a = 0; a < N; ++a) {
for (int b = 0; b < N; ++b) {
for (int c = 0; c <= a; ++c) {
for (int d = 0; d <= b; ++d) {
A[a][b][c][d] = (C[a][c] * C[b][d]) % MOD;
}
}
}
}
dp[0][cnta][cntb] = 1;
for (int i = 1; i <= 4 * n + 1; i += 2) {
ll res = 0;
// positive direction
for (int x = 0; x <= cnta; ++x) {
for (int y = 0; y <= cntb; ++y) {
if (dp[i - 1][x][y]) {
for (int a = 0; a <= x; ++a) {
for (int b = 0; b <= y; ++b) {
if ((a | b) && (total(a, b) <= lim)) {
dp[i][x - a][y - b] +=
(A[x][y][a][b] * dp[i - 1][x][y]);
dp[i][x - a][y - b] %= MOD;
}
}
}
}
}
}
if (dp[i][0][0]) {
cout << i << '\n' << dp[i][0][0] << '\n';
return;
}
// negative direction
for (int x = 0; x <= cnta; ++x) {
for (int y = 0; y <= cntb; ++y) {
if (dp[i][x][y]) {
for (int a = 0; a <= cnta - x; ++a) {
for (int b = 0; b <= cntb - y; ++b) {
if ((a | b) && (total(a, b) <= lim)) {
dp[i + 1][x + a][y + b] +=
(A[cnta - x][cntb - y][a][b] * dp[i][x][y]);
dp[i + 1][x + a][y + b] %= MOD;
}
}
}
}
}
}
}
cout << -1 << '\n' <<0 <<'\n';
return;
}
CF 59A
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
while(cin>>s){
int low=0,up=0;
for(int i=0;i<s.length();++i){
if(s[i]>=65&&s[i]<=90) ++up;
else if(s[i]>=97&&s[i]<=122) ++low;
}
if(low>=up){
for(int i=0;i<s.length();++i){
if(s[i]>=65&&s[i]<=90)
s[i]+=32;
}
}
else{
for(int i=0;i<s.length();++i){
if(s[i]>=97&&s[i]<=122)
s[i]-=32;
}
}
cout<<s<<'\n';
}
return 0;
}