2015 Multi-University Training Contest 10 (hdu5406 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

题意:一棵树,两个人,树上有若干个果子,果子有甜度,两个人从上到下吃,要求每个人吃到的果子顺序甜度都是不降的,问两个人吃到果子个数总数最多是多少。

思路:其实可以看出来就是求两个LIS,要求总值最大。用树状数组+dp解决

先离散化甜度,然后,设dp[i][j]为第一个人在i点甜度,第二个人在j点时的甜度最大值。

dp[i][k] = dp[k][i] = max(dp[i][j] | j < k)。

代码:

struct p
{
    int h, d;
    p(){}
    p(int h, int d):h(h), d(d){}
    bool operator <(const p& x) const{
        return h != x.h? h > x.h : d < x.d;
    }
};
p pp[1005];
int s[2005];
int dp[1005][1005], tmp[1005];

void add(int* a, int x, int d)
{
    for(int i = x; i < 1005; i += i&-i)
        a[i] = max(a[i], d);
}
int sum(int* a, int x){
    int ret = 0;
    for(int i = x; i; i -= i&-i)
        ret = max(ret, a[i]);
    return ret;
}

int main()
{
    int t, n; scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &pp[i].h, &pp[i].d);
            s[i] = pp[i].d;
        }
        sort(pp, pp+n);
        sort(s, s+n);
        for(int i = 0; i < n; i++)
            pp[i].d = lower_bound(s, s+n, pp[i].d)-s+1;

        memset(dp, 0, sizeof(dp));
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            int v = pp[i].d;
            for(int j = 1; j <= n; j++) 
            {
                tmp[j] = sum(dp[j], v)+1;
                ans = max(ans, tmp[j]);
            }
            for(int j = 1; j <= n; j++)
            {
                add(dp[j], v, tmp[j]);
                add(dp[v], j, tmp[j]);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值