【AtCoder】Yahoo Programming Contest 2019

时间:2021-05-16 16:57:19

A - Anti-Adjacency

K <= (N + 1) / 2

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 40005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,K;
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(N);read(K);
if(K <= (N + 1) / 2) puts("YES");
else puts("NO");
}

B - Path

点度都不超过3

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 40005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int deg[5];
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
int a,b;
for(int i = 1 ; i <= 3 ; ++i) {
read(a);read(b);
++deg[a];++deg[b];
}
for(int i = 1 ; i <= 4 ; ++i) {
if(deg[i] >= 3) {puts("NO");return 0;}
}
puts("YES");
}

C - When I hit my pocket...

先给自己加到A块饼干

然后判断一下B - A是否大于2,然后就用两次不断的换B - A

否则就直接一直拍得一次饼干就行

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 40005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int64 K,A,B;
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(K);read(A);read(B);
int64 now = 1;
if(now < A) {
int64 t = min(A - now,K);
now += t;
K -= t;
}
if(B - A > 2) {
now += K / 2 * (B - A);
K %= 2;
}
now += K;
out(now);enter;
}

D - Ears

DEF我做题顺序是反的,D题我做的有点智障

FE做完之后一开榜19,D做完掉到45

大家AK水平那么高= =

事实上就是一个有点智障的dp

空 偶 奇 偶 空

每个段都可以为0,如果一个数在空段代价是它的值,奇数在偶段代价为1,偶数在奇数段代价为1,0在偶数段代价为2

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int L;
int64 dp[MAXN][6],A[MAXN];
void Solve() {
read(L);
for(int i = 1 ; i <= L ; ++i) {read(A[i]);}
for(int i = 1 ; i <= 4 ; ++i) dp[0][i] = 1e16;
dp[0][0] = 0;
for(int i = 1 ; i <= L ; ++i) {
dp[i][0] = dp[i - 1][0] + A[i];
int t = A[i] & 1;
dp[i][1] = min(dp[i - 1][1],dp[i - 1][0]); dp[i][2] = min(min(dp[i - 1][1],dp[i - 1][0]),dp[i - 1][2]);
if(A[i] % 2 == 0) dp[i][2]++;
dp[i][3] = min(dp[i - 1][2],dp[i - 1][3]);
if(A[i] & 1) {dp[i][1]++;dp[i][3]++;}
if(A[i] == 0) {dp[i][1] += 2;dp[i][3] += 2;}
dp[i][4] = min(min(dp[i - 1][3],dp[i - 1][2]),min(dp[i - 1][1],dp[i - 1][4]));
dp[i][4] += A[i];
}
int64 res = dp[L][0];
for(int i = 1 ; i <= 4 ; ++i) res = min(res,dp[L][i]);
out(res);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

E - Odd Subrectangles

如果选的行数确定了,那么选某一列的奇偶性也会确定,如果奇数有a列,偶数有b列,\(a + b = M\)

那么方案数是选a中奇数个,b随便选

方案数是\(2^{a - 1}\cdot 2^{b}\),发现这个就是\(2^{M - 1}\)

所以只要存在a即可,即选的行数的数异或起来值不为0,转而求异或为0的方案,直接上线性基即可

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 40005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353;
int N,M;
int a[305][305];
int b[305][305],cnt;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Solve() {
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= M ; ++j) {
read(a[i][j]);
}
}
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= M ; ++j) {
if(a[i][j]) {
if(!b[j][j]) {
for(int k = 1 ; k <= M ; ++k) b[j][k] = a[i][k];
++cnt;
break;
}
else {
for(int k = 1 ; k <= M ; ++k) a[i][k] ^= b[j][k];
}
}
}
}
int ans = inc(fpow(2,N),MOD - fpow(2,N - cnt));
ans = mul(ans,fpow(2,M - 1));
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

F - Pass

直接转二维平面,然后第i步能走到的点\((a,b)\)a个红球,b个蓝球,要满足a小于等于前i个人红球总和,b小于等于前i个人蓝球总和

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 40005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353;
char s[2005];
int sum[2005][2],N;
int dp[4005][4005];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
void Solve() {
scanf("%s",s + 1);
N = strlen(s + 1);
for(int i = 1 ; i <= N ; ++i) {
sum[i][0] = sum[i - 1][0];
sum[i][1] = sum[i - 1][1];
if(s[i] == '0') sum[i][0] += 2;
if(s[i] == '1') {sum[i][0]++;sum[i][1]++;}
if(s[i] == '2') sum[i][1] += 2;
}
dp[0][0] = 1;
for(int i = 1 ; i <= 2 * N ; ++i) {
int t = min(i,N);
for(int j = 0 ; j <= i ; ++j) {
int a = j,b = i - j;
if(a <= sum[t][0] && b <= sum[t][1]) {
if(a) update(dp[a][b],dp[a - 1][b]);
if(b) update(dp[a][b],dp[a][b - 1]);
}
}
}
out(dp[sum[N][0]][sum[N][1]]);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}