题目: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的更多相关文章

  1. CF786B Legacy(线段树优化建边)

    模板题CF786B Legacy 先说算法 如果需要有n个点需要建图 给m个需要建边的信息,从单点(或区间内所有点)向一区间所有点连边 如果暴力建图复杂度\(mn^2\) 以单点连向区间为例,在n个点 ...

  2. Chance – 功能强大的 JavaScript 随机数生成类库

    Chance 是一个基于 JavaScript 的随机数工具类.可以生成随机数字,名称,地址,域名,邮箱,时间等等,几乎网站中使用的任何形式的内容都能够生成.这个随机数工具可以帮助减少单调的测试数据编 ...

  3. poj 1698 Alice‘s Chance

    poj 1698  Alice's Chance 题目地址: http://poj.org/problem?id=1698 题意: 演员Alice ,面对n场电影,每场电影拍摄持续w周,每周特定几天拍 ...

  4. 【性能诊断】九、并发场景的性能分析(windbg案例,Fist Chance Exception/Crash dump)

          经常会碰到这样的场景,自测及单单点的测试时没有任何问题,但在并发环境或生产环境下有时出现没规律的异常.报错等情况.在代码中增加日志是其中一种解决方式:抓取指定异常时的dump,通过wind ...

  5. (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 ...

  6. Alice's Chance POJ - 1698(按时间点建边)

    Alice's Chance Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7791   Accepted: 3174 De ...

  7. Codeforces 1045A Last chance 网络流,线段树,线段树优化建图

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1045A.html 题目传送们 - CF1045A 题意 你有 $n$ 个炮,有 $m$ 个敌人,敌人排成一 ...

  8. 2018.07.06 POJ1698 Alice's Chance(最大流)

    Alice's Chance Time Limit: 1000MS Memory Limit: 10000K Description Alice, a charming girl, have been ...

  9. 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. 做不想做 ...

随机推荐

  1. linux 软连接的使用

    软连接是linux中一个常用命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接. 具体用法是:ln -s 源文件 目标文件. 当 我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需 ...

  2. debian下使用shell脚本时出现了 declare:not found 解决方法

    问题:出现declare:not found的提示 解决:原来,UBUNTU用的是dash(后来证明这个其实这个不是错误的原因:从#!/bin/bash到#!/bin/dash,依旧无法运行,在这写出 ...

  3. 20191105 《Spring5高级编程》笔记-第5章

    第5章 Spring AOP 面向切面编程(AOP)是面向对象编程(OOP)的补充.AOP通常被称为实施横切关注点的工具.术语横切关注点是指应用程序中无法从应用程序的其余部分分解并且可能导致代码重复和 ...

  4. Trapping Rain Water I && II

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. git日常开发中的使用

    作者:python技术人 博客:https://www.cnblogs.com/lpdeboke 1.在远程新建一个仓库,可以使github.gitlib或者bitbucket,这里以bitbucke ...

  6. SpringBoot 整合 MybatisPlus (项目的创建及简单的单表查询)

    添加依赖 <!--mybatis-plus的springboot支持--> <dependency> <groupId>com.baomidou</group ...

  7. 小白学Python(12)——pyecharts ,生成词云图 WordCloud

    WordCloud(词云图) from pyecharts import options as opts from pyecharts.charts import Page, WordCloud fr ...

  8. 问题 D: 小k的硬币问题

    问题 D: 小k的硬币问题 时间限制: 1 Sec  内存限制: 128 MB提交: 21  解决: 5[提交] [状态] [命题人:jsu_admin] 题目描述 小k和小p一起玩一个游戏,有n堆硬 ...

  9. 通过设置代理解决AndroidStudio无法下载gradle问题

    一.AndroidStudio代理 我们平时在使用android studio时,难免需要从android官网下载一些项目运行所需要的SDK文件,但是因为android官网在国外,访问起来会比较慢,所 ...

  10. Object of type 'ndarray' is not JSON serializable

    Object of type 'ndarray' is not JSON serializable import numpy as np import json arr=np.asarray([345 ...