Algorithm
Problem Name: beecrowd | 2813
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2813
Avoiding Rain
By Christian Bonilha, UTFPR Brazil
Timelimit: 1
Rafael hates rain, and to avoid getting wet he started to use a weather forecasting system. In this system he can predict if it's going to rain at the time he goes to work and/or at the time he comes back from work.
Rafael also hates to carry an umbrella when it's not raining. To avoid it, he's going to buy several umbrellas and store them
at home and at the office, and he's only going to use it when it's raining. In other words, if it's raining at the time he goes to work, he'll take an umbrella that is at home, use it on the way to work, and leave it there. In a similar way, if it's raining at the time he comes back from work, he'll take an umbrella that is at the office, use it on the way home, and leave it there.
Given the meteorological forecasts, find out how many umbrellas Rafael must buy and store at home and at the office, in a way that he never gets wet and he never has to carry an umbrella if it's not raining.
Input
The first input row has an integer N, indicating how many days were forecast by the meteorological system (1 <= N <= 1000).
Following there will be N rows, each with two words SD and SN, indicating the forecast of the day at the time Rafael goes to work, and at the time Rafael comes back from work, respectively. If the word is "sol", it means that at this time it's not going to rain, and if the word is "chuva", it means that at this time it's going to rain.
Output
For each test case, you should print one row with two integers C and E, indicating how many umbrellas Rafael must buy and store at home and at the office.
Input Samples | Output Samples |
3 |
0 3 |
2 |
0 1 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream&t;
#include <string>
using namespace std;
int main()
{
int n, h=0, w=0, a=0, b=0;
cin >> n;
string s1, s2;
for (int i = 0; i < n; ++i)
{
cin >> ws >> s1 >> s2;
if (s1=="chuva")
{
if (h==0)
{
a++;
h++;
}
w++;
h--;
}
if (s2=="chuva")
{
if (w==0)
{
b++;
w++;
}
h++;
w--;
}
}
cout << a << " " << b << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner leitor = new Scanner(System.in);
int N = leitor.nextInt();
int casaComprado = 0;
int casaSobrando = 0;
int trabComprado = 0;
int trabSobrando = 0;
for (int i = 0; i < N; i++) {
String irProTrabalho = leitor.next();
String irPraCasa = leitor.next();
if (irProTrabalho.equalsIgnoreCase("chuva") && casaSobrando == 0) {
casaComprado++;
trabSobrando++;
} else if (irProTrabalho.equalsIgnoreCase("chuva") && casaSobrando > 0) {
trabSobrando++;
casaSobrando--;
}
if (irPraCasa.equalsIgnoreCase("chuva") && trabSobrando == 0) {
trabComprado++;
casaSobrando++;
} else if (irPraCasa.equalsIgnoreCase("chuva") && trabSobrando > 0) {
casaSobrando++;
trabSobrando--;
}
}
System.out.println(casaComprado + " " + trabComprado);
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
const cases = parseInt(prompt());
var weatherHome;
var weatherWork;
var storeHome = 0;
var storeWork = 0;
var home = 0;
var work = 0;
for (let i = 0; i < cases; i++) {
[weatherHome, weatherWork] = prompt().split(" ");
if (weatherHome == "chuva") {
if (home > 0) {
home--;
} else {
storeHome++;
}
work++;
}
if (weatherWork == "chuva") {
if (work > 0) {
work--;
} else {
storeWork++;
}
home++;
}
}
console.log(storeHome + " " + storeWork);
Copy The Code &
Try With Live Editor
Input
Output