2018CCPCFINAL B Balance of the Force 枚举最大值
题意
n个人能选择黑暗面和光明面,选择两个面分别能获得\(L_i\)和\(R_i\)的力量,有m对人不能选择同一面,问n个人的力量中的最大值-最小值尽可能小为多少。
\(1<=n<=2\times 10^5\)
\(0<=m<=2\times 10^5\)
\(1<=L_i,D_i<=10^9\)
分析
先二分图染色,每个连通块的最大值和最小值有两种方案,设一共有k个连通块,将所有方案按最大值升序排序,去枚举最大值mx,用线段树维护所有连通块的最小值,若前\(i\)个方案能构成\(k\)个连通块,查询\(k\)个连通块的最小值的最小值\(mn\),更新答案\(ans=min(ans,mx-mn)\),若前\(i-1\)个方案中有第\(i\)个方案的孪生方案,先删去第\(i\)个方案的孪生方案的最小值,更新答案后再将当前方案所在的连通块的最小值更新为两种方案较大的最小值。
Code
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define ll long long
using namespace std;
const int inf=1e9+10;
const int mod=1e9+7;
const int maxn=4e5+10;
int T,n,m,tot,sz,col[maxn],D[maxn][2],vis[maxn];
vector<int>g[maxn];
struct ppo{
int mn,mx,i;
bool operator <(const ppo &r) const{
return mx<r.mx;
}
}a[maxn];
bool dfs(int u,int o){
col[u]=o;
a[tot].mn=min(a[tot].mn,D[u][o]);
a[tot].mx=max(a[tot].mx,D[u][o]);
a[tot-1].mn=min(a[tot-1].mn,D[u][o^1]);
a[tot-1].mx=max(a[tot-1].mx,D[u][o^1]);
for(int x:g[u]){
if(col[x]==-1){
if(!dfs(x,o^1)) return false;
}else if(col[x]!=o^1) return false;
}
return true;
}
int tr[maxn<<2];
void bd(int l,int r,int p){
tr[p]=inf;
if(l==r) return;
int mid=l+r>>1;
bd(lson);bd(rson);
}
void up(int x,int l,int r,int p,int k){
if(l==r) return tr[p]=k,void();
int mid=l+r>>1;
if(x<=mid) up(x,lson,k);
else up(x,rson,k);
tr[p]=min(tr[p<<1],tr[p<<1|1]);
}
int qy(int x,int l,int r,int p){
if(l==r) return tr[p];
int mid=l+r>>1;
if(x<=mid) return qy(x,lson);
else return qy(x,rson);
}
int main(){
//ios::sync_with_stdio(false);
//freopen("in","r",stdin);
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
scanf("%d%d",&n,&m);tot=sz=0;
for(int i=0;i<=n;i++) g[i].clear(),col[i]=-1,vis[i]=0;
for(int i=1,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
g[a].pb(b);g[b].pb(a);
}
for(int i=1;i<=n;i++){
scanf("%d%d",&D[i][0],&D[i][1]);
}
int flag=1;
for(int i=1;i<=n&&flag;i++) if(col[i]==-1){
++sz;
a[++tot]={inf,0,sz};
a[++tot]={inf,0,sz};
if(!dfs(i,0)) flag=0;
}
printf("Case %d: ",cas);
if(!flag){
puts("IMPOSSIBLE");
continue;
}
sort(a+1,a+tot+1);
bd(1,sz,1);
int sum=0,ans=inf;
for(int i=1;i<=tot;i++){
if(!vis[a[i].i]) sum++,vis[a[i].i]=1;
int tmp=qy(a[i].i,1,sz,1);
if(sum==sz){
up(a[i].i,1,sz,1,a[i].mn);
ans=min(ans,a[i].mx-tr[1]);
}
if(tmp==inf) tmp=0;
up(a[i].i,1,sz,1,max(tmp,a[i].mn));
}
printf("%d\n",ans);
}
return 0;
}
2018CCPCFINAL B Balance of the Force 枚举最大值的更多相关文章
- Gym 102055B Balance of the Force
大意: $n$个骑士, 第$i$个骑士若加入光明阵营, 那么能力值$L_i$, 加入黑暗阵营, 能力值$D_i$. 给定$m$个限制$(u_i,v_i)$, 表示$u_i,v_i$不能在同一阵营. 求 ...
- POJ 1837 Balance(01背包变形, 枚举DP)
Q: dp 数组应该怎么设置? A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数 题意: 有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣. 2 4 - ...
- enum操作--获取枚举里的最大值
一个应用系统,如果程序里没有任何enum的使用,我认为它的可读性是有待商榷的. 求枚举里的最大/最小枚举值, 其实是对Array进行操作: enum EnumTest { ddd = , eee } ...
- Java枚举类使用
用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...
- Java 枚举(enum) 详解7种常见的用法
Java 枚举(enum) 详解7种常见的用法 来源 https://blog.csdn.net/qq_27093465/article/details/52180865 JDK1.5引入了新的类型— ...
- java枚举中常见的7中用法
2016年08月11日 11:14:45 李学凯 原文链接https://blog.csdn.net/qq_27093465/article/details/52180865 JDK1.5引入了新的 ...
- 模拟赛小结:2018 China Collegiate Programming Contest Final (CCPC-Final 2018)
比赛链接:传送门 跌跌撞撞6题摸银. 封榜后两题,把手上的题做完了还算舒服.就是罚时有点高. 开出了一道奇奇怪怪的题(K),然后ccpcf银应该比区域赛银要难吧,反正很开心qwq. Problem A ...
- Longest XXX
Longest Common Substring Brute Force 遍历a和b所有位置的组合,向后延伸,直到遇到两个不同的字符,复杂度是\(n^3\)级别. class Solution { p ...
- 题解 english
传送门 好题 肝完这题感觉头巨痛 首先\(n \leqslant 1000\)的部分可以\(n^2\)单调队列,有30pts 然后考场上魔改了下单调栈,让它能顺便维护出以\(1~i-1\)为左端点的区 ...
随机推荐
- (转)从0移植uboot (四) _点亮调试LED
这一节主要讨论1个问题:点灯.点灯是实际开发中,特别是裸板开发中常见的调试手段,相当于主机开发中漫天飞舞的printf/printk.为了追踪程序的现场执行情况,很多时候我们都使用点一个灯的方法来进行 ...
- STM32之ADC实例(基于DMA方式)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zouleideboke/article/details/75112224 ADC简介: ADC(An ...
- java之集合那些事
集合概述: 集合和数组都可以保存多个对象,但是数组的长度不可变,集合可以保存数量变化的数据.java中的集合类主要由两个接口派生出,Collection和Map Collection接口和Iterat ...
- excelize
// 参考:https://gitee.com/xurime/excelize // github.com/360EntSecGroup-Skylar/excelize
- springboot 整合 web 项目找不到 jsp 文件
今天遇到一个问题,就是使用springboot整合web项目的时候,怎么都访问不到 \webapp\WEB-INF\jsp\index.jsp 页面.这个问题搞了半天,试了各种方式.最后是因为在启动的 ...
- Windows 批处理 bat 开启 WiFi 菜单选项 设置ID PWD
@echo off rem 设置标题 title windows 7 无线热点设置 author:humi rem 设置背景颜色 color 1E :: 设置窗口大小 mode con: cols=1 ...
- JS基础_构造函数修改
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- js之拖拽事件
js之拖拽事件 api:https://www.runoob.com/jsref/event-ondrag.html 拖拽事件是js原生的事件,使用时在div上添加 draggable="t ...
- webbrowser 屏蔽脚本错误
webBrowser1.ScriptErrorsSuppressed = true
- 第二章 Django之Django安装(2)
Django 安装 1.官方发布版安装 大多数人会考虑从 http://www.djangoproject.com/download/ 下载安装最新的官方 发布版.Django 使用了 Python ...