CodeForcesGym 100502K Train Passengers

Train Passengers

Time Limit: 1000ms

Memory Limit: 524288KB

This problem will be judged on CodeForcesGym. Original ID: 100502K
64-bit integer IO format: %I64d      Java class name: (Any)

  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 ?t in the cars and have to wait for the next train! The authorities want to ?x 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 ?nish the journey empty, in particular passengers should not wait for the train at the last station.

Input

The ?rst 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

1 2

0 1 1

1 0 0

Sample Output 1

possible

Sample Input 2

1 2

1 0 0

0 1 0

Sample Output 2

impossible

Sample Input 3

1 2

0 1 0

1 0 1

Sample Output 3

impossible

Sample Input 4

1 2

0 1 1

0 0 0

Sample Output 4

impossible

解题:直接模拟就好

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 bool check(int n,int C){
 5     int left,went,wait;
 6     LL cur = 0;
 7     bool flag = true;
 8     for(int i = 0; i < n; ++i){
 9         scanf("%d %d %d",&left,&went,&wait);
10         if(cur - left < 0) flag = false;
11         cur += went - left;
12         if(cur > C || cur < C && wait) flag = false;
13     }
14     if(cur != 0) flag = false;
15     return flag;
16 }
17 int main(){
18     int n,C;
19     while(~scanf("%d %d",&C,&n))
20         printf("%spossible\n",check(n,C)?"":"im");
21     return 0;
22 }

时间: 2024-10-28 11:37:12

CodeForcesGym 100502K Train Passengers的相关文章

Sicily 13914. Train Passengers

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 com

csu 1584 Train Passengers

#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<stack> #include<vector> #include<stdlib.h> #include<algorithm> using namespace std; int dir4[][2]={{0,1},{

【模拟】NCPC 2014 K Train passengers

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1797 题目大意: 有N个车站,火车一共可以坐M个人,每个车站下车Ai,上车Bi个人,在车站等待下一班Ci个人.问输入是否合法. 合法:火车上的人不超过M,第一站不能有人下车,最后一站不能有人上车,火车满的时候才能有人在车站等下一班. 题目思路: [图论] 签到水题.模拟到达每个车站的状态即可. 1 // 2 //by coolxxx 3 //#include<bits/stdc++.

CSU1584: Train Passengers

Description Input Output Sample Input 1 2 0 1 1 1 0 0 Sample Output possible 题意:给出火车容量c,站点数n 对于每个站点给出上车人数,下车人数和等待人数 问最后是否能按要求完成旅途 思路:这道题理解了蛮久,后来得到如下确定 1.最后一站,计算完之后,必须车上无人,没人上车,没人等待 2.超出车的容量,必然不行 3.如果车没上满,却还有等待的人,那么是不行的,因为车没上满我干嘛不上啊,还等毛线啊 #include <io

HDU1022 Train Problem I (栈)

栈+队列 1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 int main() 7 { 8 int n; 9 char a[11],b[11]; 10 stack<char>s; 11 queue<int>q; 12 while(scanf("%d",&

HDU 1022 Train Problem I

描述As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there i

hdu 1022 Train Problem

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30588    Accepted Submission(s): 11561 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

Train Problem II 卡特兰裸题(入门题)

Train Problem II  题目大意:给你一个数n,表示有n辆火车,编号从1到n,从远方驶过来,问你有多少种出站的可能. 解题思路:模拟栈的问题而已.  卡特兰问题. 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 5 public class Main 6 { 7 static int MS=101; 8 public static void main(String[] args) 9 { 10 Sca

POJ3007(Organize Your Train part II)

传送门 Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K       Description RJ Freight, a Japanese railroad company for freight operations has recently constructed exchange lines at Hazawa, Yokohama. The layout of the lines is shown i