Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)

E. Hanoi Factory
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.

There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:

  • Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi
  • Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai
  • The total height of all rings used should be maximum possible. 
Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock.

The i-th of the next n lines contains three integers aibi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.

Output

Print one integer — the maximum height of the tower that can be obtained.

Examples
input
3
1 5 1
2 6 2
3 7 3
output
6
input
4
1 2 1
1 3 3
4 6 2
5 7 1
output
4
Note

In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.

In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.

因a和b数组中的数较大,因此需要离散化。可以用类似于LIS的方法进行dp转移,但因为题目要求时间复杂度为O(nlogn),所以还要用树状数组或线段树进行优化,维护1到某个半径的最大高度。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
struct ss
{
    long long a,b,c;
};
ss a[3000001];
long long n,len,tree[3000001],m=0;
struct Hash : vector<int> {    //离散化
    void prepare() {
        sort(begin(), end());
        //erase(unique(begin(), end()), end());
    }
    long long get(long long x) {
        return lower_bound(begin(), end(), x)-begin()+1;
    }
} has;
void upd(long long x,long long y)
{
    for (;x<=m;x+=x&(-x)) tree[x]=max(tree[x],y);
}
long long sum(long long x)
{
    long long p=0;
    for (;x;x-=x&(-x)) p=max(p,tree[x]);
    return p;
}
inline bool cmp(ss a,ss b)
{
    return (a.b>b.b||a.b==b.b&&a.a>b.a);
}
int main()
{
    scanf("%lld",&n);
    long long i;
    for (i=1;i<=n;i++)
        scanf("%lld%lld%lld",&a[i].a,&a[i].b,&a[i].c);
    has.clear();
    for (i=1;i<=n;i++)
        has.push_back(a[i].a),has.push_back(a[i].b);
    has.prepare();
    for (i=1;i<=n;i++)
        a[i].a=has.get(a[i].a),a[i].b=has.get(a[i].b),m=max(a[i].b,m);
    m*=2;
    sort(a+1,a+n+1,cmp);
    //for (i=1;i<=n;i++)
    //    printf("%d %d %d\n",a[i].a,a[i].b,a[i].c);
    //cout<<m<<endl;
    memset(tree,0,sizeof(tree));
    long long ans=0;
    for (i=1;i<=n;i++)
    {
        long long now=sum(a[i].b-1)+a[i].c;
        //cout<<sum(a[i].b-1)<<endl;
        upd(a[i].a,now);
        ans=max(ans,now);
    }
    printf("%lld\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/hnqw1214/p/6476103.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权。最终,ans\[u\]代表包含节点u在内的子连通块的最大权。\[1\] 问题: CodeForces - 982C DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值