zoukankan      html  css  js  c++  java
  • 最长k可重线段集问题

    和那道可重区间集一样
    不过这道题可能有垂直于x轴的线段,这就很烦了,直接连会有负环,判掉又会WA
    可以想办法把r端点和l端点分开,又要保证答案不变
    那么直接把区间l,r都乘以2,l=r时r++,否则l++,这样r就与l分开,并且对其它没有影响(相当于在x轴上多加了点)


    这道题在LOJ上可以切


    如果看到了的并且有数据可以卡掉我的代码的请在下面评论

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    # define Copy(a, b) memcpy(a, b, sizeof(a))
    # define Sqr(a) (1LL * (a) * (a))
    using namespace std;
    typedef long long ll;
    # define int ll
    const int _(1010), __(1e6 + 10), INF(2e9);
    
    IL ll Read(){
        char c = '%'; ll x = 0, z = 1;
        for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
        return x * z;
    }
    
    int n, k, l[_], y_1[_], r[_], y_2[_], _w[_], o[_], len;
    int cnt, fst[_], w[__], to[__], nxt[__], dis[_], vis[_], S, T, cost[__], pe[_], pv[_], max_flow, max_cost;
    queue <int> Q;
    
    IL void Add(RG int u, RG int v, RG int f, RG int co){
        cost[cnt] = co; w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
        cost[cnt] = -co; w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
    }
    
    IL bool Bfs(){
        Q.push(S); Fill(dis, 127); dis[S] = 0; vis[S] = 1;
        while(!Q.empty()){
            RG int u = Q.front(); Q.pop();
            for(RG int e = fst[u]; e != -1; e = nxt[e]){
                if(!w[e] || dis[to[e]] <= dis[u] + cost[e]) continue;
                dis[to[e]] = dis[u] + cost[e];
                pe[to[e]] = e; pv[to[e]] = u;
                if(!vis[to[e]]) vis[to[e]] = 1, Q.push(to[e]);
            }
            vis[u] = 0;
        }
        if(dis[T] >= dis[T + 1]) return 0;
        RG int ret = INF;
        for(RG int u = T; u != S; u = pv[u]) ret = min(ret, w[pe[u]]);
        for(RG int u = T; u != S; u = pv[u]) w[pe[u]] -= ret, w[pe[u] ^ 1] += ret;
        max_cost -= ret * dis[T]; max_flow += ret;
        return 1;
    }
    # undef int
    int main(RG int argc, RG char *argv[]){
    # define int ll
        Fill(fst, -1); n = Read(); k = Read();
        for(RG int i = 1; i <= n; ++i){
            l[i] = Read(); y_1[i] = Read(); r[i] = Read(); y_2[i] = Read();
            _w[i] = sqrt(Sqr(l[i] - r[i]) + Sqr(y_1[i] - y_2[i]));
            if(l[i] > r[i]) swap(l[i], r[i]), swap(y_1[i], y_2[i]);
            l[i] <<= 1; r[i] <<= 1;
            if(l[i] == r[i]) ++r[i];
            else ++l[i];
        }
        for(RG int i = 1; i <= n; ++i) o[++len] = l[i], o[++len] = r[i];
        sort(o + 1, o + len + 1); len = unique(o + 1, o + len + 1) - o - 1;
        T = len + 1;
        for(RG int i = 0; i <= len; ++i) Add(i, i + 1, k, 0);
        for(RG int i = 1; i <= n; ++i){
            l[i] = lower_bound(o + 1, o + len + 1, l[i]) - o;
            r[i] = lower_bound(o + 1, o + len + 1, r[i]) - o;
            Add(l[i], r[i], 1, -_w[i]);
        }
        while(Bfs()); printf("%lld
    ", max_cost);
        return 0;
    }
    
  • 相关阅读:
    ioi1998 Polygon
    [Noip模拟题]Seq
    [noip模拟]分组行动
    入门OJ:photo
    Sgu167 I-country
    入门OJ:简单的网络游戏
    入门OJ:Coin
    ATT&CK实战系列
    Metasploit Framework(二)
    RoarCTF 2019
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206318.html
Copyright © 2011-2022 走看看