首页 > 其他 > 详细

hdu magic balls

时间:2014-11-29 22:53:39      阅读:382      评论:0      收藏:0      [点我收藏+]

magic balls

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 74    Accepted Submission(s): 10


Problem Description
The town of W has N people. Each person takes two magic balls A and B every day. Each ball has the volume abubuko.com,布布扣ibubuko.com,布布扣bubuko.com,布布扣 and bbubuko.com,布布扣ibubuko.com,布布扣bubuko.com,布布扣 . People often stand together. The wizard will find the longest increasing subsequence in the ball A. The wizard has M energy. Each point of energy can change the two balls’ volume.(swap(abubuko.com,布布扣ibubuko.com,布布扣,bbubuko.com,布布扣ibubuko.com,布布扣)bubuko.com,布布扣 ).The wizard wants to know how to make the longest increasing subsequence and the energy is not negative in last. In order to simplify the problem, you only need to output how long the longest increasing subsequence is.
 

 

Input
The first line contains a single integer T(1T20)bubuko.com,布布扣 (the data for N>100bubuko.com,布布扣 less than 6 cases), indicating the number of test cases.
Each test case begins with two integer N(1N1000)bubuko.com,布布扣 and M(0M1000)bubuko.com,布布扣 ,indicating the number of people and the number of the wizard’s energy. Next N lines contains two integer abubuko.com,布布扣ibubuko.com,布布扣bubuko.com,布布扣 and bbubuko.com,布布扣ibubuko.com,布布扣(1abubuko.com,布布扣ibubuko.com,布布扣,bbubuko.com,布布扣ibubuko.com,布布扣10bubuko.com,布布扣9bubuko.com,布布扣)bubuko.com,布布扣 ,indicating the balls’ volume.
 

 

Output
For each case, output an integer means how long the longest increasing subsequence is.
 

 

Sample Input
2 5 3 5 1 4 2 3 1 2 4 3 1 5 4 5 1 4 2 3 1 2 4 3 1
 

 

Sample Output
4 4
 

 

Source
 
思路: 建 m 棵线段树,里面维护的是权值,
PS :BC的pst好像都好水,写的时候那么大错误还过了。。
==
bubuko.com,布布扣
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#define LL long long
#define maxn 2010
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

int a[maxn],b[maxn],id[maxn] ;
int  ql,qr;
short v ;
struct node
{
    int n,mid,hehe[maxn];
    short Max[maxn*4];
    void init(int n )
    {
       for(int i=1;i<=n*4;i++)
        Max[i]=0;
       for(int i=1;i<=n;i++)
        hehe[i]=0;
    }
    void insert(int L,int R,int o )
    {
        if(L==R)
        {
            Max[o]=v;
            hehe[L]=v;
            return ;
        }
        mid=(L+R)>>1 ;
        if(ql<=mid) insert(L,mid,o<<1) ;
        else insert(mid+1,R,o<<1|1) ;
        Max[o]=max(Max[o<<1],Max[o<<1|1]);
    }
    void find(int L,int R,int o)
    {
        if(ql<=L&&qr>=R)
        {
            v=max(Max[o],v) ;
            return ;
        }
        mid=(L+R)>>1 ;
        if(ql<=mid) find(L,mid,o<<1) ;
        if(qr>mid) find(mid+1,R,o<<1|1) ;
    }
}qe[1010] ;
int main()
{
    int i,xx,yy,ans ;
    int j,n,m,pre,sz;
    int T,tmp,val;
    //freopen("in.txt","r",stdin);
    cin >> T ;
    while(T--)
    {
        scanf("%d%d",&n,&m) ;
        sz=0;
        for( i = 1 ; i <= n ;i++){
            scanf("%d%d",&a[i],&b[i]) ;
            id[sz++]=a[i];
            id[sz++]=b[i];
        }
        sort(id,id+sz) ;
        sz=unique(id,id+sz)-id;
        ans=0;
        for( i = 0; i <= m;i++)
        {
            qe[i].init(sz);
        }
        for( i = 1 ; i <= n ;i++)
        {
            a[i]=lower_bound(id,id+sz,a[i])-id;
            b[i]=lower_bound(id,id+sz,b[i])-id;
            a[i]++;b[i]++;
            for( j = min(i,m); j >= 0 ;j--)
            {
                ql=1;
                qr=a[i]-1;
                v=0;
                if(ql<=qr) qe[j].find(1,sz,1) ;
                ql=a[i] ;
                v=v+1;
                tmp=v;
                if(qe[j].hehe[ql]<v)qe[j].insert(1,sz,1);
                if(j>0){
                    ql=1;
                    qr=b[i]-1;
                    v=0;
                    if(ql<=qr) qe[j-1].find(1,sz,1) ;
                    ql=b[i] ;
                    v=v+1;
                    tmp=max(tmp,(int)v);
                    if(qe[j].hehe[ql]<v)qe[j].insert(1,sz,1);
                }
                ans=max(ans,tmp);
            }
        }
        cout<<ans<<endl;
    }
    return 0 ;
}
View Code

 

 

hdu magic balls

原文:http://www.cnblogs.com/20120125llcai/p/4132090.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!