【codeforces 777E】Hanoi Factory

【题目链接】:http://codeforces.com/problemset/problem/777/E

【题意】

让你摆汉诺塔片;
要求在上面的片的外圈大于在下面的片的内圈,且小于下面的片的外圈;
给你n个片;
每个片有属性->外圈半径,内圈半径,高度;
然后让你选择其中的一些片;
使得它们叠起来高度最高;

【题解】

对于外圈半径相同的。
我们知道它们肯定是能够叠在一起的;
且我们可以把这些外圈相同的泛化成一个片;
这个片的外圈半径和原来的一样,内圈半径是这些外圈半径相同的片的内圈半径中最小的那个->即把内圈半径最小的那个放在最上面;->这样就能够最大限度地让上面的片能够多放一点了,且显然是正确的。
然后这个泛化出来的片的高度就是所有这个外圈半径的片的高度的和;
泛化完毕以后;
我们就得到了n个新的,外圈半径全都不同的片;
接下来我们把这些片按照外圈半径从大到小排序;
然后就能做一个类似最长不下降子序列的DP了
即设f[i]为以第i个片作为最上面那个片的最大高度;
f[i]=max(f[j])+h[i],这里j< i且j的内圈半径< r的外圈半径;
这里我们可以写个线段树来优化;
即在边做DP的时候,边维护内圈半径为r[i]的片的f[i]值;
这里的线段树,线段表示的就是内圈半径;值就是f[i]的值;
所以在查的时候query(1..i的外圈半径-1,1,n,1);
更新的时候同理;
写个离散化就行啦

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5+100;

struct abc
{
    LL a, b, h;
};

int n,totn;
abc a[N],b[N];
LL c[N], ma[N << 2],f[N],ans;

bool cmp1(abc a, abc b)
{
    return a.b > b.b;
}

void up_data(int pos, LL val, int l, int r, int rt)
{
    if (l == r)
    {
        ma[rt] = max(ma[rt], val);
        return;
    }
    int m = (l + r) >> 1;
    if (pos <= m)
        up_data(pos, val, lson);
    else
        up_data(pos, val, rson);
    ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]);
}

LL query(int L, int R, int l, int r, int rt)
{
    if (L > R) return 0;
    if (L <= l && r <= R)
        return ma[rt];
    int m = (l + r) >> 1;
    LL temp1 = 0, temp2 = 0;
    if (L <= m)
        temp1 = query(L, R, lson);
    if (R > m)
        temp2 = query(L, R, rson);
    return max(temp1, temp2);
}

int main()
{
    //freopen("F:\rush.txt", "r", stdin);
    rei(n);
    rep1(i, 1, n)
        rel(a[i].a), rel(a[i].b), rel(a[i].h);
    sort(a + 1, a + 1 + n, cmp1);
    rep1(i, 1, n){
        int j = i;
        LL mi = a[i].a,height = a[i].h;
        while (j + 1 <= n && a[j + 1].b == a[i].b) j++,mi = min(mi,a[j].a),height+=a[j].h;
        b[++totn].b = a[i].b, b[totn].a = mi, b[totn].h = height;
        c[totn] = mi;
        i = j;
    }
    sort(c + 1, c + 1 + totn);
    n = totn;
    f[1] = b[1].h;
    up_data(lower_bound(c + 1, c + 1 + n, b[1].a) - c,f[1], 1, n, 1);
    rep1(i, 2, n)
    {
        int idx = upper_bound(c + 1, c + 1 + n, b[i].b - 1) - c -1;
        LL bef = query(1, upper_bound(c + 1, c + 1 + n, b[i].b - 1) - c-1, 1, n, 1);
        f[i] = max(f[i], bef + b[i].h);
        idx = lower_bound(c + 1, c + 1 + n, b[i].a) - c;
        up_data(lower_bound(c + 1, c + 1 + n, b[i].a) - c, f[i], 1, n, 1);
    }
    ans = f[1];
    rep1(i, 2, n)
        ans = max(ans, f[i]);
    printf("%lld
", ans);
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626487.html