题目大意

一个物品有三个属性 : 价值,键值,等级.

你不能选取等级高于\(level\)的物品,键值之和为质数的两个数字不共存.

问最低的等级使得可以选出价值之和超过\(k\)的物品.

\(n\leq 100, 1 \leq \text{键值} \leq n\)

题解

首先考虑二分答案.

这样可以去掉物品等级的限制.

我们很容易发现除了\(2\)的所有偶数都是非质数.

什么意思呢 ?

如果我们不考虑\(1\)这个神奇的数字,那么按照排斥关系建边会发现这构成了一张二分图.

所以可以直接用最小割经典模型解决.

那么现在考虑一下\(1\)这个神奇的数字.

我们发现只有\(1+1\)会得到\(2\).

所以我们可以对\(1\)进行特殊处理.

因为我们知道只能在所有的\(1\)中选取出一个\(1\).

所以我们可以找到价值最大的\(1\)进行建图.

复杂度\(O(\text{网络流})\)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int maxn = 128;
const int maxnum = 200010;
const int inf = 0x3f3f3f3f;
int pri[maxnum],pri_cnt;bool vis[maxnum];
void liner(int n){
vis[1] = true;
rep(i,2,n){
if(!vis[i]) pri[++pri_cnt] = i;
rep(j,1,pri_cnt){
ll x = 1LL*i*pri[j];
if(x > n) break;
vis[x] = true;
if(i % pri[j] == 0) break;
}
}
}
struct Edge{
int to,next,cap;
}G[maxn*(maxn+4)];
int head[maxn],cnt = 1;
void add(int u,int v,int c){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
G[cnt].cap = c;
}
inline void insert(int u,int v,int c){
//printf("edge %d -> %d : (%d)\n",u,v,c);
add(u,v,c);add(v,u,0);
}
#define v G[i].to
int q[maxn],l,r,dis[maxn];
int S,T;
bool bfs(){
memset(dis,-1,sizeof dis);
l = 0;r = -1;q[++r] = S;
dis[S] = 0;
while(l <= r){
int u = q[l++];
for(rg i = head[u];i;i=G[i].next){
if(dis[v] == -1 && G[i].cap){
dis[v] = dis[u] + 1;
q[++r] = v;
}
}
}return dis[T] != -1;
}
int dfs(int u,int f){
if(u == T || f == 0) return f;
int ret = 0;
for(rg i = head[u];i;i=G[i].next){
if(dis[v] == dis[u] + 1 && G[i].cap){
int x = dfs(v,min(G[i].cap,f));
ret += x;f -= x;
G[i].cap -= x;
G[i^1].cap += x;
if(f == 0) break;
}
}return ret;
}
inline int dinic(){
int ret = 0;
while(bfs()) ret += dfs(S,inf);
return ret;
}
#undef v
struct Node{
int p,c,l;
Node(){}
Node(const int &a,const int &b){
p = a;c = b;
}
}a[maxn];
inline bool cmp(const Node &a,const Node &b){
return a.l < b.l;
}
int sta[2][maxn],top[2],val = 0,max1 = 0;
int nod[2][maxn],nodecnt;
inline void insert(const Node &x){
val += x.p;++ nodecnt;
if(x.c & 1){
insert(nodecnt,T,x.p);
rep(i,1,top[0]){
if(vis[x.c + sta[0][i]] == false){
insert(nod[0][i],nodecnt,inf);
}
}
sta[1][++top[1]] = x.c;
nod[1][top[1]] = nodecnt;
}else{
insert(S,nodecnt,x.p);
rep(i,1,top[1]){
if(vis[x.c + sta[1][i]] == false){
insert(nodecnt,nod[1][i],inf);
}
}
sta[0][++top[0]] = x.c;
nod[0][top[0]] = nodecnt;
}
}
int n,k;
inline bool check(int mid){
top[0] = top[1] = 0;max1 = 0;
memset(head,0,sizeof head);
cnt = 1;val = 0;nodecnt = 0;
S = ++ nodecnt;T = ++ nodecnt;
int max1 = 0;
rep(i,1,n){
if(a[i].l <= mid && a[i].c != 1) insert(a[i]);
if(a[i].l <= mid && a[i].c == 1){
max1 = max(max1,a[i].p);
}
}
if(max1 != 0) insert(Node(max1,1));
val -= dinic();
return val >= k;
}
int main(){
read(n);read(k);
liner(200000);
rep(i,1,n){
read(a[i].p);read(a[i].c);read(a[i].l);
}sort(a+1,a+n+1,cmp);
int l = 1,r = n,ans = -1;
while(l <= r){
int mid = l+r >> 1;
if(check(mid)) ans = mid,r = mid-1;
else l = mid+1;
}printf("%d\n",ans);
return 0;
}

