Day1-T3
Describe:两个限制条件,求第三属性的最大和
code:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
long long ans,n,f[101010],tot; //f[i]表示sort后枚举到第i个时的最优解
struct node{
int a;
}p[10101010],ls[10101010]; //p仅仅是为了方便做大根堆排,ls是堆顶 pop 出的f[i]
struct huan{
int a,b,h;
}a[101010]; //题意所示
inline bool operator <(node x,node y){ //堆排规则限且仅限f[i]的大小
return f[x.a]<f[y.a];
}
priority_queue<node>q;
inline bool cmp(huan x,huan y){ //开局一sort
return x.b==y.b?x.a>y.a:x.b>y.b;
}
inline int read(){
int ret=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
inline void write(int x){
if(x/10)write(x/10);putchar(x%10+'0');
}
int main(){
// freopen("hanoi.in","r",stdin);
// freopen("hanoi.out","w",stdout);
n=read();for(int i=1;i<=n;i++)
p[i].a=i,a[i].a=read(),a[i].b=read(),a[i].h=read();
sort(a+1,a+n+1,cmp); // sort
for(int i=1;i<=n;i++){
while(!q.empty()){
if(a[i].b>a[q.top().a].a){f[i]=f[q.top().a]+(long long)a[i].h;break;} //弹到第一次满足条件为止(此时f[i]=max{f[i]})
ls[++tot]=q.top();q.pop(); //弹出
}
if(!f[i]){f[i]=(long long)a[i].h;q.push(p[i]);continue;} //弹完了依旧不满足
while(tot){q.push(ls[tot]);--tot;}q.push(p[i]);tot=0; //弹出的放回去
}
for(int i=1;i<=n;i++)ans=max(ans,f[i]); //取个 ans
printf("%lld",ans);
return 0;
}

浙公网安备 33010602011771号