大意: $n$个骑士, 第$i$个骑士若加入光明阵营, 那么能力值$L_i$, 加入黑暗阵营, 能力值$D_i$. 给定$m$个限制$(u_i,v_i)$, 表示$u_i,v_i$不能在同一阵营. 求一种划分方案, 使得能力值最大值减最小值最小.

对于一个连通块, 如果不是二分图, 那么无解. 否则的话这个连通块最大值最小值只有两种情况, 枚举最大值, 求出最小值的最大可能值更新答案即可.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e6+50;
int n,m,ok,vis[N],l[N],d[N],mi[N],ID[N],cur[N];
vector<int> g[N];
pii f[N],A,B; void dfs(int x, int c) {
vis[x] = c;
if (c) {
A.x = min(A.x,l[x]);
A.y = max(A.y,l[x]);
B.x = min(B.x,d[x]);
B.y = max(B.y,d[x]);
}
else {
A.x = min(A.x,d[x]);
A.y = max(A.y,d[x]);
B.x = min(B.x,l[x]);
B.y = max(B.y,l[x]);
}
for (int y:g[x]) {
if (vis[y]<0) dfs(y,c^1);
else if (vis[y]==c) ok=0;
}
} void work() {
scanf("%d%d",&n,&m);
REP(i,1,n) vis[i]=-1,g[i].clear();
REP(i,1,m) {
int u, v;
scanf("%d%d",&u,&v);
g[u].pb(v),g[v].pb(u);
}
REP(i,1,n) scanf("%d%d",l+i,d+i);
ok = 1;
vector<pii> events;
int cnt = 0;
multiset<int> s;
REP(i,1,n) if (vis[i]<0) {
A = B = {1e9,0};
dfs(i, 0);
if (!ok) return puts("IMPOSSIBLE"),void();
s.insert(cur[i]=-INF);
ID[cnt]=i,mi[cnt]=A.x,events.pb(pii(A.y,cnt)),++cnt;
ID[cnt]=i,mi[cnt]=B.x,events.pb(pii(B.y,cnt)),++cnt;
}
sort(events.begin(),events.end());
int ans = 1e9;
for (auto &p:events) {
s.erase(s.find(cur[ID[p.y]]));
cur[ID[p.y]] = max(cur[ID[p.y]], mi[p.y]);
s.insert(cur[ID[p.y]]);
ans = min(ans, p.x-*s.begin());
}
printf("%d\n", ans);
} int main() {
int t=rd();
REP(i,1,t) {
printf("Case %d: ",i);
work();
}
}

Gym 102055B Balance of the Force的更多相关文章

  1. 2018CCPCFINAL B Balance of the Force 枚举最大值

    题意 n个人能选择黑暗面和光明面,选择两个面分别能获得\(L_i\)和\(R_i\)的力量,有m对人不能选择同一面,问n个人的力量中的最大值-最小值尽可能小为多少. \(1<=n<=2\t ...

  2. 模拟赛小结:2018 China Collegiate Programming Contest Final (CCPC-Final 2018)

    比赛链接:传送门 跌跌撞撞6题摸银. 封榜后两题,把手上的题做完了还算舒服.就是罚时有点高. 开出了一道奇奇怪怪的题(K),然后ccpcf银应该比区域赛银要难吧,反正很开心qwq. Problem A ...

  3. martini-能量最小化参数(mdp文件)

    1 ; 2 ; STANDARD MD INPUT OPTIONS FOR MARTINI 2.x 3 ; Updated 02 feb 2013 by DdJ 4 ; 5 ; for use wit ...

  4. martini-md参数(mdp文件)

    输入参数:一个典型的mdp文件 1 ; 2 ; STANDARD MD INPUT OPTIONS FOR MARTINI 2.x 3 ; Updated 02 feb 2013 by DdJ 4 ; ...

  5. 每日英语:Boost Your Balance; Avoid Falls

    If you find yourself needing to sit down to take off your shoes, it might be time to start paying at ...

  6. 强化学习之MountainCarContinuous(注册自己的gym环境)

    目录 1. 问题概述 2. 环境 2.1 Observation & state 2.2 Actions 2.3 Reward 2.4 初始状态 2.5 终止状态- Episode Termi ...

  7. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  8. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  9. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

随机推荐

  1. POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)

    水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:  ...

  2. 最近公司遇到了APR攻击,顺便了解一下知识

    原因及背景 最近公司遇到了APR攻击导致整个公司研发部.测试部.客服部.工程部等几个部门统一无法上网,TV(team viewer)无法使用,部署在公网的B/S架构系统系统无法访问,开发代码上传和下载 ...

  3. Linux最大线程数限制

    开始以为是内存不足导致无法创建线程,把jvm的-Xms,-Xmx的2个参数都加大一倍:-Xms2048m -Xmx2048m.把-Xss参数调小,还是启动失败.应该是系统方面的限制了,这台机器上搞了1 ...

  4. koa koa-static 静态资源中间件

    koa-static介绍 在网络请求中,请求往往分成两种类型,一种是静态资源,直接从服务器的文件存储中读取,一种是动态资源,一般需要先从数据库获取数据,然后经过一定的处理,最后返回给客户端. koa- ...

  5. 给lol人物模型添加动画库(mixamo)中的动画

    参考链接:https://www.bilibili.com/video/av61771773/?p=4   作为一个懒人,给提取出来的lol人物模型亲手制作动画是不存在的,但是又想给人物模型应用新的有 ...

  6. cocos creator策略类手游开发教程

    目录 大小6.83GB,Mp4格式,语言:中文 扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主

  7. 如何从OA系统批量整理出邮箱地址,并导入到Foxmail 地址薄中?

    一.打开某位leader的OA,点击查看“下属” a. 将所有的下属信息 --- 全选 --- 复制 --- 粘贴到 excel 表格中 b. 分别提取“姓名” 和 “邮箱”地址信息,结合notepa ...

  8. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  9. 基于Wiremock创建Mock Service平台(转)

    本文链接:https://blog.csdn.net/liuchunming033/article/details/52399397                                   ...

  10. maven项目打包跳过单元测试

    在pom.xml中添加一下代码: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifact ...