Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2378
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2378
Elevator
By OBI - Olimpíada Brasileira de Informática 2010 Brazil
Timelimit: 1
The Scaling with Big Comfort (SBC) it's a traditional company, with more than 50 years working on the production of elevators. All SBC's projects follow the most stringent safety standards, but unfortunately a series of accidents involving their elevators ruined the company's reputation.
By studying the accident, the company's engineers found that in several cases, the accident was caused by excess passengers in the elevator. So, SBC decided to monitor more strictly the use of their elevators: they installed a sensor in each door to count how many people get in and out in each floor. They have the records of an entire day of the elevator (that always start empty). They know that people are well polite and always let all the passenges go out before going inside it, yet they are having difficulties to decide if the maximum capacity was exceeded or not.
Your job is to write a program that, given a sequence of sensor readings and the maximum capacity of the elevator, determine if it was exceeded at any point.
Input
The first input line contain two integers, N and C, indicating the number of sensor readings and the maximum capacity of the elevator, respectively (1 ≤ N ≤ 1000 and 1 ≤ C ≤ 1000). The next N lines contain, each one, the sensor reading count. Each one have two integers, S and E, indicating how many people came out and how much entered in that floor, respectively (0 ≤ S ≤ 1000 and 0 ≤ E ≤ 1000).
Output
Your program must print a single line containing the character 'S' if the capacity was exceeded at any moment, or 'N' otherwise.
Input Samples | Output Samples |
5 10 0 5 2 7 3 3 5 2 7 0 |
N |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdbool.h >
int main (void)
{
unsigned short qtsLeituras, capacidadeMax;
unsigned short i, qtsPessoasSairam, qtsPessoasEntraram;
short lotacaoAtual;
bool ultrapassou = false;
scanf("%hu %hu", &qtsLeituras, &capacidadeMax);
lotacaoAtual = 0;
for (i = 0; i < qtsLeituras; i++)
{
scanf("%hd %hd", &qtsPessoasSairam, &qtsPessoasEntraram);
lotacaoAtual += qtsPessoasEntraram - qtsPessoasSairam;
if (lotacaoAtual > capacidadeMax && ultrapassou == false)
ultrapassou = true;
}
if (ultrapassou)
printf("S\n");
else
printf("N\n");
}
Copy The Code &
Try With Live Editor
Input
0 5
2 7
3 3
5 2
7 0
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
int main() {
int n, c, i, total = 0, excedeu = 0;
scanf("%d %d", &n, &c);
for (i = 0; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
total += b;
total -= a;
if (total > c) {
excedeu = 1;
}
}
if (excedeu == 0)
printf("N\n");
else
printf("S\n");
return 0;
}
Copy The Code &
Try With Live Editor
Input
0 5
2 7
3 3
5 2
7 0
Output