Gym 102055B Balance of the Force

Posted uid001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym 102055B Balance of the Force相关的知识,希望对你有一定的参考价值。

大意: $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 ‘
‘
#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
", ans);
}

int main() {
	int t=rd();
	REP(i,1,t) {
		printf("Case %d: ",i);
		work();
	}
}

 

以上是关于Gym 102055B Balance of the Force的主要内容,如果未能解决你的问题,请参考以下文章

The BASE principle of balance sheet

The BASE principle of balance sheet

The BASE principle of balance sheet

Gym 101666K King of the Waves(dfs)

CF gym 10326040th Petrozavodsk Programming Camp, Day 5,2021.2.3 水题2题

Gym - 101611D Decoding of Varints(边界值处理)