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 30to 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 行,每行輸入三個數s,t,k表示將有 k 個人在 s 站上車,在 t 站下車,然後問你這個列車在本次行駛時最多需要多少個座位。這題屬於需要思考的水題,希望童鞋們能獨立完成。
附代碼:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
///o(っ。Д。)っ AC萬歲!!!!!!!!!!!!!!
const int maxn = 1000;
int ss[maxn] = {}, tt[maxn];
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
memset(ss, 0, sizeof(ss));
memset(tt, 0, sizeof(tt));
if(n == 0)
{
printf("*\n");
break;
}
int u, v, k;
for(int i = 1; i <= n; i++)
{
scanf("%d %d %d", &u, &v, &k);
ss[u] += k;
tt[v] += k;
}
int sum = 0, maxx = 0;
bool flag = false;
for(int i = 1; i <= 104; i++)
{
sum += ss[i];
sum -= tt[i];
maxx = max(maxx, sum);
//printf("i = %d sum = %d\n", i, sum);
}
printf("%d\n", maxx);
}
return 0;
}
///參考數據,基本全過了就能AC
/*
4
1 20 60
5 10 12
10 60 20
60 80 40
3
1 20 60
5 10 12
60 80 40
2
1 10 8
20 50 20
3
2 30 5
20 80 20
40 90 40
5
10 60 10
26 40 30
40 60 60
20 30 20
20 60 20
*/