【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\n", ans);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 777E】Hanoi Factory的更多相关文章
- Codeforces 777E:Hanoi Factory(贪心)
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a specia ...
- Codeforces 777E:Hanoi Factory(贪心+栈)
http://codeforces.com/problemset/problem/777/E 题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且b ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- 【NOJ2024】入栈序列和出栈序列
入栈序列和出栈序列 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 总提交:293 测试通过:68 比赛描述 给出入栈序列 ...
- 100个常用的原生JavaScript函数
1.原生JavaScript实现字符串长度截取 复制代码代码如下: function cutstr(str, len) { var temp; var icount = 0; var ...
- LUOGU P1081 开车旅行 (noip 2012)
传送门 解题思路 这道题刚了一下午,主要就刚在set那里了.先写了一个暴力70分..之后优化预处理,看着大佬神犇们都用的什么双向链表之类的东西,本蒟蒻不会,又懒得手写平衡树,就拿了个set搞了搞,感觉 ...
- Thinkphp 调试方法
1.入口文件index.php配置APP_DEBUG,能直接发现页面上的错误 define('APP_DEBUG',True); 2.配置页面调试SHOW_PAGE_TRACE,可以在config里面 ...
- 杨柳絮-Info:春天将不再漫天飞“雪”,济源治理杨柳絮在行动
ylbtech-杨柳絮-Info:春天将不再漫天飞“雪”,济源治理杨柳絮在行动 1.返回顶部 1. 天气暖和了,连心情都是阳光的.然而,在这美好的时刻,漫天飞舞的杨柳絮,甚是煞风景.<ignor ...
- CEF 框架使用集锦
CEF 框架使用集锦: 参考:〓https://github.com/NetDimension/NanUI/wiki/%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8NanUI ...
- ajax下载小于500M大文件【原】
不推荐使用的FileReader 之前用FileReader读取下载文件,当文件超过1M浏览器就立即扑街了 // 文件下载 function download(blob, fileName) { va ...
- 当inline-block和text-indent遇到IE6,IE7
在实际应用中,考虑到seo,很多button,icon都要用到inline-block和text-indent来处理,例如: <a href="#" class=" ...
- golang变量-数据类型一
package main import "fmt" var t1 = 100 var t2 = 200 var t3 = 300 var ( u1 = 100 u2 = 200 u ...
- Asterisk 代码架构概述
from:http://blog.csdn.net/yetyongjin/article/details/7594447 近日分析Asterisk 1.8源码.Asterisk trunk上有这篇架构 ...