Codeforces 808F. Card Game
题目大意
一个物品有三个属性 : 价值,键值,等级.
你不能选取等级高于\(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的更多相关文章
- AC日记——Card Game codeforces 808f
F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...
- 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 ...
- 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 ...
- CodeForces 462B Appleman and Card Game(贪心)
题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...
- codeforces 893D Credit Card 贪心 思维
codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...
- 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 ...
- 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 ...
- Codeforces 106A:Card Game
题目链接http://codeforces.com/contest/106/problem/A 题意:一套牌有S.H.D.C四种花色,按等级分成6.7.8.9.T.J.Q.K.A.每次选出一个花色作为 ...
- 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 ...
随机推荐
- PAT 天梯赛 L1-006. 连续因子 【循环】
题目链接 https://www.patest.cn/contests/gplt/L1-006 思路 输出的连续因子 的乘积 也要是这个数的因子 就每个数先找它的单因子 然后每个单因子往上一个一个遍历 ...
- UI控件之UINavigationController
ViewController1 *vc1=[[ViewController1 alloc]init]; UINavigationController *nav1=[[UINavigationContr ...
- springboot——数据层访问搭建 集成Duid连接池
springboot中默认是使用的tomcat的连接池,如果我们想要第三方的连接池,我们这么配置呢? 首先在application.yml文件中注释掉之前数据库的配置,重新用druid的方式配置: # ...
- jQuery计算器插件
在线演示 本地下载
- 20145231《Java程序设计》课程总结
20145231 <Java程序设计>课程总结 每周读书笔记链接汇总 ● 20145231<Java程序设计>第一周学习总结 ●20145231<Java程序设计> ...
- 【iOS和HTML 5交互】iOS中加载html5调用html方法和修改html5内容
近期项目开发中用到了这方面的技术了,那我们一起来看看. 1.利用webView控件加载本地html5或者网络上html5 2.设置控制器为webView的代理,遵守协议 3.实现代理方法webView ...
- poj2442优先队列
感谢 http://hi.baidu.com/%C0%B6%C9%ABarch/blog/item/f9d343f49cd92e53d7887d73.html 的博主! 思路: 我们要找到n个smal ...
- mongodb 的安装(Centor OS )
1.下载地址 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.2.tgz 2.解压.配置 tar zxvf mongodb ...
- Spring初学之注解方式配置bean
直接看代码: UserController.java package spring.beans.annotation.controller; import org.springframework.be ...
- 查找和删除倒数第n个节点的问题
class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public class NthNodeFromEnd ...