13914. Train passengersConstraints
Time limit:1 secs, Memory limit:256 MB
Description
The Nordic company of passing carriages was losing money at a alarming rate because most of their trains was empty. However, on some lines the passengers is complaining that they cannot fit in the cars and has to wait for the next train !
The authorities want to fix this situation. They asked their station masters to write off, for a given train, how many people left the train at their station, how Ma NY went in, and what many had to wait. Then they hired your company of highly paid consultants to assign properly sized trains to their routes.
You just received the measurements for a train, but before feeding them to your optimisation algorithm you remembered that They were collected on a snowy day, so any sensible station master would has preferred to stay inside their cabin and MA Ke up the numbers instead of going outside and counting.
Verify your hunch by checking whether the input was inconsistent, i.e., at every time the number of people in the train did not exceed the capacity nor is below 0 and no passenger waited in vain. The train should start and finish the journey empty, in particular passengers should don't wait for the train at the last St ation.
Input
The first line contains-integers C and n (2 <= n <=), the total capacity and the number of stations the TR Ain stops in. The next n lines contain three integers each, the number of people so left the train, entered the train, and had to stay At a station. Lines is given in the same order as the train visits each station. All integers including C is between 0 and 109 inclusive.
Output
One line containing one word:possible if the measurements is consistent, impossible otherwise.
Sample Input
Example 1:
1 2
0 1 1
1 0 0
Example 2:
1 2
1 0 0
0 1 0
Example three:
1 2
0 1 0
1 0 1
Example four:
1 2
0 1 1
0 0 0
Sample Output
Example 1:
possible
Example 2:
impossible
Example three:
impossible
Example four:
impossible
Problem Source
2015 every Monday match fourth game
#include <stdio.h>
int main() {
bool possible = true;
int c, n, m = 0, l, e, w;
scanf("%d%d", &c, &n);
while (n && n--) {
scanf("%d%d%d", &l, &e, &w);
if (!(0 <= l && l <= m)) possible = false;
m -= l;
if (!(0 <= e && e <= c - m)) possible = false;
m += e;
if (0 > w || (c > m && w > 0)) possible = false;
}
while (n--) scanf("%d%d%d", &l, &e, &w);
printf(possible && !m ? "possible\n" : "impossible\n");
return 0;
}
Sicily 13914. Train passengers