Codeforces Round #401 (Div. 2) E. Hanoi Factory

题目链接:Codeforces Round #401 (Div. 2) E. Hanoi Factory

题意:

给你n个环,每个环有内径a,外径b,高度v,现在让你将这n个环重起来,问你能重的最大高度。

满足条件:bi>=bj,bj>ai。(i<j)

题解:

首先将所以数据离散化,然后我们先按b从大到小排序,以a为第二关键字。

这里有一个贪心的思想,就是b相同时,a越小的放上面,这样可以重更多。

然后用树状数组维护一下放当前环的最大值,更新一下答案就行。

这题也可以直接cdq分治,不过一般都是三维的时候才加。

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=a;i<=b;++i)
 3 using namespace std;
 4 typedef long long ll;
 5 
 6 const int N=1e5+7;
 7 struct node
 8 {
 9     int x,y,v;
10     bool operator <(const node &b)const{return y==b.y?x>b.x:y>b.y;}
11 }a[N];
12 
13 int n,hsh[N*2],ed,h_ed;
14 ll mx[N*2];
15 
16 inline void up(ll &a,ll b){if(a<b)a=b;}
17 void add(int x,ll c){while(x<=h_ed)up(mx[x],c),x+=x&-x;}
18 ll ask(int x){ll an=0;while(x)up(an,mx[x]),x-=x&-x;return an;}
19 int getid(int x){return lower_bound(hsh+1,hsh+1+h_ed,x)-hsh;}
20 
21 int main(){
22     scanf("%d",&n);
23     F(i,1,n)
24     {
25         scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].v);
26         hsh[++ed]=a[i].x,hsh[++ed]=a[i].y;
27     }
28     sort(hsh+1,hsh+1+ed);
29     h_ed=unique(hsh+1,hsh+1+ed)-hsh-1;
30     sort(a+1,a+1+n);
31     ll ans=0;
32     F(i,1,n)
33     {
34         ll tmp=ask(getid(a[i].y)-1);
35         up(ans,tmp+a[i].v);
36         add(getid(a[i].x),tmp+a[i].v);
37     }
38     printf("%lld\n",ans);
39     return 0;
40 }
View Code

 

转载于:https://www.cnblogs.com/bin-gege/p/6443923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值