Sicily 13914. Train Passengers

本文介绍了一个基于火车乘客上下车及等待人数的算法挑战,旨在通过分析各站点的乘客流动情况来判断列车容量是否合理配置。文章详细解释了输入输出格式,并提供了示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

13914. Train Passengers

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

The Nordic Company of Passing Carriages is losing money at an alarming rate because most of their trains are empty. However, on some lines the passengers are complaining that they cannot fit in the cars and have to wait for the next train!

The authorities want to fix this situation. They asked their station masters to write down, for a given train, how many people left the train at their station, how many went in, and how 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 have preferred to stay inside their cabin and make up the numbers instead of going outside and counting.

Verify your hunch by checking whether the input is inconsistent, i.e., at every time the number of people in the train did not exceed the capacity nor was below 0 and no passenger waited in vain. The train should start and finish the journey empty, in particular passengers should not wait for the train at the last station.

Input

The first line contains two integers C and n (2 <= n <= 100), the total capacity and the number of stations the train stops in. The next n lines contain three integers each, the number of people that left the train, entered the train, and had to stay at a station. Lines are given in the same order as the train visits each station. All integers including C are between 0 and 109 inclusive.

Output

One line containing one word: possible if the measurements are consistent, impossible otherwise.

Sample Input

样例一:
1 2
0 1 1
1 0 0
样例二:
1 2
1 0 0
0 1 0
样例三:
1 2
0 1 0
1 0 1
样例四:
1 2
0 1 1
0 0 0

Sample Output

样例一:
possible
样例二:
impossible
样例三:
impossible
样例四:
impossible

Problem Source

2015年每周一赛第四场

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值