2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B题 离散化+贪心

来源:互联网 发布:世界局势知乎 编辑:程序博客网 时间:2024/05/20 03:43

题目链接:https://nanti.jisuanke.com/t/17309

题意:给定一组人的乘车区间,区间不重合的座位可以重复使用,求最少需要用多少个座位。

思路:把区间离散化,设置一个标志表示是否是区间开始,然后排一个序,每次遇到一个区间起点就加进贡献里面,遇到区间终点总贡献减去对应人数,每次更新贡献值统计最大值,这个最大值就是答案。

#include <iostream>#include <cmath>#include <stdio.h>#include <algorithm>#include <string>#include <cstring>#include <sstream>#include <queue>#include <map>#include <set>using namespace std;typedef long long ll;const int maxn = 1003;struct node{    int s;      //区间位置    int k;      //区间人数    bool is_head;}da[maxn*2];bool cmp(node a,node b){    if(a.s!=b.s)        return a.s<b.s;    else return a.is_head<b.is_head;}int n;int main(){    while(~scanf("%d",&n)){        if(!n){            printf("*\n");            break;        }        int tot=0;        for(int i=0;i<n;i++){            int s,t,k;            scanf("%d%d%d",&s,&t,&k);            da[tot].s=s;            da[tot].is_head=true;            da[tot].k=k;            tot++;            da[tot].s=t;            da[tot].is_head=false;            da[tot].k=k;            tot++;        }        sort(da,da+tot,cmp);        ll ans=0;        ll mx=0;        for(int i=0;i<tot;i++){            if(da[i].is_head){                ans+=da[i].k;                mx=max(ans,mx);            }            else{                ans-=da[i].k;                mx=max(ans,mx);            }        }        printf("%lld\n",mx);    }    return 0;}
阅读全文
0 0