题目大意

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

你不能选取等级高于\(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. Python异步非阻塞IO多路复用Select/Poll/Epoll使用,线程,进程,协程

    1.使用select模拟socketserver伪并发处理客户端请求,代码如下: import socket import select sk = socket.socket() sk.bind((' ...

  2. no xxx find in java.library.path

    JAVA系统运行时候load native lib时候会遇到下面错误,如 java.lang.UnsatisfiedLinkError: no JSTAF in java.library.path这可 ...

  3. PHP扩展模块Pecl、Pear以及Perl的区别

    一.简短总结:pear:一个书写的比较规范,国外较流行的工具箱代码集pecl:php扩展包,但不属于php基本扩展范围perl:一种早于php出现的脚本级语言,php借鉴了他的正则表达式部分 二.Pe ...

  4. mini2440移植uboot 2014.04(五)

    代码上传到github上:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440 前几篇博文: <mini2440移植uboot 2014.04 ...

  5. MapReduce-文本输入

    1.TextInputFormat TextInputFormat是默认的InputFormat.每条记录是一行输入.键是LongWritable类型,存储该行在整个文件中的字节偏移量.值是这行的内容 ...

  6. IOS 被拒 关于 iPhone running iOS 10.3.1 on Wi-Fi connected to an IPv6 network.

    问题: Guideline 2.1 - Performance Thank you for your resubmission. However, we discovered one or more ...

  7. SQL server 2008 T-sql 总结

    数据库的实现 1.添加数据:insert [into] 表名 (字段1,字段2,···) values (值1,值2,····)     其中,into可选. 2.修改数据:update 表名 set ...

  8. SpringMVC中使用ModelAndView遇到的问题

    本文记录我在SpringMVC中使用ModelAndView,添加模型数据到ModelAndView中时遇到的问题: 1.jsp页面用EL表达式来获取值时直接显示EL表达式,JSP不解析EL表达式: ...

  9. Android 开发工具(android studio )安装中的问题记录

    第一个问题,下载安装android studio . 由于国内无法通过正常方式访问谷歌官网,所以下载的确是个问题,在我仔细寻找下,发现下面两个网站可以下载: 第一个:http://www.androi ...

  10. 新东方雅思词汇---7.2、warrant

    新东方雅思词汇---7.2.warrant 一.总结 一句话总结: warr+ant 英 ['wɒr(ə)nt]  美 ['wɔrənt]  n. 根据:证明:正当理由:委任状 vt. 保证:担保:批 ...