Train Seats Reservation(2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B)

Problem Description

You are given a list of train stations, say from the station 1 to the station 100.

The passengers can order several tickets from one station to another before the train leaves the station one. We will issue one train from the station 1 to the station 100 after all reservations have been made. Write a program to determine the minimum number of seats required for all passengers so that all reservations are satisfied without any conflict.

Note that one single seat can be used by several passengers as long as there are no conflicts between them. For example, a passenger from station 1 to station 10 can share a seat with another passenger from station 30 to 60.

Input

Several sets of ticket reservations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of orders, n, which can be as large as 1000. After nn, there will be n lines representing the n reservations; each line contains three integers s,t,k, which means that the reservation needs kk seats from the station s to the station t.These ticket reservations occur repetitively in the input as the pattern described above. An integer n=0 (zero) signifies the end of input.

Output

For each set of ticket reservations appeared in the input, calculate the minimum number of seats required so that all reservations are satisfied without conflicts. Output a single star '*' to signify the end of outputs.

Sample Input

2
1 10 8
20 50 20
3
2 30 5
20 80 20
40 90 40
0

​​​​​​​Sample ​​​​​​​Output

20
60
*

题意:t 组数据,每组给出 n 个 s t k 形式的订单,表示从站 s到站 t 有 k 个人坐车,问完成这 n 个订单最少要多少个座位

思路:

问题实质是求最大区间覆盖的值,由于 s、t 的数据范围仅到 100,那么可以借助桶排的思想模拟来做

开一个 100 的桶,扫描所有订单,将 s 的位置加入 k,代表有 k 个人上车,将 t 的位置减去 k,代表有 k 个人下车

最后从前向后扫描整个桶,记录桶的最大值即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int bucket[N];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        if(n==0){
            printf("*\n");
            break;
        }
        memset(bucket,0,sizeof(bucket));
        for(int i=1;i<=n;i++){
            int s,t,k;
            scanf("%d%d%d",&s,&t,&k);

            bucket[s]+=k;
            bucket[t]-=k;
        }

        int res=0;
        int sum=0;
        for(int i=1;i<=100;i++){
            sum+=bucket[i];
            res=max(sum,res);
        }
        printf("%d\n",res);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值