zoukankan      html  css  js  c++  java
  • hdu magic balls

    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 ai and bi . 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(ai,bi) ).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) (the data for N>100 less than 6 cases), indicating the number of test cases.
    Each test case begins with two integer N(1N1000) and M(0M1000) ,indicating the number of people and the number of the wizard’s energy. Next N lines contains two integer ai and bi(1ai,bi109) ,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好像都好水,写的时候那么大错误还过了。。
    ==
    #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
     
  • 相关阅读:
    Python之路(第十七篇)logging模块
    Python之路(第十五篇)sys模块、json模块、pickle模块、shelve模块
    Python之路(第十四篇)os模块
    Python之路(第十三篇)time模块、random模块、string模块、验证码练习
    Python之路(第十二篇)程序解耦、模块介绍导入安装、包
    Python编程笔记(第三篇)【补充】三元运算、文件处理、检测文件编码、递归、斐波那契数列、名称空间、作用域、生成器
    Python之路(第十一篇)装饰器
    Python之路(第十篇)迭代器协议、for循环机制、三元运算、列表解析式、生成器
    Python之路(第九篇)Python文件操作
    Python编程笔记(第二篇)二进制、字符编码、数据类型
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/4132090.html
Copyright © 2011-2022 走看看