HDU - 3917(朴素LIS + 最大流)

Posted wtsruvf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 3917(朴素LIS + 最大流)相关的知识,希望对你有一定的参考价值。

题意:

  求出所有的最长上升子序列的个数且每个元素只能用一次

解析:

  呵。。。呵。。。呵。。呵。。emm。。。

  再见 我死了。。。wa了15发之后。。。原来不能用~  要用 != EOF

  这题算水题吧。。。emm。。。。

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d
", a);
#define plld(a) printf("%lld
", a);
#define pc(a) printf("%c
", a);
#define ps(a) printf("%s
", a);
#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 = 22222, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int s, t, n, m;
int dp[maxn], num[maxn];
int head[maxn], cur[maxn], d[maxn], cnt;

struct node
{
    int u, v, c, next;
}Node[999999];

void add_(int u, int v, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].c = c;
    Node[cnt].next = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int c)
{
    add_(u, v, c);
    add_(v, u, 0);
}

bool bfs()
{
    mem(d, 0);
    queue<int> Q;
    Q.push(s);
    d[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i = head[u]; i != -1; i = Node[i].next)
        {
            node e = Node[i];
            if(!d[e.v] && e.c > 0)
            {
                d[e.v] = d[u] + 1;
                Q.push(e.v);
                if(e.v == t) return 1;
            }
        }
    }
    return d[t] != 0;
}

int dfs(int u, int cap)
{
    int ret = 0;
    if(u == t || cap == 0)
        return cap;
    for(int &i = cur[u]; i != -1; i = Node[i].next)
    {
        node e = Node[i];
        if(d[e.v] == d[u] + 1 && e.c > 0)
        {
            int V = dfs(e.v, min(e.c, cap));
            Node[i].c -= V;
            Node[i ^ 1].c += V;
            ret += V;
            cap -= V;
            if(cap == 0) break;
        }
    }
    if(cap > 0) d[u] = -1;
    return ret;
}

int Dinic()
{
    int ans = 0;
    while(bfs())
    {
        memcpy(cur, head, sizeof(head));
        ans += dfs(s, INF);
    }
    return ans;
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        mem(head, -1);
        cnt = 0;
        s = 0, t = n * 2 + 1;
        mem(dp, 0);
        int max_len = -INF;
        for(int i = 1; i <= n; i++)
        {
            rd(num[i]);
            dp[i] = 1;
            for(int j = 1; j < i; j++)
                if(num[j] < num[i] && dp[j] + 1 >= dp[i]) dp[i] = dp[j] + 1;
            max_len = max(max_len, dp[i]);
        }

        for(int i = 1; i <= n; i++)
        {
            add(i, i + n, 1);
            if(dp[i] == 1) add(s, i, 1);
            if(dp[i] == max_len) add(i + n, t, 1);
            for(int j = i + 1; j <= n; j++)
            {
                if(dp[i] + 1 == dp[j])
                    add(i + n, j, 1);
            }
        }
        cout << max_len << endl;
        cout << Dinic() << endl;
    }

    return 0;
}

 

以上是关于HDU - 3917(朴素LIS + 最大流)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 3917 Road constructions(最小割---最大权闭合)

[HDOJ3998] Sequence(DP,最大流)

HDU 1087 Super Jumping! Jumping! Jumping! (DP+LIS)

hdu 6197 array array array LIS

HDU 5489 Removed Interval (LIS变形)

HDU 3564 Another LIS splay(水