A
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(a,b,c) for(int a = b; a <= c;++ a)
#define per(a,b,c) for(int a = b; a >= c; -- a)
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c >'9'){ if(c == '-') f = -1; c = gc; }
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-'); x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0') ;
}
int main() {
int n = read(),m =read(),k = read();
int xu=1,xd=n,yu=1,yd=m,ans=0;
while(k --) {
if(xu>xd || yu > yd) break;
ans += 2 * (xd - xu + 1) + 2 * (yd - yu + 1) - 4;
xd -=2 , xu += 2, yd -= 2, yu += 2;
}
print(ans);
return 0;
}
B
枚举尾项往前推
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(a,b,c) for(int a = b; a <= c;++ a)
#define per(a,b,c) for(int a = b; a >= c; -- a)
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c >'9'){ if(c == '-') f = -1; c = gc; }
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-'); x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0') ;
}
int n,k;
const int maxn = 100007;
int a[maxn][2],b[maxn][2],ans[maxn][2];
bool Check() {
for(int i = n - 1;i;-- i)
for(int j = 0;j < 2;++ j) {
if(a[i][j] && b[i][j]) {
if(!ans[i + 1][j]) return false;
ans[i][j] = 1;
}
else if(!a[i][j] && !b[i][j]) {
if(ans[i + 1][j]) return false ;
ans[i][j] = 0;
}
else if(!a[i][j] && b[i][j]) return false;
else if(a[i][j] && !b[i][j]) {
if(ans[i + 1][j]) ans[i][j] = 0;
else ans[i][j] = 1;
}
}
for(int i = 1;i < n;++ i)
for(int j = 0;j < 2;++ j)
if((a[i][j] != (ans[i][j] | ans[i + 1][j])) || (b[i][j] != (ans[i][j] & ans[i + 1][j])))
return false;
puts("YES");
for(int i = 1;i <= n;++ i) print(ans[i][1] * 2 + ans[i][0]),pc(' ');
return true;
}
int main() {
n=read();
for(int i =1,ai;i < n;++ i) {
ai = read();
a[i][0] = ai & 1;
a[i][1] = ai >> 1 & 1;
}
for(int i=1,bi;i < n;++ i) {
bi = read();
b[i][0]=bi&1;
b[i][1] = bi >> 1 & 1;
}
ans[n][0] = 0, ans[n][1] = 0;
if(Check())return 0;
ans[n][0] = 0, ans[n][1] = 1;
if(Check())return 0;
ans[n][0] = 1, ans[n][1] = 0;
if(Check())return 0;
ans[n][0] = 1, ans[n][1] = 1;
if(Check())return 0;
puts("NO");
return 0;
}
C
二分最大能构成的1-n
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(a,b,c) for(int a = b; a <= c;++ a)
#define per(a,b,c) for(int a = b; a >= c; -- a)
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c >'9'){ if(c == '-') f = -1; c = gc; }
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-'); x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0') ;
}
int tot = 0;
const int maxn = 10000007;
int ans1[maxn],a1,ans2[maxn],a2,vis[maxn];
int A,B,n,m;
bool judge(int x) {
int a = A,b = B;
while(x) {
if(a < b) std::swap(a,b);
if(a < x) return false;
a -= x, -- x;
}
return true;
}
int main() {
A = read(),B = read(),n = 0,m = 0;
int l=0,r=1e6,mid,ans=0;
while(l <= r) {
if(judge(mid = l + r >> 1)) l = mid + 1,ans = mid;
else r = mid - 1;
}
for(int i = ans;i; -- i)
if(A < B) B -= i, ans2[++ m] = i;
else A -= i, ans1[++ n] = i;
print(n);
pc('
');
for(int i = n;i;-- i) print(ans1[i]),pc(' '); pc('
');
print(m);
pc('
');
for(int i=m; i; --i) print(ans2[i]),pc(' '); pc('
');
return 0;
}
D
找到最远的能使得前缀为a的点,bfs求字典序最小路径
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(a,b,c) for(int a = b; a <= c;++ a)
#define per(a,b,c) for(int a = b; a >= c; -- a)
#define gc getchar()
#define pc putchar
#define pr pair<int,int>
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c >'9'){ if(c == '-') f = -1; c = gc; }
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-'); x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0') ;
}
const int maxn = 4007;
#define mp make_pair
#define fi first
#define se second
int n,k,f[maxn][maxn],mx;
char s[maxn][maxn];
vector<pr> v;
#define INF 0x3f3f3f3f;
bool vis[maxn][maxn];
void bfs() {
vector<int> path;
memset(vis,0,sizeof(vis));
vector<pr> nxt;
int xxx = v[1].fi,yyy = v[1].se;
for(int i = 0;i < v.size();++ i) {
int X = v[i].fi,Y = v[i].se;
vis[X][Y] = 1;
nxt.push_back(v[i]);
}
for(int i = 0; i < n * 2 - (xxx + yyy); i++) {
int min_col = INF;
for(int j = 0;j < nxt.size(); j++){
int xx = nxt[j].fi,yy = nxt[j].se;
for(int k = 0;k <= 1;++ k) {
int tx = (k & 1) ? xx + 1 : xx;
int ty = !(k & 1) ? yy + 1 : yy;
if(tx > n || ty > n) continue;
min_col = min(min_col,s[tx][ty] - 'a');
}
}
path.push_back(min_col);
vector<pr> nxt2;
for(int j = 0; j < nxt.size(); j++) {
int xx = nxt[j].fi,yy = nxt[j].se;
for(int k = 0;k <= 1;++ k) {
int tx = (k & 1) ? xx + 1 : xx;
int ty = !(k & 1) ? yy + 1 : yy;
if(tx > n || ty > n) continue;
if(!vis[tx][ty] && s[tx][ty] - 'a' == min_col) {
vis[tx][ty] = 1;
nxt2.push_back(mp(tx,ty));
}
}
}
nxt = nxt2;
}
for(int i = 1; i <= mx; i++) pc('a');
for(int i = 0; i < path.size(); i ++) {
pc(path[i] + 'a');
}
}
int GG,id[maxn][maxn];
int main() {
n = read(); k = read();
for(int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
for(int i = 1; i <= n; i++) s[i][0] = 'z' + 1;
for(int i = 1; i <= n; i++) s[0][i] = s[n + 1][i] = 'z' + 1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
id[i][j] = ++GG;
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if(s[i][j] == 'a') f[i][j]++;
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
int ned = i + j - f[i][j] - 1;
if(ned <= k) {
if(i + j > mx) v.clear(), v.push_back(mp(i, j)), mx = i + j;
else if(i + j == mx) v.push_back(mp(i, j));
}
}
}
mx --;
//print(mx);
bfs();
return 0;
}