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

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 Format

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, nn, which can be as large as 10001000. After nn, there will be nn lines representing the nn reservations; each line contains three integers s, t, ks,t,k, which means that the reservation needs kk seats from the station ss to the station tt .These ticket reservations occur repetitively in the input as the pattern described above. An integer n = 0n=0 (zero) signifies the end of input.

Output Format

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.

样例输入

2
1 10 8
20 50 20
3
2 30 5
20 80 20
40 90 40
0
样例输出

20
60

  • 题目分析:
    有1-100个站点,乘客将会下订单预定从 s 站点到 t 站点中的 k 个座位,不同区间的座位之间可以自由分享,比如从1-30站点的座位可以给50-80站点的乘客。题目要我们找出每个样例中所需要的最小座位数。

  • 注意:1-10的座位也可以给10-20的乘客

  • 我的思路:
    对每一个站点都计算:


    由于数据比较小,进行区间覆盖,一个区间的需要的座位数覆盖到每一个站点上去,这样当所有的区间都覆盖完成之后,所有的站点需要的座位数就会出现一个峰值,那个峰值就是我们想要的。
    这里写图片描述

  • 完整代码:

#include<stdio.h>
#include<string.h>
#define MAX 105
int main(void)
{
    int  n, s, t, k, max, seat[MAX];
    while (scanf("%d", &n)!=EOF)
    {
        max = 0;
        memset(seat, 0, sizeof(int)*MAX);
        if (n == 0)
        {
            printf("*
");
            break;
        }
        while (n-- > 0)
        {
            scanf("%d%d%d", &s, &t, &k);
            for (int i = s; i < t; i++)
                seat[i] += k;
        }
        for (int i = 1; i < MAX; i++)
        {
            if (seat[i] > max)
                max = seat[i];
        }
        printf("%d
", max);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/FlyerBird/p/9052560.html