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

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

  1. #include <iostream>
  2. #include <sstream>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <string>
  10. #include <cstring>
  11. #include <bitset>
  12. #include <functional>
  13. #include <random>
  14. #define REP(i,a,n) for(int i=a;i<=n;++i)
  15. #define PER(i,a,n) for(int i=n;i>=a;--i)
  16. #define hr putchar(10)
  17. #define pb push_back
  18. #define lc (o<<1)
  19. #define rc (lc|1)
  20. #define mid ((l+r)>>1)
  21. #define ls lc,l,mid
  22. #define rs rc,mid+1,r
  23. #define x first
  24. #define y second
  25. #define io std::ios::sync_with_stdio(false)
  26. #define endl '\n'
  27. #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
  28. using namespace std;
  29. typedef long long ll;
  30. typedef pair<int,int> pii;
  31. const int P = 1e9+7, INF = 0x3f3f3f3f;
  32. ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
  33. 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;}
  34. ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
  35. 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;}
  36. //head
  37.  
  38. const int N = 1e6+50;
  39. int n,m,ok,vis[N],l[N],d[N],mi[N],ID[N],cur[N];
  40. vector<int> g[N];
  41. pii f[N],A,B;
  42.  
  43. void dfs(int x, int c) {
  44. vis[x] = c;
  45. if (c) {
  46. A.x = min(A.x,l[x]);
  47. A.y = max(A.y,l[x]);
  48. B.x = min(B.x,d[x]);
  49. B.y = max(B.y,d[x]);
  50. }
  51. else {
  52. A.x = min(A.x,d[x]);
  53. A.y = max(A.y,d[x]);
  54. B.x = min(B.x,l[x]);
  55. B.y = max(B.y,l[x]);
  56. }
  57. for (int y:g[x]) {
  58. if (vis[y]<0) dfs(y,c^1);
  59. else if (vis[y]==c) ok=0;
  60. }
  61. }
  62.  
  63. void work() {
  64. scanf("%d%d",&n,&m);
  65. REP(i,1,n) vis[i]=-1,g[i].clear();
  66. REP(i,1,m) {
  67. int u, v;
  68. scanf("%d%d",&u,&v);
  69. g[u].pb(v),g[v].pb(u);
  70. }
  71. REP(i,1,n) scanf("%d%d",l+i,d+i);
  72. ok = 1;
  73. vector<pii> events;
  74. int cnt = 0;
  75. multiset<int> s;
  76. REP(i,1,n) if (vis[i]<0) {
  77. A = B = {1e9,0};
  78. dfs(i, 0);
  79. if (!ok) return puts("IMPOSSIBLE"),void();
  80. s.insert(cur[i]=-INF);
  81. ID[cnt]=i,mi[cnt]=A.x,events.pb(pii(A.y,cnt)),++cnt;
  82. ID[cnt]=i,mi[cnt]=B.x,events.pb(pii(B.y,cnt)),++cnt;
  83. }
  84. sort(events.begin(),events.end());
  85. int ans = 1e9;
  86. for (auto &p:events) {
  87. s.erase(s.find(cur[ID[p.y]]));
  88. cur[ID[p.y]] = max(cur[ID[p.y]], mi[p.y]);
  89. s.insert(cur[ID[p.y]]);
  90. ans = min(ans, p.x-*s.begin());
  91. }
  92. printf("%d\n", ans);
  93. }
  94.  
  95. int main() {
  96. int t=rd();
  97. REP(i,1,t) {
  98. printf("Case %d: ",i);
  99. work();
  100. }
  101. }

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. windows 共享文件夹

    windows 共享文件夹 同步工作组 右键单击"计算机",选择"属性" 更改设置 单击"更改". 输入工作组 和 主机名 启计算机使更改生 ...

  2. 运行OpenGL红宝书第9版源码时Visual Studio提示“无法启动程序...ALL_BUILD。拒绝访问“的问题的解决办法

    问题描述: OpenGL红宝书第9版源码采用CMake编译后,用相应的Visual Studio(如VS2012)打开“vermilion9.sln”解决方案,并运行时Visual Studio提示“ ...

  3. 以太坊公链Geth同步

    1.安装所需基础工具 yum update -y && yum install git wget bzip2 vim gcc-c++ ntp epel-release nodejs c ...

  4. 2018 python获取动态User-Agent

    from fake_useragent import UserAgent ua = UserAgent() headers = {'User-Agent': ua.random} print(ua.r ...

  5. Ubuntu下root账户无法使用xshell远程连接解决方法

    以前使用xshell连接服务器跑程序都很顺利,但这次整了一个root账户,发现居然连不上.百度一下,发现ubuntu系统本身是拒绝root账户远程连接的. 解决办法:参考点击传送门

  6. IDEA查看接口的实现类

    查找接口的实现类: 快捷键 ctrl + alt +B 再按F2查看详细文档注解 查看类或接口的继承关系: ctrl + h

  7. net::ERR_ABORTED 404 (Not Found)

    对于按需加载(on-demand-load)或加载外部资源(external resources)(如图片.文件等)来说,webpack的配置,output.publicPath是很重要的选项.如果指 ...

  8. Metricbeat 轻量型指标采集器

    一.介绍 用于从系统和服务收集指标.从 CPU 到内存,从 Redis 到 Nginx,Metricbeat 能够以一种轻量型的方式,输送各种系统和服务统计数据. 1.系统级监控,更简洁(轻量型指标采 ...

  9. springcloud 连接docker中运行的RabbitMQ消息中间件。

    参考:https://blog.51cto.com/zero01/2173288 主要是记录几个坑: 第一个坑:开始订单服务中配置文件是: #配置rabbitmq 2019.5.17 added by ...

  10. php HTTP协议

    HTTP协议 超文本传输协议(HTTP,HyperText Transfer Protocol) 最常见B/s架构中,使用,浏览器端与服务器端数据交互协议. 协议:约定好的一套语法规则. 规定:请求数 ...