ACM 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation
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; }轉載于:https://www.cnblogs.com/moon13579/p/7662915.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ACM 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Debugging]分析博客园提交评论
- 下一篇: Java structured lock