C - 4-adjacent
我们挑出来4的倍数和不是4的倍数而是2的倍数,和奇数
然后就是放一个奇数,放一个4,如果一个奇数之后无法放4,然后它又不是最后一个,那么就不合法
#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('
')
#define MAXN 100005
#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,a[MAXN];
vector<int> v[5],ans;
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(a[i]);
for(int i = 1 ; i <= N ; ++i) {
if(a[i] % 4 == 0) v[4].pb(a[i]);
else if(a[i] % 2 == 0) v[2].pb(a[i]);
else v[1].pb(a[i]);
}
while(v[1].size()) {
ans.pb(v[1].back());
v[1].pop_back();
if(ans.size() == N) break;
if(v[4].size()) {
ans.pb(v[4].back());
v[4].pop_back();
}
else {
puts("No");return;
}
}
puts("Yes");
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
D - Grid Coloring
就是每行填,如果填到末尾,就下一行从末尾开始填
如果填到开头,下一行从开始填
#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('
')
#define MAXN 100005
#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 H,W,N;
int a[100005],c[105][105],g[105];
void Solve() {
read(H);read(W);
read(N);
for(int i = 1 ; i <= N ; ++i) read(a[i]);
int f = 0;
int t = 1;
for(int i = 1 ; i <= N ; ++i) {
if(g[t] + a[i] < W) {
if(!f) {
for(int j = g[t] + 1 ; j <= g[t] + a[i] ; ++j) c[t][j] = i;
}
else {
for(int j = W - g[t] ; j >= W - g[t] - a[i] + 1 ; --j) c[t][j] = i;
}
g[t] += a[i];
}
else {
for(int j = 1 ; j <= W ; ++j) {
if(!c[t][j]) c[t][j] = i;
}
a[i] -= W - g[t];
if(c[t][W] == i) f = 1;
else f = 0;
++t;
while(a[i]) {
if(!f) c[t][g[t] + 1] = i;
else c[t][W - g[t]] = i;
++g[t];--a[i];
if(g[t] == W) ++t;
}
}
}
for(int i = 1 ; i <= H ; ++i) {
for(int j = 1 ; j <= W ; ++j) {
out(c[i][j]);space;
}
enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
E - Young Maids
对于一个序列,我们取一个最小的奇数位置,然后取这个最小的奇数后面的偶数位置,用st表实现,序列会被分成三份,然后递归会形成树,我们求一个最小的dfs序即可
#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('
')
#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 N,p[MAXN],tot;
int st[2][MAXN][19],len[MAXN],pos[MAXN];
pii t[MAXN];
vector<int> son[MAXN];
set<pii > S;
int query_min(int k,int l,int r) {
int s = len[r - l + 1];
return min(st[k][l][s],st[k][r - (1 << s) + 1][s]);
}
int build_tree(int l,int r) {
if(r < l) return 0;
int k = (l & 1);
int a = query_min(k,l,r);
int b = query_min(k ^ 1,pos[a] + 1,r);
t[++tot] = mp(a,b);
int res = tot;
son[res].pb(build_tree(l,pos[a] - 1));
son[res].pb(build_tree(pos[a] + 1,pos[b] - 1));
son[res].pb(build_tree(pos[b] + 1,r));
return res;
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(p[i]);
st[i & 1][i][0] = p[i];
st[(i & 1) ^ 1][i][0] = 0x7fffffff;
pos[p[i]] = i;
}
for(int k = 0 ; k <= 1 ; ++k) {
for(int j = 1 ; j <= 18 ; ++j) {
for(int i = 1 ; i <= N ; ++i) {
if(i + (1 << j) - 1 > N) break;
st[k][i][j] = min(st[k][i][j - 1],st[k][i + (1 << j - 1)][j - 1]);
}
}
}
for(int i = 2 ; i <= N ; ++i) {
len[i] = len[i / 2] + 1;
}
int rt = build_tree(1,N);
S.insert(mp(t[rt].fi,rt));
while(!S.empty()) {
auto b = *S.begin();S.erase(S.begin());
out(t[b.se].fi);space;out(t[b.se].se);space;
for(auto k : son[b.se]) {
if(k) S.insert(mp(t[k].fi,k));
}
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
F - Prime Flip
把每个数改成异或差分
然后修改一个区间相当于修改两个点,不需要加额外的点,我们需要把所有异或差分值为1的地方两两配对
如果相差为质数,那么花费为1
相差为偶数 花费为2
相差为奇数且非质数,花费为3(一个偶数可以拆成两个奇素数的和,怎么地这么小的范围总得成立吧,不然哥德巴赫猜想早证伪了,然后只需要挑一个大一点的奇素数减掉这个偶数就行了)
之后我们把这些点分为奇数点和偶数点,差值为质数则连边,做一个最大匹配,之后每个点集两两匹配,如果两个点集还剩一个,就连一条长度为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('
')
#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 N;
bool nonprime[10000005],vis[10000005];
int prime[10000005],tot,x[205],b[205],M[2],matc[205];
vector<int> to[505];
bool used[205];
bool match(int u) {
for(auto t : to[u]) {
if(!used[t]) {
used[t] = 1;
if(!matc[t] || match(matc[t])) {
matc[t] = u;
return true;
}
}
}
return false;
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(x[i]);
vis[x[i]] = 1;
}
for(int i = 2 ; i <= 10000000 ; ++i) {
if(!nonprime[i]) {
prime[++tot] = i;
}
for(int j = 1 ; j <= tot ; ++j) {
if(prime[j] > 10000000 / i) break;
nonprime[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
tot = 0;
for(int i = 1 ; i <= 10000001 ; ++i) {
if(vis[i] != vis[i - 1]) b[++tot] = i;
}
for(int i = 1 ; i <= tot ; ++i) {
M[b[i] & 1]++;
for(int j = 1 ; j <= tot ; ++j) {
if(i == j) continue;
if(abs(b[i] - b[j]) < 3) continue;
if(!nonprime[abs(b[i] - b[j])]) to[i].pb(j);
}
}
int ans = 0;
for(int i = 1 ; i <= tot ; ++i) {
if(b[i] & 1) {
memset(used,0,sizeof(used));
if(match(i)) ++ans;
}
}
out(ans + ((M[0] - ans) / 2 + (M[1] - ans) / 2) * 2 + ((M[0] - ans) & 1) * 3);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}