PROBLEM LINK:
Author: Praveen Dhinwa
Tester: Kevin Atienza
Translators: Sergey Kulik (Russian), Team VNOI (Vietnamese) and Hu Zecong (Mandarin)
Editorialist: Kevin Atienza
DIFFICULTY:
Cakewalk
PREREQUISITES:
Loops, integer division
PROBLEM:
Given the history of a user’s activities in CodeChef and other relevant information regarding the new Laddu Accrual System, how many months can he/she redeem laddus?
EXPLANATION:
This is the easiest problem in the contest. A straightforward implementation will do. The idea is to first count the amount of laddus the user has accumulated, and then simply divide by 200 or 400, depending on the origin.
Here are sample implementations.
C++:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int cases;
cin >> cases;
while (cases--) {
int activities, points = 0;
string origin;
cin >> activities >> origin;
while (activities--) {
string kind;
cin >> kind;
if (kind == "CONTEST_WON") {
int rank;
cin >> rank;
int bonus = max(0, 20 - rank);
points += 300 + bonus;
} else if (kind == "TOP_CONTRIBUTOR") {
points += 300;
} else if (kind == "BUG_FOUND") {
int severity;
cin >> severity;
points += severity;
} else if (kind == "CONTEST_HOSTED") {
points += 50;
}
}
cout << points / (origin == "INDIAN" ? 200 : 400) << endl;
}
}
Python:
for cas in xrange(input()):
n, origin = raw_input().strip().split()
points = 0
for i in xrange(int(n)):
data = raw_input().strip().split()
kind = data[0]
if kind == 'CONTEST_WON':
rank = int(data[1])
bonus = max(0, 20 - rank)
points += 300 + bonus
elif kind == 'TOP_CONTRIBUTOR':
points += 300
elif kind == 'BUG_FOUND':
severity = int(data[1])
points += severity
elif kind == 'CONTEST_HOSTED':
points += 50
print points / (200 if origin == 'INDIAN' else 400)
Common mistakes
Being the easiest one, this attracts the most number of participants, and probably also the most number of mistakes. Here are the ones I found while looking at incorrect answers:
-
Opening a file. This causes “Time Limit Exceeded” or “Wrong Answer”. Remember that input is taken from stdin, and output should be written in stdout. No file input/output should be done. If you want to test your code locally using a file, use redirection, e.g.
my_program < my_input_file > my_output_file
which means “Runmy_program
with input frommy_input_file
, and write the output inmy_output_file
”. -
Printing unnecessary output. Remember that your answer will only get accepted if it exactly matches the intended output, character by character. So please don’t print anything like
Enter the number of test cases:
, because that is a part of the output. If you want to print auxiliary code, print to stderr instead, but don’t overdo it, otherwise you might get “Time Limit Exceeded” or “Output Limit Exceeded”. -
Some common coding errors, like writing
laddus = 300
instead ofladdus += 300
. -
Problems with “Bonus” computation, like forgetting that
bonus = 0
whenrank > 20
, or forgetting to add300
ifrank > 20
. -
Not allocating enough bytes for strings in languages with picky string input, like C. It’s generally a good idea to allocate more than needed, e.g. even though the longest string is
TOP_CONTRIBUTOR
which has 15 characters, you should allocate, say, 21 (or even 100) characters just to be safe. -
Incorrectly reading the input. One example I found is forgetting that
activities
andorigin
are found in the same line. You should pay careful attention to the input format.
These mistakes are typical when you’re still just starting programming, and programming contests. If you didn’t get accepted, don’t worry! Everyone starts somewhere, and I’m sure with more practice you’ll encounter these problems less often.
Time Complexity:
O(\text{activities})