DP Codeforces Round 401#div2 E.Hanoi Factory

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sumword_/article/details/57082993
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 ≤ 109bi > 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.

思路:ring.total表示将这个放在最下面时塔的最大长度。先把ring的b从小到大排序,b相同时,将a从小到大排序,然后for循环,使用栈,如果top能满足就记录最大total值,然后pop掉,否则就将此时的ring[i].total = h+maxx,然后push进去。最后找出total最大的。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;
class K{
public:
    int a;
    int b;
    int h;
    long long total;
    friend bool operator < (const K a, const K b){
        if(a.b == b.b)
            return a.a < b.a;
        else
            return a.b < b.b;
    }
}ring[100500];
int main(){
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n; i++){
        scanf("%d%d%d",&ring[i].a,&ring[i].b,&ring[i].h);
    }
    sort(ring,ring+n);
    stack<K>my;
    long long result = 0;
    for(int i = 0; i < n; i++){
        long long maxx = 0;
        while(1){
            if(my.empty())break;
            if(my.top().b > ring[i].a){
                maxx = max(my.top().total,maxx);
                my.pop();
            }
            else
                break;
        }
        ring[i].total = ring[i].h+maxx;
        my.push(ring[i]);
        result = max(result,ring[i].total);
    }
    cout << result <<endl;

}

猜你喜欢

转载自blog.csdn.net/sumword_/article/details/57082993