AC日记——Card Game codeforces 808f
思路:
题意:
有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的更多相关文章
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——T-Shirt Hunt codeforces 807b
T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- AC日记——Broken BST codeforces 797d
D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- AC日记——Maximal GCD codeforces 803c
803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...
- AC日记——Vicious Keyboard codeforces 801a
801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...
- AC日记——Valued Keys codeforces 801B
801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...
随机推荐
- thead tfoot tbody标签的使用
这三个都是<body>元素的子标签,不常用,因为其只是对<tr>标签做了一个区分 <thread>用于包裹表格头信息 <tfoot>用于包裹表格最后一行 ...
- AJAX基本演示使用
Servlet配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...
- C#判断字符串是否为数字字符串
在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现.[方法一]:使用 try{} catch{} 语句. 我们可以在try语句块中试图将str ...
- [CF895C]Square Subsets
题目大意:给一个集合$S$($1\leq S_i\leq 70$),选择一个非空子集,使它们的乘积等于某个整数的平方的方法的数量. 求方案数,若两种方法选择的元素的索引不同,则认为是不同的方法. 题解 ...
- 实际上ECMAScript中并没有对类的定义
首先,我们用一个经典例子来简单阐述一下ECMAScript中的继承机制. 在几何学上,实质上几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形. ...
- The 13th Zhejiang Provincial Collegiate Programming Contest - C
Defuse the Bomb Time Limit: 2 Seconds Memory Limit: 65536 KB The bomb is about to explode! Plea ...
- python3处理pdf
https://github.com/1049451037/pdfminer3k 使用pdfminer3k,如果是python2的话直接用pdfminer就行了. python setup.py in ...
- inflate
LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化 https://www.cnblogs.com/savagemorgan/p/3865831.html
- 学习python类
类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...
- 九大排序算法Java实现
之前学习数据结构与算法时花了三天时间整理九大排序算法,并采用Java语言来实现,今天第一次写博客,刚好可以把这些东西从总结的文档中拿出来与大家分享一下,同时作为自己以后的备忘录. 1.排序算法时间复杂 ...