你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Java专栏
poj 1631
 

这个题目是 给出一些连线关系,要你删除一些连线使得剩下的连线最多并且 不相交。。

看到后面知道其实这个题目就是求最长上升子序列。。可以用dp做,不过要dp+二分,

我用的是树状数组来做,c来存前i个中的最长上升子序列,dat[i]=getmax(dat[i]-1)+1。。

[cpp] 
//============================================================================ 
// Name        : hello.cpp 
// Author      : lxw 
// Version     : 
// Copyright   : Your copyright notice 
// Description : Hello World in C++, Ansi-style 
//============================================================================ 
 
#include <iostream> 
using namespace std; 
 
#define MAXN 40010 
 
int dat[MAXN],c[MAXN];//c[i]表示前i个中的最长上升子序列 
int n; 
 
int lowbit(int x){return x&(-x);} 
 
void update(int i){ 
    int m=c[i]; 
    while(i<=n){ 
        if(c[i]<m)c[i]=m; 
        i+=lowbit(i); 
    } 

 
int getMax(int i){ 
    int Max=0; 
    while(i>0){ 
        if(c[i]>Max)Max=c[i]; 
        i-=lowbit(i); 
    } 
    return Max; www.2cto.com

 
int main() { 
    //setbuf(stdout,NULL); 
    int t; 
    scanf("%d",&t); 
    while(t--){ 
        scanf("%d",&n); 
        for(int i=1;i<=n;i++)scanf("%d",&dat[i]); 
        memset(c,0,sizeof(c)); 
        int maxnum=0; 
        for(int i=1;i<=n;i++){ 
            int tmp=c[dat[i]]=getMax(dat[i]-1)+1; 
            if(tmp>maxnum)maxnum=tmp; 
            update(dat[i]); 
        } 
        printf("%d\n",maxnum); 
    } 
    return 0; 


  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089