本文共 1464 字,大约阅读时间需要 4 分钟。
emmm。。。去吃早饭了。。。
rujia讲的很好。。
最小值最大化问题,,,二分枚举答案 设x1、x2为同一个集合中的元素,y1、y2为另一个集合中的元素,如果x1与y1之差小于mid,那么如果选了x1就必须选y2,反过来,选了y1就必须选x2。这样就是2-SAT模型了。只需找出使得这个2-SAT有解的最大mid即可。
#include #include #include #include #include #include #include #include #include #include #include #define rap(a, n) for(int i=a; i<=n; i++)#define MOD 2018#define LL long long#define ULL unsigned long long#define Pair pair #define mem(a, b) memset(a, b, sizeof(a))#define _ ios_base::sync_with_stdio(0),cin.tie(0)//freopen("1.txt", "r", stdin);using namespace std;const int maxn = 10010, INF = 0x7fffffff;int n, T[maxn][2];struct TwoSAT{ int n; vector G[maxn*2]; bool mark[maxn*2]; int S[maxn*2], c; bool dfs(int x) { if(mark[x^1]) return false; if(mark[x]) return true; mark[x] = true; S[c++] = x; for(int i = 0; i < G[x].size(); i++) if(!dfs(G[x][i])) return false; return true; } void init(int n) { this->n = n; for(int i=0; i < n*2; i++) G[i].clear(); mem(mark, 0); } void add_clause(int x, int xval, int y, int yval) { x = x * 2 + xval; y = y * 2 + yval; G[x^1].push_back(y); G[y^1].push_back(x); } bool solve() { for(int i=0; i < n*2; i+=2) { if(!mark[i] && !mark[i+1]) { c = 0; if(!dfs(i)) { while(c > 0) mark[S[--c]] = false; if(!dfs(i+1)) return false; } } } return true; }};TwoSAT solver;bool test(int diff){ solver.init(n); for(int i=0; i
转载于:https://www.cnblogs.com/WTSRUVF/p/9364865.html