Codeforces 777E:Hanoi Factory(贪心+栈)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 777E:Hanoi Factory(贪心+栈)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://codeforces.com/problemset/problem/777/E
題意:給出n個環狀圓柱,每個圓環有一個內半徑a,外半徑b,和高度h,只有外半徑bj <= bi并且bj > ai,這樣j才可以放在i的上面,問最大能達到的高度是多少。
思路:一開始用數組dp錯了,主要是推錯轉移方程。用不到之前的信息了。如果要利用之前的信息,其實是可以用棧來維護的。先按照外半徑從大到小,外半徑相同內半徑從大到小排序,這樣能保證如果前面符合,后面放上去能使高度最大。
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define N 100010 4 typedef long long LL; 5 struct node { 6 LL a, b, h; 7 } p[N]; 8 stack<node> sta; 9 10 bool cmp(const node &a, const node &b) { 11 if(a.b == b.b) return a.a < b.a; 12 return a.b > b.b; 13 } 14 15 int main() { 16 int n; 17 scanf("%d", &n); 18 for(int i = 1; i <= n; i++) scanf("%lld%lld%lld", &p[i].a, &p[i].b, &p[i].h); 19 sort(p + 1, p + 1 + n, cmp); 20 LL ans = p[1].h, cnt = p[1].h; 21 sta.push(p[1]); 22 for(int i = 2; i <= n; i++) { 23 if(sta.empty()) { 24 cnt += p[i].h; sta.push(p[i]); 25 continue; 26 } 27 node top = sta.top(); 28 if(top.a < p[i].b) { 29 cnt += p[i].h; 30 sta.push(p[i]); 31 } else { 32 ans = max(ans, cnt); 33 while(top.a >= p[i].b) { 34 cnt -= top.h; 35 sta.pop(); 36 if(sta.empty()) break; 37 top = sta.top(); 38 } 39 sta.push(p[i]); 40 cnt += p[i].h; 41 } 42 ans = max(ans, cnt); 43 } 44 printf("%lld\n", ans); 45 return 0; 46 }?
轉載于:https://www.cnblogs.com/fightfordream/p/6490157.html
總結
以上是生活随笔為你收集整理的Codeforces 777E:Hanoi Factory(贪心+栈)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ST3新建py2和py3的build s
- 下一篇: SpringMVC:学习笔记(5)——数