[bzoj4429] [Nwerc2015] Elementary Math小学数学
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[bzoj4429] [Nwerc2015] Elementary Math小学数学相关的知识,希望对你有一定的参考价值。
一开始以为是2-sat。。QAQ
膜了下题解。。把各个答案离散化后,每对数与答案之间连边,看最大匹配数是否等于n
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #define ll long long 6 using namespace std; 7 const int maxn=2523<<2; 8 struct zs1{ll v;int id;}a[maxn]; 9 ll b[maxn];int cnt,n1; 10 struct zs{ 11 int too,pre;bool flow; 12 }e[233333];int tot,last[maxn]; 13 short dis[maxn]; 14 int dl[maxn],x[maxn],y[maxn]; 15 int i,j,k,n,m,s,t,ans; 16 17 int ra,fh;char rx; 18 inline int read(){ 19 rx=getchar(),ra=0,fh=1; 20 while((rx<‘0‘||rx>‘9‘)&&rx!=‘-‘)rx=getchar(); 21 if(rx==‘-‘)fh=-1,rx=getchar(); 22 while(rx>=‘0‘&&rx<=‘9‘)ra*=10,ra+=rx-48,rx=getchar();return ra*fh; 23 } 24 inline bool bfs(){ 25 memset(dis,0,(t+1)<<1); 26 int l=0,r=1,i,now;dl[1]=s,dis[s]=1; 27 while(l<r&&!dis[t]) 28 for(i=last[now=dl[++l]];i;i=e[i].pre)if(e[i].flow&&!dis[e[i].too]) 29 dis[e[i].too]=dis[now]+1,dl[++r]=e[i].too; 30 // for(i=1;i<=t;i++)printf("0->%d %d\\n",i,dis[i]); 31 return dis[t]; 32 } 33 int dfs(int x,int mx){ 34 if(x==t)return mx; 35 int used=0,i;bool w; 36 for(i=last[x];i;i=e[i].pre)if(e[i].flow&&dis[e[i].too]==dis[x]+1){ 37 w=dfs(e[i].too,1);if(w){ 38 e[i].flow=0,e[i^1].flow=1,used+=w; 39 if(used==mx)return used; 40 } 41 } 42 dis[x]=0;return used; 43 } 44 inline void insert(int a,int b){//printf(" %d-->%d\\n",a,b); 45 e[++tot].too=b,e[tot].flow=1,e[tot].pre=last[a],last[a]=tot, 46 e[++tot].too=a,e[tot].flow=0,e[tot].pre=last[b],last[b]=tot; 47 } 48 bool cmp(zs1 a,zs1 b){return a.v<b.v;} 49 int main(){ 50 n=read(); 51 for(i=1;i<=n;i++) 52 j=x[i]=read(),k=y[i]=read(), 53 a[++n1].v=j+k,a[++n1].v=j-k,a[++n1].v=1LL*j*k, 54 a[n1-2].id=a[n1-1].id=a[n1].id=i; 55 sort(a+1,a+1+n1,cmp);int cnt=0; 56 tot=1; 57 for(i=1;i<=n1;insert(a[i].id,cnt+n),i++) 58 if(a[i].v!=a[i-1].v||i==1)b[++cnt]=a[i].v; 59 // for(i=1;i<=n1;i++)printf(" %lld %d\\n",a[i].v,a[i].id); 60 s=0,t=n+cnt+1; 61 for(i=1;i<=n;i++)insert(s,i); 62 for(i=1;i<=cnt;i++)insert(i+n,t); 63 while(bfs())ans+=dfs(s,1<<23); 64 if(ans==n){ 65 for(i=1;i<=n;i++) 66 for(j=last[i];j;j=e[j].pre)if(e[j].too&&!e[j].flow){ 67 ll k=b[e[j].too-n]; 68 if(x[i]+y[i]==k)printf("%d + %d = %lld\\n",x[i],y[i],k);else 69 if(x[i]-y[i]==k)printf("%d - %d = %lld\\n",x[i],y[i],k);else 70 if(1LL*x[i]*y[i]==k)printf("%d * %d = %lld\\n",x[i],y[i],k); 71 break; 72 } 73 }else puts("impossible"); 74 return 0; 75 }
以上是关于[bzoj4429] [Nwerc2015] Elementary Math小学数学的主要内容,如果未能解决你的问题,请参考以下文章
bzoj 4428: [Nwerc2015]Debugging调试
bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼
BZOJ4428[Nwerc2015]Debugging调试 记忆化搜索+分块
bzoj4430 [Nwerc2015]Guessing Camels赌骆驼