Algorithm
Problem Name: beecrowd | 2162
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2162
Peaks and Valleys
By M.C. Pinto, UNILA Brazil
Timelimit: 1
Professor MC realized that at each 100 meter interval there is a peak in the Nlogony landscape. And that at exactly half way of each two peaks there is a valley. That means that at each 50 meters there is a valley or a peak and, alongside the landscape, there is not a peak followed by another peak neither there is a valley followed by another valley.
Professor MC got curious with that pattern and wants to know if this happens again to other landscapes. Your task is, given a landscape, to indicate if it has this pattern.
Input
The input is given in two lines. The first one has the number N of landscape measures (1 < N ≤ 100). The second line has N integers: the height Hi of each measure (-10000 ≤ Hi ≤ 10000, for all Hi, such that 1 ≤ i ≤ N). A measure is considered a peak if it is higher than the previous measure. A measure is considered a valley if it is lower than the previous measure.
Output
The output is given in one single line. If the landscape has the same pattern of Nlogony it must be shown the number 1. Otherwise, the number 0 must be shown.
Input Samples | Output Samples |
3 |
1 |
5 |
1 |
4 |
0 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main() {
int paisagens, atual, anterior, ehPadrao = 1, pico;
scanf("%d",&paisagens);
if (paisagens == 1) {
//se eu só tenho 1 paisagem, então já está no padrão
scanf("%d",&atual);
printf("%d\n", ehPadrao);
} else if (paisagens == 2) {
//se eu tenho 2 paisagens, elas não podem ser iguais
scanf("%d",&anterior);
scanf("%d",&atual);
printf("%d\n", !(atual == anterior));
} else {
// caso eu tenha 3 ou mais paisagens
// leio as duas primeiras, e decremento o contador de paisagens
scanf("%d",&anterior);
scanf("%d",&atual);
paisagens -= 2;
// verifico se as duas primeiras paisagens formam um pico.
pico = atual > anterior;
do {
//leio as paisagens seguintes 1 a uma
anterior = atual;
scanf("%d",&atual);
paisagens--;
// se ate a leitura atual eu ainda estou dentro do padrão, eu devo continuar testando
// caso contrário, só vou lendo os valores até o fim, sem realizar nenhum processamento.
if (ehPadrao) {
if (pico) {
// se eu ainda estou no padrão e minhas duas paisagens anteriores formavam um pico
// então meu atual deve ser menor que o anterior (vale)
ehPadrao = atual < anterior ;
//agora eu não tenho um pico e sim um vale..
pico = 0;
} else {
// se eu ainda estou no padrão e minhas duas paisagens anteriores formavam um vale
// então meu atual deve ser maior que o anterior (pico)
ehPadrao = atual > anterior;
//agora eu não tenho um pico e sim um vale..
pico = 1;
}
}
} while (paisagens > 0);
printf("%d\n",ehPadrao);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,most=1,a[101],c1=0,c2=0;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
if(a[0] < a[1]){
for(int i = 0; i < n-1; i++){
if(i%2==0){
if(a[i] < a[i+1]> {
c1++;
}
}else{
if(a[i] > a[i+1]) c2++;
}
}
}else if(a[0]>a[1]){
for(int i = 0; i < n-1; i++){
if(i%2==0>{
if(a[i] > a[i+1]){
c1++;
}
}else{
if(a[i] < a[i+1]){
c2++;
}
}
}
}
// cout << c1 << c2 << c1+c2+1 << endl;
if(c1+c2+1 == n>cout << 1 << endl;
else cout << 0 << endl;
return 0 ;
}
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');
const A = lines.shift();
const picos = lines.shift().split(" ");
let temp;
let below = 0;
let above = 0;
let equal = 0;
for(let i = 0; i < picos.length, i <= parseInt(A); i++){
if(i == 0){
temp = parseInt(picos[0]);
}
else{
if(temp < picos[i]){
temp = parseInt(picos[i]>;
above++;
below = 0;
}
else if(temp > picos[i]){
temp = parseInt(picos[i]);
below++;
above = 0;
}
else if(temp == picos[i]){
equal++;
}
if(above == 2 || below == 2 || equal == 1){
console.log("0");
equal++
break;
}
}
}
if(above < 2 && below < 2 && equal == 0){
console.log("1">;
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
n = int(input())
h = [int(x) for x in input().split()]
if n == 2 and h[0] == h[1]:
pico = 0
else:
pico = 1
for i in range(1, n-1):
if not ((h[i] < h[i-1] and h[i] < h[i+1]) or (h[i] > h[i-1] and h[i] > h[i+1])):
pico = 0
break
print(pico)
Copy The Code &
Try With Live Editor
Input
Output