大意: $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. [BZOJ1191]超级英雄Hero

    Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的 多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确 ...

  2. mysql mod() 获取余数

    mysql> ,); +-----------+ | mod(,) | +-----------+ | | +-----------+ row in set (0.00 sec)

  3. 【算法编程 C++ Python】根据前序遍历、中序遍历重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  4. hdu1501 Zipper[简单DP]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...

  5. [C++] const和mutable关键字使用方法

    const 修饰的变量为常成员变量,表示此变量不能被修改赋值,并且构造函数中只能用初始化列表的方式初始化,其他初始化方式都是错误的 const 修饰的函数为常成员函数,表示此函数中只能读取成员变量,不 ...

  6. Git创建与合并分支,撤销修改

    git回滚到指定版本并推送到远程分支(撤销已提交的修改,并已push) git reset --hard <commit ID号> git push -f git回滚到上一个版本并推送到远 ...

  7. SQL Server case when 实现分类汇总

    case when 实现分类汇总

  8. tushare包使用案例

    Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工 到 数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据,为他们在数据获取 ...

  9. 改进初学者的PID-测量的比例编码

    最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...

  10. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...