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 population divided into age groups [1, 10], [11, 20], [21, 30], \ldots, [91, \infty). The age groups are numbered from 1 to 10 and there are X_i people in age group i.
The COVID vaccine drive has started, and people would be vaccinated in the decreasing order of their age groups. Every day exactly P people are vaccinated. If there are less than P people left in the current age group, then the remaining doses of that day are given to people of immediate smaller age group on the same day. People within the same age group can be vaccinated in any order.
Chef is in age group G, determine the minimum and the maximum number of days will it take for Chef to be vaccinated.
QUICK EXPLANATION
- All people in age groups older than G would be vaccinated before Chef and all people in age groups younger than Chef will be vaccinated after Chef.
- So Chef can only choose any position in the order people in age group G are vaccinated.
- For the minimum day, the Chef shall be the first in his group to be vaccinated, and for the maximum day, Chef will be the last in his group to be vaccinated.
EXPLANATION
Since the age groups are processed in descending order, Chef has to wait for all the age groups older than the age group G to be vaccinated.
Similarly, all people including Chef shall be vaccinated before any age group younger than the age group G is vaccinated.
Chef has no control over the above two facts, so He has to wait for older groups to be vaccinated first, and he shall be vaccinated before the lower groups. All Chef can control is his position in the order, people of age group G are vaccinated.
Let us assume there are total S people in age groups older than age group G (\displaystyle S = \sum_{i = G+1}^{10} X_i) and C = X_G denote the number of people in age group G.
If we visualize all people from all age groups standing in a queue in the order they would be vaccinated, we can see that there are at least S people before Chef, and Chef is among the first S+C people. Hence, the position chef can be standing in this queue is between [S+1, S+C]
The first P people are vaccinated on day 1, the next P on day 2, and so on. So, for the minimum day, Chef must stand at position S+1, and for maximum day, Chef must stand at position S+C. Hence, the minimum day the Chef is vaccinated is the day the S+1-th person is vaccinated, and the maximum day the Chef is vaccinated, is the day the S+C-th person in the queue is vaccinated.
By basic math, we can see that person at position X is vaccinated on day \displaystyle \bigg\lceil \frac{X}{P} \bigg\rceil.
Hence, minimum day is \displaystyle \bigg\lceil \frac{S+1}{P} \bigg\rceil and maximum day is \displaystyle \bigg\lceil \frac{S+C}{P} \bigg\rceil.
TIME COMPLEXITY
The time complexity is O(1) per test case.
SOLUTIONS
Setter's Solution
#include<bits/stdc++.h>
# define pb push_back
#define pii pair<int, int>
#define mp make_pair
# define ll long long int
using namespace std;
const int maxt = 1e4, maxp = 1e5, minpp = 1, maxpp = 1e5;
const string newln = "\n", space = " ";
int main()
{
int t; cin >> t;
int g, p;
int a[11];
while(t--){
cin >> g >> p;
for(int i = 1; i <= 10; i++){
cin >> a[i];
}
int days = 0;
for(int j = 10; j > g; j--){
days += a[j] / p; a[j - 1] += a[j] % p;
}
int minans = days + 1, maxans = days + (a[g] + p - 1) / p;
cout << minans << " " << maxans << 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 = readIntLn(1, 10000);
forn(tc, T)
{
int G = readIntSp(1, 10);
int P = readIntSp(1, 100'000);
vector<int> X(10);
int su1 = 0;
int su2 = -1;
forn(i, 10)
{
if(i<9)
X[i] = readIntSp(1, 100'000);
else
X[i] = readIntLn(1, 100'000);
if (i >= G)
su1 += X[i];
if (i + 1 >= G)
su2 += X[i];
}
printf("%d %d\n", su1 / P + 1, su2 / P + 1);
}
assert(getchar() == -1);
return 0;
}
Editorialist's Solution
import java.util.*;
import java.io.*;
class VACCINE3{
//SOLUTION BEGIN
void pre() throws Exception{}
void solve(int TC) throws Exception{
int G = ni(), P = ni(), N = 10;
int[] X = new int[1+N];
for(int i = 1; i<= N; i++)X[i] = ni();
int older = 0;
for(int i = G+1; i<= N; i++)older += X[i];
int minRank = 1+older;
int maxRank = older+X[G];
int minDay = (minRank+P-1)/P, maxDay = (maxRank+P-1)/P;
pn(minDay+" "+maxDay);
}
//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 VACCINE3().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.