hdu 5406 2015 多校联合训练赛#10 dp

CRB and Apple

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 358    Accepted Submission(s): 109


Problem Description
In Codeland there are many apple trees.
One day CRB and his girlfriend decided to eat all apples of one tree.
Each apple on the tree has height and deliciousness.
They decided to gather all apples from top to bottom, so an apple can be gathered only when it has equal or less height than one just gathered before.
When an apple is gathered, they do one of the following actions.
1.  CRB eats the apple.
2.  His girlfriend eats the apple.
3.  Throw the apple away.
CRB(or his girlfriend) can eat the apple only when it has equal or greater deliciousness than one he(she) just ate before.
CRB wants to know the maximum total number of apples they can eat.
Can you help him?
 

Input
There are multiple test cases. The first line of input contains an integer  T , indicating the number of test cases. For each test case:
The first line contains a single integer  N  denoting the number of apples in a tree.
Then  N  lines follow,  i -th of them contains two integers  Hi  and  Di  indicating the height and deliciousness of  i -th apple.
1 ≤  T  ≤ 48
1 ≤  N  ≤ 1000
1 ≤  Hi Di  ≤  109

 

Output
For each test case, output the maximum total number of apples they can eat.
 

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

Sample Output
  
  
4
 

Author
KUT(DPRK)
 

Source

先对h排序,然后就只找两条不相交的最长不降子序列。

对d进行离散化。用dp[i][j]表示取到当前位置,两个子序列的最后一个苹果的可口值是i和j的情况下,

最多能吃的苹果个数。

对于当前的d,枚举i进行转移公式:dp[i][d] = dp[d][i] = dp[i][j] + 1 (j <= d)

根据不降子序列的性质有单调性,只需dp[i][d] = dp[d][i] = max(dp[i][j]), j<=d

用树状数组维护最大值即可



#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
#define maxn 1001
int tree[maxn][maxn];
int m;
void add(int *T,int p,int n){
    for(;p<=m;p+=p&-p)
        T[p] = max(T[p],n);
}

int query(int *T,int p){
    int ans = 0;
    for(;p>0;p-=p&-p)
        ans = max(ans,T[p]);
    return ans;
}


struct Node{
    int h,d;
};
Node p[1001];
int comp(Node a,Node b){
    if(a.h == b.h) return a.d > b.d;
    return a.h < b.h;
}
int haha[maxn];
int main(){
    int t,n,h,d;
    //freopen("1001.in","r",stdin);
    //freopen("1001x.out","w",stdout);
    scanf("%d",&t);
    while(t--)
        memset(tree,0,sizeof(tree));
        scanf("%d",&n);
        for(int i = 0 ;i < n; i++){
            scanf("%d%d",&p[i].h,&p[i].d);
            haha[i] = p[i].d;
        }
        sort(haha,haha+n);
        m = unique(haha,haha+n)-haha;
        sort(p,p+n,comp);
        for(int i = 0;i < n; i++)
            p[i].d = m-(lower_bound(haha,haha+m,p[i].d)-haha);
        int d,u,v;
        for(int i = 0;i < n; i++){
            memset(haha,0,sizeof(haha));
            d = p[i].d;
            for(int j = 1;j <= m; j++)
                haha[j] = query(tree[j],d)+1;
            for(int j = 1;j <= m; j++){
                add(tree[j],d,haha[j]);
                add(tree[d],j,haha[j]);
            }
        }
        int ans = 0;
        for(int i = 1;i <= m; i++)
            ans = max(ans,query(tree[i],m));
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值