F - Card Game

思路:

  题意:

    有n张卡片,每张卡片三个值,pi,ci,li;

    要求选出几张卡片使得pi之和大于等于给定值;

    同时,任意两两ci之和不得为素数;

    求选出的li的最小值,如果不能到达给定值则输出-1;

    二分+网络流最小割;

代码:

#include <bits/stdc++.h>

namespace data
{
#define maxn 105
#define maxque 200005 int val[maxn],mag[maxn],lev[maxn],num,lit; #undef maxn
#undef maxque
} namespace init
{
inline void in(int &justval)
{
int if_z=; justval=;
char Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
justval=justval*+Cget-'';
Cget=getchar();
}
justval*=if_z;
}
} namespace solve
{
#define maxn 105
#define maxque 200005
#define INF 0x7fffffff struct EdgeType
{
int v,f; EdgeType *next,*another; };
struct EdgeType *head[maxn],e[maxque]; int que[maxque],sum,prime[maxque*],num,cnt,s,t,deep[maxn]; bool p_[maxque*-]; inline void edge_add(int u,int v,int f)
{
e[++cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=&e[cnt];
e[++cnt].v=u,e[cnt].f=,e[cnt].next=head[v],head[v]=&e[cnt];
e[cnt].another=&e[cnt-],e[cnt-].another=&e[cnt];
} int min(const int &tops_,const int &tops__)
{
if(tops_<tops__) return tops_;
else return tops__;
} inline bool bfs()
{
t=data::num+,s=;int h=,tail=;que[h]=s;
for(int i=s;i<=t;i++) deep[i]=-;deep[s]=;
while(h<tail)
{
int now=que[h++];
for(EdgeType *i=head[now];i!=NULL;i=i->next)
{
if(deep[i->v]<&&i->f)
{
deep[i->v]=deep[now]+;
if(i->v==t) return true;
que[tail++]=i->v;
}
}
}
return false;
} int flowing(int now,int flow)
{
if(now==t||flow<=) return flow;
int oldflow=;
for(EdgeType *i=head[now];i!=NULL;i=i->next)
{
if(deep[i->v]==deep[now]+&&i->f)
{
int pos=flowing(i->v,min(i->f,flow));
flow-=pos,oldflow+=pos,i->f-=pos,i->another->f+=pos;
if(!flow) return oldflow;
}
}
if(!oldflow) deep[now]=-;
return oldflow;
} bool dinic(int res,int lit)
{
while(bfs()) res-=flowing(s,INF);
return res>=lit;
} void ouler(int limit)
{
for(int i=;i<=limit;i++)
{
if(!p_[i]) prime[++num]=i;
for(int j=;prime[j]*i<=limit&&j<=num;j++)
{
p_[i*prime[j]]=true;
if(i%prime[j]==) break;
}
}
} bool check(int x)
{
int n=data::num,k=data::lit,res=;s=,t=n+,cnt=;
for(int i=s;i<=t;i++) head[i]=NULL;
int idx__=-;
for(int i=;i<=n;i++)
{
if(data::lev[i]>x) continue;
if(data::mag[i]==&&data::val[i]>data::val[idx__]) idx__=i;
}
for(int i=;i<=n;i++)
{
if(data::lev[i]>x||(data::mag[i]==&&idx__!=i)) continue;
res+=data::val[i];
if(data::mag[i]&)
{
edge_add(s,i,data::val[i]);
for(int j=;j<=n;j++)
{
if(data::lev[j]>x||i==j) continue;
if(!(data::mag[j]&))
{
if(!p_[data::mag[i]+data::mag[j]]) edge_add(i,j,INF);
}
else if(data::mag[i]+data::mag[j]==) edge_add(i,j,INF);
}
}
else edge_add(i,t,data::val[i]);
}
return dinic(res,k);
} void binary()
{
ouler(maxque*-);
int l=,r=data::num,ans=-;
while(l<=r)
{
int mid=l+r>>;
if(check(mid)) ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
} #undef INF
#undef maxn
#undef maxque
} int main()
{
init::in(data::num),init::in(data::lit);
for(int i=;i<=data::num;i++)
{
init::in(data::val[i]);
init::in(data::mag[i]);
init::in(data::lev[i]);
}
solve::binary();
return ;
}

AC日记——Card Game codeforces 808f的更多相关文章

  1. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  2. AC日记——Success Rate codeforces 807c

    Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  3. AC日记——T-Shirt Hunt codeforces 807b

    T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  4. AC日记——Magazine Ad codeforces 803d

    803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...

  5. AC日记——Broken BST codeforces 797d

    D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...

  6. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  7. AC日记——Maximal GCD codeforces 803c

    803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...

  8. AC日记——Vicious Keyboard codeforces 801a

    801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...

  9. AC日记——Valued Keys codeforces 801B

    801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...

随机推荐

  1. WCF 动态调用(动态创建实例接口)

    很多时候,服务地址都不止一个的,这个时候就要动态去配置地址.配置Web.config,很麻烦 下面就看看怎样实现动态调用WCF. 首先看看动态创建服务对象的代码: using System; usin ...

  2. Delphi中取得程序版本号

    Delphi做的程序,如果想包含版本信息, 必须在Delphi的集成编辑环境的菜单“Project/Options/Version Info”里面添加版本信息.即在Version Info 选项卡中选 ...

  3. Luogu3959 NOIP2017宝藏(状压dp)

    按层dp,f[i][j]表示已扩展i子集的节点当前在第j层的最小代价,预处理点集间距离即可. #include<iostream> #include<cstdio> #incl ...

  4. Springboot2.0 集成shiro权限管理

    在springboot中结合shiro教程搭建权限管理,其中几个小细节的地方对新手不友好,伸手党更是无法直接运行代码,搭建过程容易遇坑,记录一下.关键的地方也给注释了. 版本:springboot版本 ...

  5. 【C++ troubleshooting】A case about decltype

    template <typename iter_t> bool next_permutation(iter_t beg, iter_t end) { // if (beg == end | ...

  6. 【BZOJ 2553】[BeiJing2011]禁忌 AC自动机+期望概率dp

    我一开始想的是倒着来,发现太屎,后来想到了一种神奇的方法——我们带着一个既有期望又有概率的矩阵,偶数(2*id)代表期望,奇数(2*id+1)代表概率,初始答案矩阵一列,1的位置为1(起点为0),工具 ...

  7. API网关Kong部署和使用文档

    KONG安装使用说明 系统版本:ubuntu14 1.下载安装包 $ wget https://github.com/Mashape/kong/releases/download/0.8.3/kong ...

  8. How to turn off the binary log for mysqld_multi instances?

    Q: MySQL supports running multiple mysqld on the same server. One of the ways is to use mysqld_multi ...

  9. Codeforces Round #524 (Div. 2) A. Petya and Origami

    A. Petya and Origami 题目链接:https://codeforc.es/contest/1080/problem/A 题意: 给出n,k,k表示每个礼品里面sheet的数量(礼品种 ...

  10. PowerDesigner使用教程(转)

    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...