Codeforces 808F. Card Game的更多相关文章

  1. AC日记——Card Game codeforces 808f

    F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...

  2. Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  3. Codeforces 1156F Card Bag(概率DP)

    设dp[i][j]表示选到了第i张牌,牌号在j之前包括j的概率,cnt[i]表示有i张牌,inv[i]表示i在mod下的逆元,那我们可以考虑转移,dp[i][j]=dp[i-1][j-1]*cnt[j ...

  4. CodeForces 462B Appleman and Card Game(贪心)

    题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...

  5. codeforces 893D Credit Card 贪心 思维

    codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...

  6. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  7. Codeforces 388C Fox and Card Game (贪心博弈)

    Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...

  8. Codeforces 106A:Card Game

    题目链接http://codeforces.com/contest/106/problem/A 题意:一套牌有S.H.D.C四种花色,按等级分成6.7.8.9.T.J.Q.K.A.每次选出一个花色作为 ...

  9. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card

    D - Travel Card 思路:dp,类似于单调队列优化. 其实可以写的更简单... #include<bits/stdc++.h> #define LL long long #de ...

随机推荐

  1. val() attr('value')

    val() 只能更改输入框内的值,能更改value属性, 在浏览器中体现不出value被改变 attr('value') 都可以 谷歌浏览器 val,attr都能获取输入框最新的value值

  2. linux练习命令

    任务一:按要求完成以下操作1)显示日期格式2)在/tmp/下新建目录test ,并指定权限6643)显示环境变量path,但将/root加入到$PATH中4)用cat显示/etc/passwd,并打印 ...

  3. asp.net 下载图片

    public class DownLoad : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Res ...

  4. linux下从源代码安装git的问题(install from source)

    安装环境:centos7.2 安装依赖包: yum install -y gcc .el7..x86_64 openssl-devel.x86_64 yum install -y curl.x86_6 ...

  5. 数字代币ICO

    随着比特币.莱特币.以太币的逐步兴起,越来越多的数字代币开始衍生,虚拟货币扑朔迷离,一不小心就被人割了韭菜..... 从荷兰IPO的故事说起 400多年前,西方有一群精英海盗开了一家公司.为了顺利拓展 ...

  6. apache2.4配置ssl

    1,yum 安装openssl和openssl-devel,httpd-devel2,生成证书(也可以从公司的证书颁发机构获取): #建立服务器密钥 openssl genrsa -des3 > ...

  7. Spring中操作Hibernate的几种方式

    1.直接操作模版方式HQL: //通过spring的模版方式来操作Hibernate的HQL语句 return this.getHibernateTemplate().find("from ...

  8. 关于Hystrix

    RPC远程调用过程中如何防止服务雪崩效用 微服务中如何保护服务 Hystrix是一个微服务中关于服务保护框架,在分布式中能够实现对服务容错.出错之后的预备方案 背景 在今天,基于SOA的架构已经大行其 ...

  9. ZooKeeper-安装和运行

    ZooKeeper安装和运行 1. 下载安装包 zookeeper-3.4.9.tar.gz 2. 解压 tar -zxvf zookeeper-3.4.9.tar.gz ZooKeeper提供了几个 ...

  10. Android fill_parent和wrap_content分析

    fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕.(这是不管内容大小,直接适应整个屏幕的大小,例长度设置了这,就只有长度适应屏幕的长度) wrap_content布局指根据视图内部内 ...