Now or later UVALive

emmm。。。去吃早饭了。。。

rujia讲的很好。。

最小值最大化问题,,,二分枚举答案   设x1、x2为同一个集合中的元素,y1、y2为另一个集合中的元素,如果x1与y1之差小于mid,那么如果选了x1就必须选y2,反过来,选了y1就必须选x2。这样就是2-SAT模型了。只需找出使得这个2-SAT有解的最大mid即可。

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#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<int, int>
#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<int> 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<n; i++) for(int a=0; a<2; a++)
    for(int j=i+1; j<n; j++) for(int b=0; b<2; b++)
        if(abs(T[i][a] - T[j][b] )< diff) solver.add_clause(i, a^1, j, b^1);
    return solver.solve();
}

int main()
{
    while(~scanf("%d", &n) && n)
    {
        int L = 0, R = 0;
        for(int i=0; i<n; i++)
            for(int a=0; a<2; a++)
            {
                scanf("%d", &T[i][a]);
                R = max(R, T[i][a]);
            }
        while(L <= R)
        {
            int mid = L + (R-L)/2;
            if(test(mid)) L = mid + 1;
            else R = mid - 1;
        }
        printf("%d
", R);

    }

    return 0;
}
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
原文地址:https://www.cnblogs.com/WTSRUVF/p/9364865.html