传送门:https://codeforces.com/contest/1405
A
将序列翻转输出即可。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
int main(){
int T; cin>>T;
while(T--){
int n; cin>>n;
vi res;
rep(i,1,n){
int v; read(v);
res.pb(v);
}
reverse(all(res));
for(auto i: res) cout<<i<<' ';
cout<<endl;
}
return 0;
}
B
维护当前的正值,看看最后的正值是多少。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(ll &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
int main(){
int T; cin>>T;
while(T--){
int n; cin>>n;
ll cur=0;
rep(i,1,n){
ll v; read(v);
cur=max(0LL, cur+v);
}
cout<<cur<<endl;
}
return 0;
}
C
注意到必须有第 (i) 个元素等于 (i+k) 个元素,如果出现冲突输出 NO
,否则看看 ([1, k]) 的元素是否出现 (1) 或者 (0) 个数超过 (frac{k}{2}) 的情况,没有就是 YES
。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
string s;
int main(){
ios::sync_with_stdio(false);
int T; cin>>T;
while(T--){
int n, k; cin>>n>>k;
cin>>s;
s=' '+s;
for(auto &i: s) if(i=='?') i='2';
vi res(n+1, 2);
bool ok=true;
rep(i,1,k) for(int j=i; j<=n; j+=k){
int v=s[j]-'0';
if(v==2) continue;
if((v^res[i])==1){
ok=false;
break;
}
res[i]=v;
}
if(!ok){
puts("NO");
continue;
}
int cnt[2]={0};
rep(i,1,n) if(res[i]!=2) cnt[res[i]]++;
if(cnt[0]>k/2 || cnt[1]>k/2) puts("NO");
else puts("YES");
}
return 0;
}
D
图论 + 结论题
Alice
胜利当且仅当下面至少一条满足:
- 树的直径 (geq 2da)
- (2dageq db)
- 初始距离 (dageq dis(a, b))
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#define debug(x) cerr << #x << ": " << x << endl
#define pb push_back
#define eb emplace_back
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/(b)
#define all(x) (x).begin(), (x).end()
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vpii = vector<pii>;
using ll = long long;
using ull = unsigned long long;
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
const int N=1e5+5, M=N<<1;
struct Edge{
int to, next;
}e[M];
int n;
int h[N], tot;
void add(int u, int v){
e[tot].to=v, e[tot].next=h[u], h[u]=tot++;
}
int fa[N], son[N], sz[N], dep[N];
int cnt, id[N], top[N];
void dfs1(int u, int father, int depth){
fa[u]=father, sz[u]=1, dep[u]=depth;
for(int i=h[u]; ~i; i=e[i].next){
int go=e[i].to;
if(go==father) continue;
dfs1(go, u, depth+1);
sz[u]+=sz[go];
if(sz[go]>sz[son[u]]) son[u]=go;
}
}
void dfs2(int u, int t){
id[u]=++cnt, top[u]=t;
if(!son[u]) return;
dfs2(son[u], t);
for(int i=h[u]; ~i; i=e[i].next){
int go=e[i].to;
if(go==fa[u] || go==son[u]) continue;
dfs2(go, go);
}
}
int lca(int u, int v){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u, v);
u=fa[top[u]];
}
return dep[u]<dep[v]? u: v;
}
int dis(int x, int y){
return dep[x]+dep[y]-2*dep[lca(x, y)];
}
bool vis[N];
int d[N];
int find(int u){
queue<int> q;
q.push(u);
rep(i,1,n) vis[i]=0, d[i]=INF;
d[u]=0; int res=0;
vis[u]=true;
while(q.size()){
int t=q.front(); q.pop();
for(int i=h[t]; ~i; i=e[i].next){
int go=e[i].to;
if(!vis[go]){
vis[go]=true;
d[go]=d[t]+1;
res=max(res, d[go]);
q.push(go);
}
}
}
return res;
}
int get_D(){
int u=1;
rep(i,1,n) if(dep[i]>dep[u]) u=i;
return find(u);
}
int main(){
int T; cin>>T;
while(T--){
int a, b, x, y; cin>>n>>a>>b>>x>>y;
rep(i,1,n) h[i]=-1, son[i]=fa[i]=top[i]=sz[i]=dep[i]=id[i]=0;
tot=cnt=0;
rep(i,1,n-1){
int u, v; read(u), read(v);
add(u, v), add(v, u);
}
dfs1(1, -1, 1), dfs2(1, 1);
int d=get_D();
if(dis(a, b)<=x || d<=2*x || 2*x>=y) puts("Alice");
else puts("Bob");
}
return 0;
}