Algorithm
Problem Name: beecrowd | 1973
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1973
Star Trek
By Leandro Zatesko, UFFS Brazil
Timelimit: 1
After buying many adjacent farms at the west region of Santa Catarina, the Star family built a single road which passes by all farms in sequence. The first farm of the sequence was named Star 1, the second Star 2, and so on. However, the brother who lives in Star 1 has got mad and decided to make a Star Trek in order to steal sheep from the proprieties of his siblings. But he is definitely crazy. When passes by the farm Star i, he steals only one sheep (if there is any) from that farm and moves on either to Star i + 1 or Star i - 1, depending on whether the number of sheep in Star i was, respectively, odd or even. If there is not the Star to which he wants to go, he halts his trek. The mad brother starts his Star Trek in Star 1, stealing a sheep from his own farm.
Input
The first input line consists of a single integer N (1 ≤ N ≤ 106), which represents the number of Stars. The second input line consists of N integers, such that the ith integer, Xi (1 ≤ Xi ≤ 106), represents the initial number of sheep in Star i.
Output
Output a line containing two integers, so that the first represents the number of Stars attacked by the mad brother and the second represents the total number of non-stolen sheep.
Exemplo de Entrada | Exemplo de Saída |
8 |
8 68 |
8 |
7 63 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int main()
{
long long int n , i;
cin >> n;
int star[n] , sheep[n] ;
long long int star_count = 0 , sheep_count = 0;
for(i = 0 ; i < n ; i++){
cin >> sheep[i];
star[i] = 0;
}
i = 0;
while(1){
if(i == 0 && sheep[i]%2 == 0){
star[i] = 1;
if(sheep[i] > 0)
sheep[i]--;
break;
}
else if(i == n-1 && sheep[i]%2 == 1){
star[i] = 1;
if(sheep[i] > 0)
sheep[i]--;
break;
}
if(sheep[i]%2 == 1){
star[i] = 1;
sheep[i]--;
i++;
}
if(sheep[i]%2 == 0){
star[i] = 1;
if(sheep[i] > 0)
sheep[i]--;
i--;
}
}
for(i = 0 ; i < n ; i++>{
sheep_count += sheep[i];
star_count += star[i];
}
cout << star_count << " " << sheep_count << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 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 number = parseInt(prompt());
const stars = prompt().split(" ").map(Number);
var attacked = 0;
var i = 0;
var sheeps = 0;
while (i < number) {
attacked++;
if (stars[i] % 2 == 0) {
while (stars[i] >= 0) {
stars[i] = stars[i] - 1;
i = i - 1;
}
break;
}
stars[i] = stars[i] - 1;
i = i + 1;
}
for (let n = 0; n < number; n++) {
if (stars[n] < 0) {
stars[n] = 0;
}
sheeps = sheeps + stars[n];
}
console.log(attacked + " " + sheeps);
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
n = int(input())
x = [int(i) for i in input().split()]
total = sum(x)
atacadas = [0] * n
i = 0
while i >= 0 and i < n:
lado = x[i] % 2
if x[i] > 0:
x[i] -= 1
atacadas[i] = 1
total -= 1
if lado:
i += 1
else:
i -= 1
atacadas = atacadas.count(1)
print(atacadas, total)
Copy The Code &
Try With Live Editor
Input
Output