Codeforces Round #840 (Div. 2) 题解
A Absolute Maximization
答案为所有数的 or 减去所有数的 and。
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
const int M=998244353;
inline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;}
inline int Mod(re int x){return x>=M?x-M:x;}
inline int ksm(re int x,re int y){
re int s=1;
while(y){
if(y&1)s=1ll*s*x%M;
x=1ll*x*x%M,y>>=1;
}
return s;
}
int t,n,m,a[1000002],ans,A,B,R[1000002],stk[1000002],tp;
char s[1000002];
int main(){
t=read();
while(t--){
n=read();int ans1,ans2;
for(re int i=1;i<=n;++i)a[i]=read();
ans1=ans2=a[1];
for(re int i=2;i<=n;++i)ans1|=a[i],ans2&=a[i];
printf("%d\n",ans1-ans2);
}
}
B Incinerate
按照血量排序,预处理后缀最小值,直接模拟,时间复杂度
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
const int M=998244353;
inline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;}
inline int Mod(re int x){return x>=M?x-M:x;}
inline int ksm(re int x,re int y){
re int s=1;
while(y){
if(y&1)s=1ll*s*x%M;
x=1ll*x*x%M,y>>=1;
}
return s;
}
int t,n,m,a[1000002],ans,A,B,R[1000002],stk[1000002],tp,mn[1000002];
char s[1000002];
struct node{
int x,y;
bool operator <(const node A)const{return x<A.x;}
}p[100002];
int main(){
t=read();
while(t--){
n=read(),m=read();
for(re int i=1;i<=n;++i)p[i].x=read();
for(re int i=1;i<=n;++i)p[i].y=read();
sort(p+1,p+n+1);
long long sum=0;
for(re int i=n;i;--i){
mn[i]=p[i].y;
if(i^n)mn[i]=min(mn[i],mn[i+1]);
}
int pos=1;
while(1){
if(pos>n){
puts("YES");
break;
}
if(m<=0){
puts("NO");
break;
}
sum+=m;
while(pos<=n&&p[pos].x<=sum)++pos;
if(pos<=n)m-=mn[pos];
}
}
}
C Another Array Problem
这场唯一有趣一点的题。
这一类的题目一般都存在一个构造使得答案能取到一个很明显的上界。比如这道题在大部分时候我们能使答案取到 最大值
构造如下:
如果最大值位置是
先对
若
这样除了
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
const int M=998244353;
inline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;}
inline int Mod(re int x){return x>=M?x-M:x;}
inline int ksm(re int x,re int y){
re int s=1;
while(y){
if(y&1)s=1ll*s*x%M;
x=1ll*x*x%M,y>>=1;
}
return s;
}
int t,n,m,a[1000002],A,B,R[1000002],stk[1000002],tp;
char s[1000002];
long long ans;
int main(){
t=read();
while(t--){
n=read();
for(re int i=1;i<=n;++i)a[i]=read();
ans=0;re int mx=0;
for(re int i=1;i<=n;++i)ans+=a[i];
for(re int i=1;i<=n;++i)if(i>=3||i<=n-2)mx=max(mx,a[i]);
if(n>=2)mx=max(mx,abs(a[1]-a[2])),mx=max(mx,abs(a[n]-a[n-1]));
ans=max(ans,1ll*n*mx);
printf("%lld\n",ans);
}
}
Valid Bitonic Permutations
双调序列的常见 DP 方式:维护两个队列,所有数从小往大加入,每个数加入左边的队列或右边的队列,最后将两个队列拼在一起得到答案。
直接套用这个就行。
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
const int M=1e9+7;
inline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;}
inline int Mod(re int x){return x>=M?x-M:x;}
inline int ksm(re int x,re int y){
re int s=1;
while(y){
if(y&1)s=1ll*s*x%M;
x=1ll*x*x%M,y>>=1;
}
return s;
}
int t,n,m,a[1000002],ans,A,B,X,Y,R[1000002],stk[1000002],tp,f[102][102][102];
char s[1000002];
int main(){
t=read();
while(t--){
n=read(),A=read(),B=read(),X=read(),Y=read();
memset(f,0,sizeof(f));
f[1][0][0]=1;
for(re int i=1;i<=n;++i)
for(re int a=0;a<i;++a)
for(re int b=0;b+a<i;++b){
if(i==n&&(a==0||a==n-1))continue;
if((a!=A-1||i==X)&&(a!=B-1||i==Y))add(f[i+1][a+1][b],f[i][a][b]);
if((n-b!=B||i==Y)&&(n-b!=A||i==X))add(f[i+1][a][b+1],f[i][a][b]);
}
re int ans=0;
for(re int a=1;a<n;++a)add(ans,f[n+1][a][n-a]);
printf("%lld\n",1ll*ans*(M+1>>1)%M);
}
}
E Node Pairs
双向可达关系构成多个完全图。
那第一问就是若干个完全图边数总和为
第二问一个显然的上界是
其中
也是可以构造了,串成一条链就行了。
对于
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
const int M=998244353;
inline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;}
inline int Mod(re int x){return x>=M?x-M:x;}
inline int ksm(re int x,re int y){
re int s=1;
while(y){
if(y&1)s=1ll*s*x%M;
x=1ll*x*x%M,y>>=1;
}
return s;
}
int t,n,m,a[1000002],A,B,R[1000002],stk[1000002],tp,f[200002],g[200002];
char s[1000002];
int ans1,ans2;
int main(){
memset(f,0x3f,sizeof(f)),memset(g,0x3f,sizeof(g));
f[0]=0,g[0]=0;
for(re int i=1;i<=1000;++i)
for(re int j=i*(i-1)/2;j<=200000;++j){
if(f[j]>f[j-i*(i-1)/2]+i)f[j]=f[j-i*(i-1)/2]+i,g[j]=g[j-i*(i-1)/2]+i*(i-1)/2;
else if(f[j]>f[j-i*(i-1)/2]+i)g[j]=min(g[j],g[j-i*(i-1)/2]+i*(i-1)/2);
}
n=read();
printf("%d %d",f[n],f[n]*(f[n]-1)/2-g[n]);
}
F Edge Queries
没看懂这题,直接建圆方树输出路径上非割边条数,好像并不需要任何图的性质?
#include<bits/stdc++.h>
#define re register
using namespace std;
const int Mxdt=100000; //单次大小
inline char gc(){
static char buf[Mxdt],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxdt,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read(){
re int t=0;re char v=gc();
while(v<'0')v=gc();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=gc();
return t;
}
int n,m,stk[400002],tot,head[400002],cnt,dfn[400002],low[400002],tim,val[400002],siz[400002],tp,fa[22][400002],sum[400002],dep[400002];
long long ans;
struct edge{int to,next;}e[800002];
inline void add(re int x,re int y){e[++cnt]=(edge){y,head[x]},head[x]=cnt;}
vector<int>g[400002];
inline void tj(re int x){
dfn[x]=low[x]=++tim,stk[++tp]=x;
for(re int i=head[x];i;i=e[i].next)
if(!dfn[e[i].to]){
tj(e[i].to);
low[x]=min(low[x],low[e[i].to]);
if(low[e[i].to]==dfn[x]){
val[++tot]=1;
map<int,int>vis;vis[x]=1;
while(1){
++val[tot];
g[tot].push_back(stk[tp]);
g[stk[tp]].push_back(tot);
for(re int j=head[stk[tp]];j;j=e[j].next)if(vis.count(e[j].to))++sum[tot];
vis[stk[tp]]=1;
if(stk[tp--]==e[i].to)break;
}if(sum[tot]==1)sum[tot]=0;
g[x].push_back(tot);
g[tot].push_back(x);
}
}
else low[x]=min(low[x],dfn[e[i].to]);
}
inline void dfs(re int x,re int y){
fa[0][x]=y,dep[x]=dep[y]+1;
for(re int i=1;i<=20;++i)fa[i][x]=fa[i-1][fa[i-1][x]];
for(re int i=0;i<g[x].size();++i){
re int z=g[x][i];
if(z^y)sum[z]+=sum[x],dfs(z,x);
}
}
int main(){
tot=n=read(),m=read();
for(re int i=1,x,y;i<=m;++i)x=read(),y=read(),add(x,y),add(y,x);
tj(1),dfs(1,1);
int q=read();
while(q--){
re int x=read(),y=read(),oo=sum[x]+sum[y];
if(dep[x]<dep[y])swap(x,y);
for(re int i=20;~i;--i)if(dep[fa[i][x]]>=dep[y])x=fa[i][x];
for(re int i=20;~i;--i)if(fa[i][x]!=fa[i][y])x=fa[i][x],y=fa[i][y];
if(x!=y)x=fa[0][x];
oo-=sum[x],oo-=sum[fa[0][x]];
printf("%d\n",oo);
}
}