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

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值