文章目录
Codeforces Round #691 (Div. 2)
A. Red-Blue Shuffle
题意:
题解:
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const MAXN = 1e3 + 10;
int m, T;
int t;
char a[MAXN], b[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
int f1 = 0, f2 = 0;
for (int i = 0; i < n; i++) {
if (a[i] > b[i]) f1++;
if (a[i] < b[i]) f2++;
}
if (f1 > f2)
cout << "RED" << endl;
else if (f1 < f2)
cout << "BLUE" << endl;
else
cout << "EQUAL" << endl;
}
return 0;
}
B. Move and Turn
题意:
题解: bfs打表
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const MAXN = 2e5 + 10;
int n, m, T;
int f[2][4] = {0, 0, 1, -1, 1, -1, 0, 0}; //down up right left
struct node
{
int x, y, step, f;
};
set<pair<int, int>> st[MAXN];
queue<node> q;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
if (n & 1)
{
cout << ((n + 1) / 2 + 1) * ((n + 1) / 2 + 1) + ((n - 1) / 2 + 1) * ((n - 1) / 2 + 1) - 1;
}
else
{
cout << (n / 2 + 1) * (n / 2 + 1) << endl;
}
/*
q.push({0,1,1,0});
q.push({0,-1,1,1});
q.push({1,0,1,2});
q.push({-1,0,1,3});
while(!q.empty()){
node now =q.front();
q.pop();
int x=now.x,y=now.y,f=now.f,step=now.step;
if(step>=20)break;
st[step].insert({x,y});
if(f==0){
node ne=now;
ne.f=2;
ne.step=step+1;
ne.x+=1;
q.push(ne);
ne=now;
ne.f=3;
ne.step=step+1;
ne.x-=1;
q.push(ne);
}
if(f==1){
node ne=now;
ne.f=2;
ne.step=step+1;
ne.x+=1;
q.push(ne);
ne=now;
ne.f=3;
ne.step=step+1;
ne.x-=1;
q.push(ne);
}
if(f==2){
node ne=now;
ne.f=0;
ne.step=step+1;
ne.y-=1;
q.push(ne);
ne=now;
ne.f=1;
ne.step=step+1;
ne.y+=1;
q.push(ne);
}
if(f==3){
node ne=now;
ne.f=0;
ne.step=step+1;
ne.y-=1;
q.push(ne);
ne=now;
ne.f=1;
ne.step=step+1;
ne.y+=1;
q.push(ne);
}
}
for(int i=1;i<=15;i++)cout<<st[i].size()<<endl;*/
return 0;
}
C. Row GCD
题意:
题解: 辗转相减法
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const MAXN = 2e5 + 10;
int n, m, T;
LL a[MAXN], b[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
LL pregcd = 0;
for (int i = 0; i < n - 1; i++) {
pregcd = __gcd(pregcd, a[i] - a[n - 1]);
}
for (int i = 0; i < m; i++) {
cout << llabs(__gcd(pregcd, a[n - 1] + b[i])) << ' ';
}
return 0;
}
D. Glass Half Spilled
题意: n个杯子,容量ai,已有bi的水。每次往其他杯子倒水会损失倒出水一半,问你最终只留k个杯子有水,其他杯子的水都往这k个杯子里倒,问每个k最终保留的水最大是多少。
题解: 01背包+滚动数组。 f ( i , k , S a ) f(i,k,S_a) f(i,k,Sa)表示从前 i i i个杯子中选取 k k k个杯子,总容量为 S a S_a Sa时的最大现有水量,转移就是一个背包问题, f ( i , k , S a ) = m a x ( f ( i − 1 , k , S a ) , f ( i − 1 , k − 1 , S a − a i ) + b i ) f(i, k, S_a) = max(f(i-1,k,S_a), f(i-1,k-1,S_a-a_i)+b_i) f(i,k,Sa)=max(f(i−1,k,Sa),f(i−1,k−1,Sa−ai)+bi)
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;
int an[MAXN], bn[MAXN];
int f[2][MAXN][MAXN * MAXN];
int main(void) {
int n, tot1 = 0, tot2 = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> an[i] >> bn[i];
tot2 += bn[i];
}
memset(f, 128, sizeof(f));
f[0][0][0] = 0;
for (int i = 1; i <= n; i++) {
tot1 += an[i];
for (int j = 0; j <= i; j++) {
for (int k = 0; k <= tot1; k++) {
f[i & 1][j][k] = f[(i - 1) & 1][j][k];
if (j && k >= an[i])
f[i & 1][j][k] =
max(f[i & 1][j][k],
f[(i - 1) & 1][j - 1][k - an[i]] + bn[i]);
}
}
}
for (int i = 1; i <= n; i++) {
double res = 0.0;
for (int j = 0; j <= tot1; j++) {
res = max(res, min(1.0 * j, f[n & 1][i][j] / 2.0 + tot2 / 2.0));
}
printf("%.10f ", res);
}
return 0;
}