PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Practice
Setter: Daanish Mahajan
Tester: Istvan Nagy
Editorialist: Taranpreet Singh
DIFFICULTY
Cakewalk
PREREQUISITES
None
PROBLEM
Given the number of gold, silver, and bronze medals won by two countries, find which country shall be ranked better than the other, if countries are ranked by the total number of medals won. It is guaranteed that the tie didn’t happen.
QUICK EXPLANATION
- If G_1 + S_1 + B_1 \gt G_2 + S_2 + B_2, then first country is better, else second country.
EXPLANATION
Idea
We just need to do what is mentioned in problem statement. Compute sum of medals won by each country, let’s denote T_1 = G_1 + S_1 + B_1 and T_2 = G_2 + S_2 + B_2.
If T_1 \gt T_2, then the first country is ranked better, otherwise the second country is ranked better.
Implementation
int G1, S1, B1, G2, B2, S2;
cin>>G1>>S1>>B1>>G2>>S2>>B2;
int T1 = G1+S1+B1, T2 = G2+S2+B2;
if(T1 > T2)cout<<1<<endl;
else cout<<2<<endl;
TIME COMPLEXITY
The time complexity is O(1) per test case.
SOLUTIONS
Setter's Solution
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define rb pop_back
#define ti tuple<int, int, int>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
using namespace std;
const int maxt = 1000, maxm = 30;
const string newln = "\n", space = " ";
int main()
{
int t; cin >> t;
int a[6];
while(t--){
for(int i = 0; i < 6; i++)cin >> a[i];
int s1 = a[0] + a[1] + a[2], s2 = a[3] + a[4] + a[5];
assert(s1 != s2);
int ans = 2;
if(s1 > s2)ans = 1;
cout << ans << endl;
}
}
Tester's Solution
#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <random>
#ifdef HOME
#include <windows.h>
#endif
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
using namespace std;
long long readInt(long long l, long long r, char endd) {
long long x = 0;
int cnt = 0;
int fi = -1;
bool is_neg = false;
while (true) {
char g = getchar();
if (g == '-') {
assert(fi == -1);
is_neg = true;
continue;
}
if ('0' <= g && g <= '9') {
x *= 10;
x += g - '0';
if (cnt == 0) {
fi = g - '0';
}
cnt++;
assert(fi != 0 || cnt == 1);
assert(fi != 0 || is_neg == false);
assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
}
else if (g == endd) {
assert(cnt > 0);
if (is_neg) {
x = -x;
}
assert(l <= x && x <= r);
return x;
}
else {
//assert(false);
}
}
}
string readString(int l, int r, char endd) {
string ret = "";
int cnt = 0;
while (true) {
char g = getchar();
assert(g != -1);
if (g == endd) {
break;
}
cnt++;
ret += g;
}
assert(l <= cnt && cnt <= r);
return ret;
}
long long readIntSp(long long l, long long r) {
return readInt(l, r, ' ');
}
long long readIntLn(long long l, long long r) {
return readInt(l, r, '\n');
}
string readStringLn(int l, int r) {
return readString(l, r, '\n');
}
string readStringSp(int l, int r) {
return readString(l, r, ' ');
}
int main(int argc, char** argv)
{
#ifdef HOME
if(IsDebuggerPresent())
{
freopen("../in.txt", "rb", stdin);
freopen("../out.txt", "wb", stdout);
}
#endif
int T;
T = readIntLn(1, 1000);
forn(tc, T)
{
int G1, S1, B1, G2, S2, B2;
G1 = readIntSp(0, 30);
S1 = readIntSp(0, 30);
B1 = readIntSp(0, 30);
G2 = readIntSp(0, 30);
S2 = readIntSp(0, 30);
B2 = readIntLn(0, 30);
int r1 = G1 + B1 + S1;
int r2 = G2 + B2 + S2;
assert(r1 != r2);
if (r1 < r2)
{
printf("2\n");
}
else
{
printf("1\n");
}
}
return 0;
}
Editorialist's Solution
import java.util.*;
import java.io.*;
class OLYRANK{
//SOLUTION BEGIN
void pre() throws Exception{}
void solve(int TC) throws Exception{
int S1 = ni()+ni()+ni(), S2 = ni() + ni()+ ni();
if(S1 > S2)pn(1);
else if(S2 > S1)pn(2);
else hold(false);
}
//SOLUTION END
void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
static boolean multipleTC = true;
FastReader in;PrintWriter out;
void run() throws Exception{
in = new FastReader();
out = new PrintWriter(System.out);
//Solution Credits: Taranpreet Singh
int T = (multipleTC)?ni():1;
pre();for(int t = 1; t<= T; t++)solve(t);
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
new OLYRANK().run();
}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
String n()throws Exception{return in.next();}
String nln()throws Exception{return in.nextLine();}
int ni()throws Exception{return Integer.parseInt(in.next());}
long nl()throws Exception{return Long.parseLong(in.next());}
double nd()throws Exception{return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception{
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception{
String str = "";
try{
str = br.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
}
Feel free to share your approach. Suggestions are welcomed as always.