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\)为左端点的区 ...
随机推荐
- PyCryptodome安装使用方法
PyCryptodome是PyCrypto的一个分支.基于PyCrypto2.6.1,多了以下特性: Authenticated encryption modes (GCM, CCM, EAX, SI ...
- (六)Hibernate的增删改查操作(3)
一.在Hibernate中使用原生SQL语句 sql语句面向的是数据库,所以sql语句中对应的不再是bean了,比如sql="select * from user" 在hql中 ...
- 【Transact-SQL】找出不包含字母、不包含汉字的数据
原文:[Transact-SQL]找出不包含字母.不包含汉字的数据 测试的同事,让我帮忙写个sql语句,找出表中xx列不包含汉字的行. 下面的代码就能实现. IF EXISTS(SELECT * FR ...
- 八、wepy代码规范
变量与方法尽量使用驼峰式命名,并且注意避免使用$开头. 以$开头的标识符为WePY框架的内建属性和方法,可在JavaScript脚本中以this.的方式直接使用,具体请参考API文档. 小程序入口.页 ...
- matlab cell
cell元包是matlab中提供的一种数据类型,功能强大. 关于cell的创建: 1.跟一般创建举证一样,直接使用C = {A B D E}这种形式,不过这里把"[]"改成了}&q ...
- java注解日志记录到数据库
1. pom添加依赖包 <!--添加aop依赖--><dependency> <groupId>org.springframework.boot</group ...
- js写guess网页
(一)布局 猜前 -> 猜后 (二)明确实现功能和具体实现: 1.网页生 ...
- CVE-2018-2879 - anniversary
For the anniversary of the discovery of CVE-2018-2879 by Sec Consult (https://sec-consult.com/en/blo ...
- Oracle11g数据库导入Oracle10g操作成功
转自:https://wenku.baidu.com/view/1b652b57f7ec4afe04a1dfb8.html
- string+DFS leetcode-17.电话号码下的字母组合
题面 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that ...