题目大意

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

你不能选取等级高于\(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. Yii2 注册表单验证规则 手机注册时候使用短信验证码

    public function rules() { return [ ['username', 'filter', 'filter' => 'trim'], ['username', 'requ ...

  2. 一步步讲解如何开源自己的项目到GitHub上,Mac机示例

    如果你有自己的优秀项目,想要分享给大家,那GitHub会是你正确的选择.如何才能将自己的项目上传到GitHub上呢?接下来请一步一步跟着走. 需要准备的资源: 1.一台Mac机 2.安装git客户端( ...

  3. css系列(5)css的运用(一)

        从本节开始介绍css配合html可以达到的一些效果.     (1)导航栏: <html> <head> <title>示例5.1</title> ...

  4. $用python实现快速排序算法

    本文主要介绍用python实现基本的快速排序算法,体会一下python的快排代码可以写得多么简洁. 1. 三言两语概括算法核心思想 先从待排序的数组中找出一个数作为基准数(取第一个数即可),然后将原来 ...

  5. WEB-INF有关的目录路径总结、转向方式: forward 重定向方式: Redirect

    WEB-INF有关的目录路径总结 1.资源文件只能放在WebContent下面,如 CSS,JS,image等.放在WEB-INF下引用不了. 2.页面放在WEB-INF目录下面,这样可以限制访问,提 ...

  6. Windos Server 2008 配置定时清理任务

    系统环境:Windos 2008 R2 x64 位 实施方案:自动清理超过两周的备份系统文件. 编写自动清理脚本..bat文件后缀. 打开计划任务

  7. 单周期CPU设计

    终于有点时间了,恰好多周期的设计也已经完成,其实只想写写多周期的,无奈单周期补上才好,哈哈哈~ —————+—————黄金分割线—————+————— 首先要理解什么叫单周期CPU(与后面多周期CPU ...

  8. javax.mail.MessagingException: Could not connect to SMTP host: smtp.xdf.cn

    1.问题描述:关于使用Java Mail进行邮件发送,抛出Could not connect to SMTP host: xx@xxx.com, port: 25的异常可能: 当我们使用Java Ma ...

  9. spring配置hibernate映射文件-------通配符

    <!-- 这里一定要注意是使用spring的mappingLocations属性进行通配的 -->      <property name="mappingLocation ...

  10. CentOS 7 安装 docker-compose

    compose是用来在docker中定义和运行复杂应用的小工具,比如在一个文件中定义多个容器,只用一行命令就可以让一切就绪并运行. 安装pip: 这里显示,找不到相应的包?? 说没有python-pi ...