玲珑学院OJ 1097 萌萌哒的第二题【dp+线段树】

1097 - 萌萌哒的第二题

Time Limit:5s Memory Limit:128MByte

Submissions:641Solved:172

DESCRipTION

一条东西走向的河两边有都排着工厂,北边有n间工厂A提供原材料,南边有n间工厂B进行生产。现在需要在工厂A和工厂B之间建运输桥以减少运输成本。可是每个工厂B只能接受最多6个工厂A提供的材料能满足生产,而且建立的运输桥之间不能有交叉,北边的工厂A由西向东编号1~n,南边的工厂B也是一样,不能交叉的意思是如果a号工厂A跟b号工厂B之间建立了运输桥,那么不能存在c、d(c < a 且d > b) 使得c号工厂A和d号工厂b之间建立运输桥,每个工厂A只能给一个工厂B提供材料,每个工厂B只能由一间工厂A提供材料,请问在满足上述条件的情况下最多能建立多少个运输桥。 (每个工厂最多有6个选择,但只能选一个)

INPUT 包含多组测试数据(<=15),其中每组测试数据: 第一行一个整数n(1<= n <= 10^5) 接下来n行,第i+1行6个整数表示i号工厂B能接受的6个工厂A的编号,保证所有编号的范围是1~n,可能重复(请看样例)。 OUTPUT 每组数据输出一行一个整数表示最多能建立的运输桥数量。 SAMPLE INPUT 3 1 2 3 1 2 3 2 2 2 2 2 2 1 3 1 3 1 3 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 1 1 1 1 1 SAMPLE OUTPUT 3 5

思路:

其实问题就是让你求一个每个位子可以从六个数中取一个数组成的序列中的最长递增子序列。

那么我们设定dp【i】表示最后一个工厂B,匹配到的最远的A工厂编号为i的最长递增子序列长度。

那么有:dp【i】=max(dp【j】)【j<i】+1

这里直接用线段树维护一下即可,那么时间复杂度O(nlong);

Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1

int tree[1000050*5];
int dp[108050];
int a[100050][10];
void pushup(int rt)
{
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    else
    {
        int m=(l+r)>>1;
        int ans=0;
        if(L<=m)
        {
            ans=max(Query(L,R,lson),ans);
        }
        if(m<R)
        {
            ans=max(Query(L,R,rson),ans);
        }
        return ans;
    }
}
void build( int l ,int r , int rt )
{
    if( l == r )
    {
        tree[rt]=0;
        return ;
    }
    else
    {
        int m = (l+r)>>1 ;
        build(lson) ;
        build(rson) ;
        pushup(rt) ;
    }
}
void update(int p,int c,int l,int r,int rt)//p阵营c数据.
{
    if(l==r)
    {
        tree[rt]=max(tree[rt],c);
    }
    else
    {
        int m=(l+r)>>1;
        if(p<=m) update(p,c,lson);
        else  update(p,c,rson);
        pushup(rt);
    }
}
inline void read(int &t) {
    int f = 1;char c;
    while (c = getchar(), c < '0' || c > '9') if (c == '-') f = -1;
    t = c   -'0';
    while (c = getchar(), c >= '0' && c <= '9') t = t * 10 + c  - '0';
    t *= f;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(dp,0,sizeof(dp));
        build(1,n,1);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=6;j++)
            {
                read(a[i][j]);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=6;j++)
            {
                if(a[i][j]==1)
                dp[1]=1;
                else
                {
                    int u=a[i][j];
                    int maxnval=Query(1,u-1,1,n,1);
                    dp[u]=max(dp[u],maxnval+1);
                }
            }
            for(int j=1;j<=6;j++)
            {
                update(a[i][j],dp[a[i][j]],1,n,1);
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)ans=max(ans,dp[i]);
        PRintf("%d\n",ans);
    }
}