HDU 5406 CRB and Apple

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

题意:略

解法:

dp[i][j]ij.

dp[i][j]=max(max(dp[i][k]),max(dp[k][j]))+1

dp[i]

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n;
struct node{
    int h, d;
    void read()
    {
        scanf("%d%d",&h,&d);
    }
}a[1010];
bool cmp(node x, node y)
{
    if(x.h!=y.h) return x.h>y.h;
    return x.d<y.d;
}
int dp[1010][1010];
int temp[1010];
//dp[i][j]表示上一次男女吃的deliciousness分别为i,j的时候吃的最多的苹果
//dp[i][j] = max(dp[i][k]+1), 0<k<=j
//dp[i][j] = max(dp[k][j]+1), 0<k<=i
namespace BIT{
    int tree[maxn][maxn];
    void init(){
        memset(tree, 0, sizeof(tree));
    }
    inline int lowbit(int x)
    {
        return x&-x;
    }
    void update(int id, int x, int v)
    {
        while(x<maxn){
            tree[id][x]=max(tree[id][x], v);
            x+=lowbit(x);
        }
    }
    int query(int id, int x){
        int ret = 0;
        while(x>0){
            ret = max(ret, tree[id][x]);
            x -= lowbit(x);
        }
        return ret;
    }
}
vector <int> v;
using namespace BIT;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        v.clear();
        init();
        scanf("%d", &n);
        for(int i=1; i<=n; i++){
            a[i].read();
            v.push_back(a[i].d);
        }
        sort(a+1,a+n+1,cmp);
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(),v.end()),v.end());
        for(int i=1; i<=n; i++) a[i].d = lower_bound(v.begin(), v.end(), a[i].d)-v.begin()+1;
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                temp[j] = query(a[i].d, j)+1;
                temp[j] = max(temp[j], query(j, a[i].d)+1);
            }
            for(int j=1; j<=n; j++){
                update(j, a[i].d, temp[j]);
                update(a[i].d, j, temp[j]);
            }
        }
        int ans = 0;
        for(int i=1; i<=n; i++){
            ans = max(ans, query(i, v.size()));
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值