UVALive 3211 Now or later 二分+Twosat

本文介绍了一种使用二分查找结合二部图的双向闭包算法解决飞机起降时间优化问题的方法。通过定义飞机早、晚起降的时间点,并利用二分查找找到最大可能的间隔时间,最终实现所有飞机起降时间的最优安排。

题目链接:https://vjudge.net/problem/UVALive-3211

题意:每架飞机有早晚起降两种方式,给定n架飞机两种方式的起落时间,为每架飞机安排起落时间(早或

晚),使得所有飞机起降时间按照早到晚的顺序之间的间隔时间最小值尽量大。

解法:最小时间尽量大应该采用二分的方法比较好,然后就变成了判别某个时间间隔m是不是符合要求的了。

为没加飞机设立一个变量xi,0表示早,1表示晚,然后每架飞机i用两个点2*i,2*i+1表示,前者如果被标记表

示早,后者被标记表示晚降。 然后对不同的飞机的起降时间中间隔小于m的i,j建立约束条件,例如:i架飞机

早降间隔和j架飞机的早降时间间隔小于m,那么约束条件就可以在图中这样表示:建立两条边2*i—>2*j+1,

2*j->2*i+1,表示如果i早降则j必须晚降,j早降i必须晚降。

///UVALIVE 3211

#include <bits/stdc++.h>
using namespace std;
const int maxn = 4000010;
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]=1;
        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();
        memset(mark, 0, sizeof(mark));
    }
    void addedge(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;

int n, T[maxn][2];
bool check(int mid){
    twosat.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])<mid){
                        twosat.addedge(i,a^1,j,b^1);
                    }
                }
            }
        }
    }
    return twosat.solve();
}

int main(){
    while(scanf("%d", &n)!=EOF&&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]);
            }
        }
        int ans = 0;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(mid)) ans=mid,l=mid+1;
            else r=mid-1;
        }
        printf("%d\n", ans);
    }
    return 0;
}
#include <bits/stdc++.h> // #include "atcoder/convolution" // #include "atcoder/dsu" // #include "atcoder/fenwicktree" // #include "atcoder/lazysegtree" // #include "atcoder/math" // #include "atcoder/maxflow" // #include "atcoder/mincostflow" // #include "atcoder/modint" // #include "atcoder/scc" // #include "atcoder/segtree" // #include "atcoder/string" // #include "atcoder/twosat" // using namespace __gnu_cxx; // using namespace __gnu_pbds; using namespace std; auto rng = mt19937(random_device()()); auto rngl = mt19937_64(random_device()()); // Let's set a bit and flow! // I came, I divided, I conquered! int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t --) { int n, q; cin >> n >> q; vector<long long> nums(n); for (auto &x: nums) cin >> x; vector<long long> vis = nums, cur = nums; vector<pair<int, int>> updates(q); for (auto &[idx, val]: updates) { cin >> idx >> val; idx --; cur[idx] += val; vis.emplace_back(cur[idx]); } sort(vis.begin(), vis.end()); vis.erase(unique(vis.begin(), vis.end()), vis.end()); int k = vis.size(); vector<int> fen(k + 1, 0); auto getidx = [&] (long long val) -> int { return lower_bound(vis.begin(), vis.end(), val) - vis.begin(); }; auto add = [&] (int idx, int val) -> void { idx ++; while (idx <= k) { fen[idx] += val; idx += idx & -idx; } }; auto not_exceed = [&] (int val) -> int { int res = 0, idx = 0; for (int i = 20; i >= 0; i --) { if ((idx | (1 << i)) <= k && res + fen[idx | (1 << i)] <= val) { idx |= 1 << i; res += fen[idx]; } } return res; }; for (
最新发布
07-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值