BC#65 T5 ZYB's Prime

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5594

完全不会啊TAT。。

其实官方题解已经说的很清楚了。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <queue>
#include <cmath>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 405
#define ll long long
#define inf int(1e9)
using namespace std;
int a[maxn],head[maxn],cur[maxn],uu[maxn],odd[maxn],even[maxn];
int tot,t,s;
struct data{int obj,pre,c;
}e[200500];
int read(){
    int x=0,f=1; char ch=getchar();
    while (!isdigit(ch)){ if (ch==‘-‘) f=-1; ch=getchar();}
    while (isdigit(ch)) {x=x*10+ch-‘0‘; ch=getchar();}
    return x*f;
}
void insert(int x,int y,int z){
    e[++tot].obj=y; e[tot].c=z; e[tot].pre=head[x]; head[x]=tot;
    e[++tot].obj=x; e[tot].c=0; e[tot].pre=head[y]; head[y]=tot;
}
bool bfs(){
    queue<int >q; clr(uu,-1); uu[s]=0; q.push(s);
    while (!q.empty()){
        int u=q.front(); q.pop();
        for (int j=head[u];j;j=e[j].pre){
            int v=e[j].obj;
            if (uu[v]==-1&&e[j].c) uu[v]=uu[u]+1,q.push(v);
        }
    }
    if (uu[t]==-1) return 0;
    return 1;
}
int dfs(int x,int mx){
    if (x==t) return mx;
    int used=0;
    for (int j=cur[x];j;j=e[j].pre){
        int v=e[j].obj;
        if (uu[v]==uu[x]+1){
            int w=dfs(v,min(e[j].c,mx-used));
            used+=w; e[j].c-=w; e[j^1].c+=w;
            if (e[j].c) cur[x]=j;
            if (used==mx) return mx;
        }
    }
    if (!used) uu[x]=-1;
    return used;
}
int dinic(){
    int ans=0;
    while (bfs()){
        rep(i,s,t) cur[i]=head[i];
        ans+=dfs(s,inf);
    }
    return ans;
}
bool jud(int x){
    int s=int(sqrt(x))+1;
    rep(i,2,s) if (x%i==0) return 0;
    return 1;
}
bool work(){
    tot=1; clr(head,0); clr(odd,0); clr(even,0); //clr(one,0);
    int odd_cnt=0,even_cnt=0,one_in=0,one_out=0,one_cnt=0;
    int n=read();
    rep(i,1,n) {
        a[i]=read();
           if (a[i]>1&&(a[i]&1)) odd[++odd_cnt]=a[i];
        else if (a[i]>1&&((a[i]&1)==0)) even[++even_cnt]=a[i];
        else one_cnt++;
    }
    int need=even_cnt-odd_cnt;
    rep(i,1,one_cnt){
        if (need) {
               one_in++;
               odd[++odd_cnt]=1;
               need--;
           }
           else one_out++;
    }
    if ((!one_in && (one_out==2||one_out==1)) || need!=0) return 0;
    s=0; t=2*odd_cnt+1;
    int cnt=odd_cnt;
    rep(i,1,cnt) insert(s,i,2);
    rep(i,1,cnt) insert(i+cnt,t,2);
    rep(i,1,cnt) rep(j,1,cnt){
        if (jud(odd[i]+even[j])) {
            if (odd[i]==1&&one_out>0) insert(i,j+cnt,2);
            else insert(i,j+cnt,1);
        }
    }
    if (dinic()!=2*cnt) return 0;
    return 1;
}
int main(){
    int T=read();
    while (T--){
        if (work()) puts("YES"); else puts("NO");
    }
    return 0;
}

BC#65 T5 ZYB's Prime

时间: 2024-08-24 09:00:07

BC#65 T5 ZYB's Prime的相关文章

BC 65 ZYB&#39;s Premutation (线段树+二分搜索)

题目简述:有一个全排列,一直每个前缀区间的逆序对数,还原这个排列. fi记录逆序对数,pi记录该位置数值,则k=fi-f(i-1)表示前i-1个数比pi大的数的个数,那么只要在剩余元素求出按大小顺序第i-k个数字即可. 线段树+二分搜索,线段树bit[i]记录i的在剩余元素的排名顺序. 1 /******************************* 2 3 Date : 2015-12-06 19:49:59 4 Author : WQJ ([email protected]) 5 Lin

BestCoder Round #65 (ZYB&#39;s Game)

ZYB's Game Accepts: 672 Submissions: 1207 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description ZYBZYBZYB played a game named NumberBombNumber BombNumberBomb with his classmates in hiking:a host keeps a

hdu 5594 ZYB&#39;s Prime 最大流

ZYB's Prime Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5594 Description After getting 600 scores in NOIP,ZYB(ZJ−267) creates a problem:you are given N numbers,now you are asked to divide them into K groups(

BC 65 game

主持人一直某个数字在1到n范围,假设甲乙已经知道,甲先猜乙后,都采用最优策略,主持人说偏大还是偏小,不断缩小范围,问最后乙能会获胜的X的取值的个数. 如果n为奇数,那么仅当x=n/2乙必然获胜,若为奇数乙不可能获胜. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #inclu

hdu 1973 Prime Path

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices

BestCoder Round #65 hdu5592 5593 5594

hdu5592 - ZYB's Premutation http://acm.hdu.edu.cn/showproblem.php?pid=5592 对于一个X=a[i]-a[i-1],它可以表示为原排列第i个位置的数,在它前面有X个比它大的数,也就是说在1到i的区间内第i个数为该区间内第X+1大的数 那么可以考虑倒着求出原排列:对于最后一个数,令k=a[n]-a[n-1],最后一个数就是k+1:而对于倒数第二个数,令k'=a[n-1]-a[n-2],则该位置的答案为从1到n中除去k,第k'+1

H.264 数据示例

最近项目需要在研究视频实时监控功能. 第一个需要了解的就是 H.264 格式,先以 H.264 文件为例进行数据分析. 在网上下载了 foreman.264 文件,进行了帧类型的分析和帧数据的分析.然后对比实际项目视频的需要,大概分析了一下数据传输的可能性. 代码后续再上传吧,呵呵... // 分辨率为: 176 * 144 - foreman.264 FrameInfo // 实际多媒体录制为: 352 * 288,即关键帧数据约为此 H264 文件关键帧数据的 4 倍 // 关键帧数据约 2

CF449C Jzzhu and Apples (筛素数 数论?

Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tre

OpenSSL &lt;搭建私有CA&gt;

CA  Server : el7-2 (172.16.1.51)  ,颁发证书 CA  Client  : el7-1 (172.16.1.52)  ,申请证书 1)CA自签证书 ###################生成私钥 [[email protected]   ~]# openssl genrsa  -out  /etc/pki/CA/private/cakey.pem 2048 Generating   RSA private key, 2048 bit long modulus ..