[CF1045A] Last chance
题目:Last chance
传送门:http://codeforces.com/contest/1045/problem/A
分析:
1)有$n$个敌方飞船,己方有$m$个武器,有三种类型。
2)$第0种$:能攻击一艘 编号属于大小为$ki$的集合的飞船。显然,这是一个二分图最大匹配,可以暴力建边。
3)$第1种$:能攻击一艘 编号在$[l_i,r_i]$区间内的飞船。显然,这还是二分图最大匹配,但是,“武器到区间所有飞船建边”这种复杂度就不可接受了。
4)点与区间所有点建边可以采用线段树优化。
5)$第2种$:能攻击三艘飞船中的0艘或者两艘,编号分别为$ai,bi,ci$。题目保证了一艘飞船至多被包括在一个这样的三元组中,也就是所有第2种武器的攻击集合互不相交。
6)贪心攻击掉两艘。对每个三元组建一个辅助点让它们的流量和小于等于$1$.
7)很明显的网络流了。
8)对于$第0种$武器:源点到武器建一条容量为1的边,武器到集合中每一个飞船建一条容量为1的边。
9)对于$第1种$武器:线段树优化点到区间建边:对于这$m$个人先建一棵线段树,父亲节点向儿子节点连容量为$INF$的边,最后叶子结点向对应的飞船连容量为$1$的边。这样给$第1种$武器对应连边的时候直接给区间连边就行了。
10)对于第2种武器:贪心掉$a_i,b_i$,$a_i,b_i$不必向汇点连边,武器节点向$a_i,b_i$连接一条反向容量为1的边,向$c_i$连一条容量为1的边,这样就保证了$a_i,b_i,c_i$要么被选到2个,要么被选到3个。
11)题目还要输出方案。可以采用类似退流的方式。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxN=,INF=1e9+;
int n,m,ans=,vis[maxN];
struct Edge{int to,f,next;};
struct NetWorkFlow{
int en,fi[maxN];
Edge e[maxN];
void addE(int v,int u,int f){
e[++en].to=u;e[en].f=f;e[en].next=fi[v];fi[v]=en;
e[++en].to=v;e[en].f=;e[en].next=fi[u];fi[u]=en;
}
int tot,S,T,cnt[maxN],dis[maxN];
int sap(int t,int delta){
if(t==T)return delta;
int sum=,mindis=tot+;
for(int i=fi[t];i;i=e[i].next){
if(e[i].f && dis[t]==dis[e[i].to]+){
int save=sap(e[i].to,std::min(e[i].f,delta-sum));
sum+=save;
e[i].f-=save;
e[i^].f+=save;
if(dis[S]>=tot+ || delta==sum)return sum;
}
if(e[i].f && dis[e[i].to]<mindis)mindis=dis[e[i].to];
}
if(!sum){
if(!--cnt[dis[t]])dis[S]=tot+;
else ++cnt[dis[t]=mindis+];
}
return sum;
}
void Sol(){
cnt[]=tot+;
while(dis[S]<tot+)
ans+=sap(S,INF);
}
}NW;
struct SegmentTree{
int id[maxN];
void Init(int v,int l,int r){
id[v]=++NW.tot;
if(l==r){NW.addE(id[v],l,);return;}
int mid=(l+r)>>;
Init(v<<,l,mid);NW.addE(id[v],id[v<<],INF);
Init(v<<|,mid+,r);NW.addE(id[v],id[v<<^],INF);
}
void addE(int v,int l,int r,const int &L,const int &R,const int& u){
if(L<=l && r<=R){NW.addE(u,id[v],);return;}
int mid=(l+r)>>;
if(L<=mid)addE(v<<,l,mid,L,R,u);
if(mid< R)addE(v<<|,mid+,r,L,R,u);
}
}ST;
int Re(){
int ch='@',x=;
for(;ch< || <ch;ch=getchar());
for(;<ch && ch<;ch=getchar())x=x*+ch-;
return x;
}
int Beg,tmp;
map<int,int>mp[maxN];
void GetAns(int v){
if(v>=Beg){tmp=v-Beg+;return;}
auto it=mp[v].begin();
GetAns(it->first);
--it->second;
if(!it->second)mp[v].erase(it);
}
int main(){
n=Re();m=Re();
NW.en=;memset(NW.fi,,sizeof NW.fi);
NW.tot=m;NW.S=++NW.tot;NW.T=++NW.tot;
ST.Init(,,m);Beg=NW.tot+;
for(int i=,op;i<=n;++i){
++NW.tot;
op=Re();
if(op==){
NW.addE(NW.S,NW.tot,);
for(int k=Re(),x;k--;){x=Re();NW.addE(NW.tot,x,);}
}else if(op==){
NW.addE(NW.S,NW.tot,);
int L=Re();int R=Re();
ST.addE(,,m,L,R,NW.tot);
}else{
int a=Re();int b=Re();int c=Re();
vis[a]=vis[b]=;ans+=;
NW.addE(NW.tot,a,);NW.e[NW.en].f=;
NW.addE(NW.tot,b,);NW.e[NW.en].f=;
NW.addE(NW.tot,c,);
}
}
for(int i=;i<=m;++i)if(!vis[i])NW.addE(i,NW.T,);
NW.Sol();
printf("%d\n",ans);
for(int i=;i<=NW.tot;++i)
for(int j=NW.fi[i];j;j=NW.e[j].next)
if((j&)&&NW.e[j].f)
mp[i][NW.e[j].to]=NW.e[j].f;
for(int i=;i<=m;++i){
bool f=true;
for(int j=NW.fi[i];j&&f;j=NW.e[j].next)if(NW.e[j].to==NW.T&&NW.e[j].f)f=false;
if(f){
GetAns(i);
printf("%d %d\n",tmp,i);
}
}
return ;
}
[CF1045A] Last chance的更多相关文章
- CF786B Legacy(线段树优化建边)
模板题CF786B Legacy 先说算法 如果需要有n个点需要建图 给m个需要建边的信息,从单点(或区间内所有点)向一区间所有点连边 如果暴力建图复杂度\(mn^2\) 以单点连向区间为例,在n个点 ...
- Chance – 功能强大的 JavaScript 随机数生成类库
Chance 是一个基于 JavaScript 的随机数工具类.可以生成随机数字,名称,地址,域名,邮箱,时间等等,几乎网站中使用的任何形式的内容都能够生成.这个随机数工具可以帮助减少单调的测试数据编 ...
- poj 1698 Alice‘s Chance
poj 1698 Alice's Chance 题目地址: http://poj.org/problem?id=1698 题意: 演员Alice ,面对n场电影,每场电影拍摄持续w周,每周特定几天拍 ...
- 【性能诊断】九、并发场景的性能分析(windbg案例,Fist Chance Exception/Crash dump)
经常会碰到这样的场景,自测及单单点的测试时没有任何问题,但在并发环境或生产环境下有时出现没规律的异常.报错等情况.在代码中增加日志是其中一种解决方式:抓取指定异常时的dump,通过wind ...
- (C# Debug)A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll
Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred ...
- Alice's Chance POJ - 1698(按时间点建边)
Alice's Chance Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7791 Accepted: 3174 De ...
- Codeforces 1045A Last chance 网络流,线段树,线段树优化建图
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1045A.html 题目传送们 - CF1045A 题意 你有 $n$ 个炮,有 $m$ 个敌人,敌人排成一 ...
- 2018.07.06 POJ1698 Alice's Chance(最大流)
Alice's Chance Time Limit: 1000MS Memory Limit: 10000K Description Alice, a charming girl, have been ...
- You can fail at what you don't want, so you might as well take a chance on doing what you love.
You can fail at what you don't want, so you might as well take a chance on doing what you love. 做不想做 ...
随机推荐
- 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...
- C++ 命名管道示例
想做一个 Hook CreateFile 重定向到内存的功能,貌似可以假借命名管道实现这个功能.不熟悉命名管道,做了几个demo,如下: Server: // NamedPipeServer.cpp ...
- GIt 工作区与暂存区
转载:https://www.liaoxuefeng.com/wiki/896043488029600/897271968352576 工作区与暂存区 工作区(Working Directory) 就 ...
- python 正则表达式 re.findall &re.finditer
语法: findall 搜索string,以列表形式返回全部能匹配的子串 re.findall(pattern, string[, flags]) finditer 搜索string,返回一个顺序访问 ...
- 002/Node.js(Mooc)--Http知识
1.什么是Http 菜鸟教程:http://www.runoob.com/http/http-tutorial.html 视频地址:https://www.imooc.com/video/6713 h ...
- ceph部署-常用命令
常用命令:1.ceph healthceph -s 2.ceph osd treeceph osd lspoolsceph osd pool [poolname] rbd pg_numceph osd ...
- SpringBoot(一) -- SpringBoot入门
一.简介 Spring Boot来简化Spring应用开发,约定大于配置,去繁从简,just run就能创建一个独立的,产品级别的应用. 1.快速创建独立运行的Spring项目以及与主流框架集成; 2 ...
- Spring Boot 集成 Ehcache 缓存,三步搞定!
作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...
- 接口测试工具——postman
Postman 之前是作为Chrome 的一个插件,现在要下载应用才能使用. 以下是postman 的界面: 各个功能区的使用如下: 快捷区: 快捷区提供常用的操作入口,包括运行收藏夹的一组测试数据, ...
- 一篇包含了react所有基本点的文章
去年,我写了一本关于学习React.js的小书,原来是大约100页. 今年我要挑战自己,把它归纳为一篇文章. 本文不会涵盖什么是React,或者为什么要学习它. 相反,这是面向已经熟悉JavaScri ...