Code Chef April Cook-Off 2019题解

传送门

\(PEWDSVTS\)

我哪根筋不对了要把所有可行的拿出来\(sort\)一下……还有忘开\(long\ long\)真的好难受……

int main(){
// freopen("testdata.in","r",stdin);
for(int T=read();T;--T){
n=read(),A=read(),B=read(),X=read(),Y=read(),Z=read(),top=sum=0;
d=(Z-B+Y-1)/Y,res=Z-1ll*(d-1)*X-A;
while(!q.empty())q.pop();
fp(i,1,n){
x=read(),q.push(x);
while(x)sum+=x,x>>=1;
}
if(sum<res){puts("RIP");continue;}
sum=cnt=0;
while(sum<res)x=q.top(),q.pop(),sum+=x,q.push(x>>1),++cnt;
printf("%d\n",cnt);
}
return 0;
}

\(RANDGAME\)

首先奇数是必赢的,偶数的话考虑把它写成\(2^k\times w\)的形式,如果\(k\)是偶数必赢,否则必输。直接归纳法就可以证了。然后奇数的情况判一下\(-1\)和\(+1\)哪个可以必赢就是了

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
ll n;string s;
inline bool calc(R ll x){if(!x)return 0;R int res=0;while(x&1^1)res^=1,x>>=1;return res&1;}
int main(){
int T;cin>>T;
while(T--){
cin>>n;
if(calc(n)){cout<<"Lose"<<'\n'<<flush;cin>>s;continue;}
cout<<"Win"<<'\n'<<flush;
while(true){
if(n&1)cout<<((n==1||calc(n-1))?(--n,"-1"):(++n,"+1"))<<'\n'<<flush;
else cout<<(n>>=1,"/2")<<'\n'<<flush;
cin>>s;if(s[0]=='G')break;
if(s[1]=='1')s[0]=='+'?++n:--n;
else n>>=1;
}
}
}

\(DINCPATH\)

拆成两条有向边,按边权排个序,从小到大往里加边,跑个最短路就可以了

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
ll read(){
R ll res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=1e5+5;
struct node{
int u,v;ll d;
inline node(){}
inline node(R int uu,R int vv,R ll dd):u(uu),v(vv),d(dd){}
inline bool operator <(const node &b)const{return d<b.d;}
}st[N<<1];
ll a[N];int n,m,top,res,f[N],g[N];
int main(){
// freopen("testdata.in","r",stdin);
for(int T=read();T;--T){
n=read(),m=read(),res=top=0;
fp(i,1,n)a[i]=read(),f[i]=1;
for(R int i=1,u,v;i<=m;++i){
u=read(),v=read();
st[++top]=node(u,v,a[v]-a[u]),st[++top]=node(v,u,a[u]-a[v]);
}
sort(st+1,st+1+top);
int l=1,r;while(l<=top&&st[l].d<=0)++l;
for(r=l;l<=top;l=r){
while(r<=top&&st[r].d==st[l].d)++r;
fp(i,l,r-1)g[st[i].v]=f[st[i].v];
fp(i,l,r-1)cmax(g[st[i].v],f[st[i].u]+1);
fp(i,l,r-1)f[st[i].v]=g[st[i].v];
}
fp(i,1,n)cmax(res,f[i]);
printf("%d\n",res);
}
return 0;
}

\(MYS00T\)

我绝对傻了……

首先无解的情况不存在,证明的话如果\(a_u>a_v\)则连有向边\((u,v)\),那么无解就是说所有节点入度都不为\(0\),总共\(n-1\)条边你告诉我\(n\)个节点入度都不为\(0\)……

然后跑个点分就好了,点分树的树高是\(O(\log n)\)的,询问次数也是这个级别。

现在做交互已经不敢用\(read\)和\(scanf\)了……

//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=2e5+5;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
bool vis[N];int sz[N],son[N],size,rt,n;
inline int query(R int u){cout<<"1 "<<u<<'\n'<<flush;cin>>u;return u;}
void findrt(int u,int fa){
sz[u]=1,son[u]=0;
go(u)if(v!=fa&&!vis[v])findrt(v,u),sz[u]+=sz[v],cmax(son[u],sz[v]);
cmax(son[u],size-sz[u]);
if(son[u]<son[rt])rt=u;
}
void solve(int u){
vis[u]=1;int v=query(u);
if(v==-1)return cout<<"2 "<<u<<'\n'<<flush,void();
rt=0,size=sz[v]>sz[u]?size-sz[u]:sz[v],findrt(v,u);
solve(rt);
}
int main(){
// freopen("testdata.in","r",stdin);
ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
int T;cin>>T;
while(T--){
cin>>n;
for(R int i=1,u,v;i<n;++i)cin>>u>>v,add(u,v),add(v,u);
son[0]=n+1,size=n,rt=0,findrt(1,0),
solve(rt);
memset(head,0,(n+1)<<2),memset(vis,0,n+1),tot=0;
cin>>n;
}
return 0;
}

\(GUESSAGE\)

\(1\)类询问即为\(d_u\geq d_v+c\),\(2\)类询问即为\(d_v\geq d_u+1-c\),跑个差分约束就行了

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=1005,M=10005;
struct eg{int u,v,w;}e[M];
int n,m;ll c[N],d[N];
int main(){
// freopen("testdata.in","r",stdin);
for(int T=read();T;--T){
n=read(),m=read();
for(R int i=1,t,u,v,w;i<=m;++i){
t=read(),u=read(),v=read(),w=read();
if(t==1)e[i]={v,u,w};else e[i]={u,v,1-w};
}
memset(c,0x3f,(n+1)<<3),memset(d,0xef,(n+1)<<3);
c[1]=d[1]=0;
fp(t,1,n)fp(i,1,m){
cmax(d[e[i].v],d[e[i].u]+e[i].w);
cmin(c[e[i].u],c[e[i].v]-e[i].w);
}
bool flag=0;
fp(i,1,n)if(c[i]!=d[i]){flag=1;break;}
if(flag){puts("NO");continue;}
fp(i,1,m)if(d[e[i].v]<d[e[i].u]+e[i].w||c[e[i].u]>c[e[i].v]-e[i].w){flag=1;break;}
if(flag){puts("NO");continue;}
puts("YES");
fp(i,1,n)printf("%lld%c",c[i]," \n"[i==n]);
}
return 0;
}
上一篇:C# 多线程线程池( 一 )


下一篇:js控制css时注意