您的位置:首页 > 理论基础 > 计算机网络

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

2017-09-25 22:47 411 查看

B:train seats reservation

时间限制:1000ms 内存限制:131072K

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 n, which can be as large as 1000. After n, there will be n lines representing the n reservations; each line contains three integers s, t, k, which means that the reservation needs k 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 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

*

输入: 第一行:有n套预订。

接下来n行:每行三个数,分别为:起始车站编号,终止车站编号,所需要的座位数。

输出:最少需要多少个座位,同时保证不出现冲突的情况。

解题思路:

1: 先定义一个结构体,分别为起始车站编号,终止车站编号,所需要的座位数。并定义一个数组来接受这个结构体,数组的大小为略大于总共多少套预订。

2:输入n套预订信息。

3:按照终止车站编号的大小,从小到大对结构体进行排序。

4:用一个双重循环,每一次拿相对较早的一个终止车站编号与后续的起始车站编号进行比较,如果后续的起始车站编号小于相对较早的一个终止车站编号,则累加它们的所需座位数,赋值给sum;而如果后续的起始车站编号大于等于相对较早的一个终止车站编号,如果累加的结果小于此后续的车站的所需的座位数,则将此后续的车站的所需的座位数赋值给sum。

5:在两重循环之间,判断每一次的sum值,将最大的sum值输出,即是所求结果。

代码思路:

1:定义一个结构体

struct node{
int s, t, w;
}stu[1005];


2:输入n套预订信息

for(i = 0; i < n; i++)
scanf("%d%d%d", &stu[i].s, &stu[i].t, &stu[i].w);


3:按照终止车站编号的大小,对结构体进行排序

int cmp(node a, node b)
{
if(a.t == b.t)
return a.s < b.s;
return a.t < b.t;
}


sort(stu, stu+n, cmp);


4:用一个双重循环,在第一重循环里,将sum初始化为相对较早的一套预订所需要的座位书。每一次拿相对较早的一个终止车站编号与后续的起始车站编号进行比较,如果后续的起始车站编号小于相对较早的一个终止车站编号,则累加它们的所需座位数,赋值给sum;而如果后续的起始车站编号大于等于相对较早的一个终止车站编号,如果累加的结果小于此后续的车站的所需的座位数,则将此后续的车站的所需的座位数赋值给sum。在两重循环之间,判断每一次的sum值,将最大的sum值输出,即是所求结果。

for(i = 0; i <= n - 2; i ++)
{
sum = stu[i].w;
for(j = i + 1; j <= n - 1; j ++)
{
if(stu[j].s < stu[i].t)
{
sum += stu[j].w;
}
else
{
if(sum < stu[j].w)
{
sum = stu[j].w;
}
}

}
if(sum > Max)
{
Max = sum;
}
}
printf("%d\n", Max);


错误原因:此题错一次

第一次:

1. 因为要考虑最后要取比较后的sum的最大值,忽略了这一点,导致错误。

经验总结:

1:无。。。

我的AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;

struct node{ int s, t, w; }stu[1005];
int cmp(node a, node b)
{
if(a.t == b.t)
return a.s < b.s;
return a.t < b.t;
}

int main()
{
int s, t, k, i, j, w, n, sum, Max;
while(scanf("%d", &n) != EOF)
{
Max = 0;
if(!n)
{
printf("*\n");
break;
}
for(i = 0; i < n; i++) scanf("%d%d%d", &stu[i].s, &stu[i].t, &stu[i].w);sort(stu, stu+n, cmp);

for(i = 0; i <= n - 2; i ++)
{
sum = stu[i].w;
for(j = i + 1; j <= n - 1; j ++)
{
if(stu[j].s < stu[i].t)
{
sum += stu[j].w;
}
else
{
if(sum < stu[j].w)
{
sum = stu[j].w;
}
}

}
if(sum > Max)
{
Max = sum;
}
}
printf("%d\n", Max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