BZOJ 1577 贪心

思路:同POJ3038
http://blog.csdn.net/qq_31785871/article/details/52953214

//By SiriusRen
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int k,n,c,tot=1,size,ans;
struct Node{int s,e,m;}node[50050],jy;
multiset<Node>s;
multiset<Node>::iterator it;
bool operator < (Node a,Node b){return a.e<b.e;}
bool cmp(Node a,Node b){return a.s<b.s;}
int main(){
    scanf("%d%d%d",&k,&n,&c);
    for(int i=1;i<=k;i++)
        scanf("%d%d%d",&node[i].s,&node[i].e,&node[i].m);
    sort(node+1,node+1+k,cmp);
    for(int i=1;i<=n;i++){
        while(node[tot].s==i)size+=node[tot].m,s.insert(node[tot++]);
        it=s.begin();
        while((*it).e==i){
            ans+=(*it).m;size-=(*it).m;
            s.erase(it);it=s.begin();
        }
        while(size>c){
            it=s.end();it--;
            if(size-(*it).m<=c){
                jy=(*it);
                jy.m=(*it).m-(size-c);
                size=c;
                s.erase(it);
                s.insert(jy);
            }
            else size-=(*it).m,s.erase(it);
        }
    }
    printf("%d\n",ans);
}

这里写图片描述

posted @ 2016-11-02 15:20  SiriusRen  阅读(128)  评论(0编辑  收藏  举报