前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >B. Train Seats Reservation 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

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

作者头像
Enterprise_
发布2019-02-21 17:08:22
5140
发布2019-02-21 17:08:22
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆

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的乘客
  • 我的思路: 对每一个站点都计算:
这里写图片描述
这里写图片描述

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

这里写图片描述
这里写图片描述
  • 完整代码:
代码语言:javascript
复制
#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("*\n");
            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\n", max);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年09月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档