Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1895
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1895
Game of Limit
By Ricardo Oliveira, UFPR Brazil
Timelimit: 1
Alice and Bob decided to play a simple game to pass their time. The game is played with a deck containing N cards, numbered from 1 to N. One card is initially on the table. Also, there is a stack containing all other cards.
Alice begins taking the card on the top of the stack. She then verifies if the absolute difference between the card which is currently on the table and the card taken from the stack is at most a limit L. In other words, if the current card on the table is T and the card taken from the stack is S, then she verifies if |T-S| ≤ L. If that is true, she replaces the card on the table by the taken card, and scores |T-S| points. If it is not true, she does nothing -- the card on the table is not changed, and she does not score.
Bob then plays by doing the same. He takes a card from the stack, compares it with the card on the table, and moves accordingly. Alice then plays again, then Bob plays again, and so on. They keep playing until the stack of cards is empty. Your task is to determine the final score of both players.
Input
The first line contains three integers N, T0 and L (1 ≤ N < 60, N is odd, 1 ≤ T0 ≤ N, 1 ≤ L ≤ 10), the number of cards, the card initially on the table, and the limit L. The next N-1 lines contains an integer Si each (1 ≤ Si ≤ N). These are the cards in the stack, in order. The first card given in the input is at the top of the stack. All cards used in the game are distinct.
Output
Print a single line with two integers A and B separated by a single space, where A is Alice's final score, and B is Bob's final score.
Input Samples | Output Samples |
5 3 1 |
1 1 |
5 1 2 |
2 2 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main()
{
int card;
int card2;
int limite;
int pA, pB;
int n;
while (cin >> n >> card >> limite)
{
pA = pB = 0;
int vez = 0;
for (int i = 1; i < n ; ++i)
{
cin >> card2;
if (abs(card - card2) < = limite)
{
if (vez) pB += (abs(card - card2));
else pA += (abs(card - card2));
card = card2;
}
vez ^= 1;
}
cout << pA << ' ' << pB << '\n';
}
}
Copy The Code &
Try With Live Editor
Input
4
2
1
5
Output
#2 Code Example with Python Programming
Code -
Python Programming
n, to, l = [int(i) for i in input().split(" ")]
counta = 0
countb = 0
for i in range(n - 1):
e = int(input())
if abs(e - to) <= l:
if i % 2 == 0:
counta += abs(e - to)
else:
countb += abs(e - to)
to = e
print("%d %d" % (counta, countb))
Copy The Code &
Try With Live Editor
Input
4
2
1
5
Output