Codeforce div2 #401 E. Hanoi Factory

来源:互联网 发布:演员知乎 编辑:程序博客网 时间:2024/06/04 19:37

传送门:E. Hanoi Factory

题意:给定n个空心圆柱形砖块和他们的内径外径以及高度,要把他们摞到最高并且满足上面的外径一定要小于等于下面的外径,上面的外径要大于下面的内径。

思路:赛后补的这个题,看完题后觉得是DP,看这个题的tag也有dp,但是想不出具体该怎么转移状态来,最终还是向题解妥协。很简单的一种思路是用栈去贪心的保存所用的砖块,先按外径排序,外径相等的话内径较大的在前面,这样能保证如果i无法入栈,那么i+1一直到n都无法入栈,当要入栈砖块的内径比栈顶的砖块的外径小时,就一直出栈直到满足条件,再将其入栈。这里更新结果的时候有点小技巧,具体看代码。

#include<stdio.h>#include<iostream>#include<string.h>#include<math.h>#include<algorithm>#include<queue>#include<stack>#include<set>#include<vector>#include<map>#define ll long long#define pi acos(-1)#define inf 0x3f3f3f3f#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define Rep(i,x,n) for(int i=x;i<n;i++)using namespace std;typedef pair<int,int>P;const int MAXN=100010;int gcd(int a,int b){return b?gcd(b,a%b):a;}struct node{int a,b,h;bool operator <(node x)const{if(b==x.b)return a>x.a;return b>x.b;}}q[MAXN];stack<node>st;int main(){int n;ll ans=0;cin>>n;for(int i=0;i<n;i++)scanf("%d%d%d",&q[i].a,&q[i].b,&q[i].h);sort(q,q+n);ll temp=0;for(int i=0;i<n;i++){if(st.empty()){st.push(q[i]);temp+=q[i].h;}else{while(!st.empty()){if(st.top().a>=q[i].b){node t=st.top();st.pop();temp-=t.h;}else{st.push(q[i]);temp+=q[i].h;break;}}if(st.empty()){st.push(q[i]);temp+=q[i].h;}}ans=temp>ans?temp:ans;}cout<<ans; return 0;}

在long long上贡献一发wa。

0 0