We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: JainWinn
Just calculate length of longest subarray containing only $$$7$$$.
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define endl '\n'
void solve(){
int n;
cin>>n;
vi a(n,0);
for(int i=0 ; i<n ; i++){
cin>>a[i];
}
int maxi=0;
for(int i=0 ; i<n ; i++){
int curr=0;
while(i<n && a[i]==7){
curr++;
i++;
}
maxi=max(maxi,curr);
}
cout<<maxi<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: JainWinn
Given $$$n$$$ poles and each pole can be used at most $$$k$$$ times, the answer is simply $$$ \frac{(n \times k)}{3} $$$ if $$$n \gt 2 $$$ and $$$0$$$ otherwise. We can treat each pole as collection of $$$k$$$ poles such that our selection of poles for each triangle ensure that each pole belongs to different pole collection.
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#define int long long
#define endl '\n'
void solve(){
int n,k;
cin>>n>>k;
if(n<3){
cout<<0<<endl;
return;
}
cout<<(n*k)/3<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: JainWinn
This problem can be solved using priority queue or ordered set. Sort vector/pair of start and end time of examinations by start time. Sort pair/vector of invigilation time and it's index by invigilation time. Now iterating through each invigilation time in increasing order, we can calculate number of students invigilated using priority queue or ordered set. Push the end times of those examinations in min priority queue whose start time is smaller than or equal to current invigilation time. Now pop those examination end times from priority queue which are smaller than current invigilation time. The size of priority queue gives number of students invigilated for current invigilation time. This can also be done using ordered set. The problem can also be solved using line sweeping technique, but I leave this task on the participants to do it by themselves.
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
template <class T> using pbds = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpi vector<pair<int,int>>
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
void printarray(vector<int> &a){
for(int i=0 ; i<(int)a.size() ; i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
void solve(){
int n;
cin>>n;
vi st(n,0);
for(int i=0 ; i<n ; i++){
cin>>st[i];
}
vi ed(n,0);
for(int i=0 ; i<n ; i++){
cin>>ed[i];
}
int m;
cin>>m;
vpi t;
for(int i=0 ; i<m ; i++){
int x;
cin>>x;
t.pb(make_pair(x,i));
}
vvi s;
for(int i=0 ; i<n ; i++){
s.pb({st[i],ed[i]});
}
sort(all(s));
sort(all(t));
int itr=0;
vi ans(m,0);
priority_queue<int,vector<int>,greater<int>> exam;
for(int i=0 ; i<m ; i++){
while((itr<n) && (s[itr][0] <= t[i].ff)){
exam.push(s[itr][1]);
itr++;
}
while(!exam.empty() && exam.top()<t[i].ff){
exam.pop();
}
int curr=exam.size();
ans[t[i].ss]=curr;
}
printarray(ans);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
template <class T> using pbds = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpi vector<pair<int,int>>
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
void printarray(vector<int> &a){
for(int i=0 ; i<(int)a.size() ; i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
void solve(){
int n;
cin>>n;
vi st(n,0);
for(int i=0 ; i<n ; i++){
cin>>st[i];
}
vi ed(n,0);
for(int i=0 ; i<n ; i++){
cin>>ed[i];
}
int m;
cin>>m;
vpi t;
for(int i=0 ; i<m ; i++){
int x;
cin>>x;
t.pb(make_pair(x,i));
}
vvi s;
for(int i=0 ; i<n ; i++){
s.pb({st[i],ed[i],i});
}
sort(all(s));
sort(all(t));
int itr=0;
vi ans(m,0);
pbds<pair<int,int>> exam;
for(int i=0 ; i<m ; i++){
while((itr<n) && (s[itr][0] <= t[i].ff)){
exam.insert({s[itr][1],s[itr][2]});
itr++;
}
int curr=exam.order_of_key({t[i].ff,-1});
ans[t[i].ss]=itr-curr;
}
printarray(ans);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: vrintle
When a player have to pick up indices for any chosen character, its obvious that they will pick up every index available greedily in order to maximize score. Now, this game can be realized in a recursive way. Starting with first turn, when Alice deletes the indices for a character $$$c_1 = $$$ 'a', and Bob picks up next character say $$$c_2$$$, then it can be seen that this game restarts with Bob as first player who have chosen character $$$c_2$$$ and Alice as second player who will choose now. So, if both Alice and Bob can compute the optimal path, then they can easily decide which character to choose. Let's try to write one recursive function get_delta(choice, player) which will tell us what can be the maximum delta between the scores, i.e., $$$s_1 - s_2$$$.
// player can be 0 or 1 (first or second)
// choice is the character that the first player have to choose compulsorily
// get_delta gives the maximum value of first_score - second_score
int get_delta(char choice, bool player) {
int s1 = 0;
for(auto& c: str) s1 += c == choice;
int s2 = 0;
for(char c2 = choice + 1; c2 <= 'z'; c2++) {
int del = get_delta(c2, player ^ 1); // xor to switch players
s2 = max(s2, del); // second player will always try to maximize delta for their choice
}
return s1 - s2;
}
This recursive function simulates the whole game now. We can optimize this recursion further by using memoization to save the results of the states using a $$$dp[choice][player]$$$. But, still its time complexity is around $$$\mathcal{O}(26^2)$$$ per game. However, it could pass with some optimizations.
We can further convert this whole recursive $$$dp$$$ into iterative $$$dp$$$ to get a time complexity of $$$\mathcal{O}(26)$$$ per game, which can pass very easily within the time limit.
// Author: vrintle (Rahul Verma)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
void solve() {
int n;
string s;
cin >> n >> s;
vi cnt(26), dp(26);
for(int i = 0; i < n; i++) {
cnt[s[i] - 'a']++;
}
int mx = 0;
for(int c = 25; c >= 0; c--) {
dp[c] = cnt[c] - mx;
mx = max(mx, dp[c]);
}
cout << (dp[0] > 0 ? "Alice" : dp[0] < 0 ? "Bob" : "Draw");
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
}
return 0;
}
Author: kaichisaki
There will be around $$$2\sqrt{n}$$$ distinct values of $$$\left\lfloor \frac{n}{i} \right\rfloor$$$.
Similarly,
until,
For remaining $$$ i \in \left[1, \sqrt{n}\right]$$$, we can brute force since all $$$i$$$ in this range will have different $$$\left\lfloor \frac{n}{i} \right\rfloor$$$ value anyway.
So, ans = $$$1^{n - \left\lfloor \frac{n}{2} \right\rfloor} \times 2^{\left\lfloor \frac{n}{2} \right\rfloor - \left\lfloor \frac{n}{3}\right\rfloor} \times . . . \times (\sqrt{n} - 1)^{\left\lfloor \frac{n}{\sqrt{n-1}} \right\rfloor - \left\lfloor \frac{n}{\sqrt{n}} \right\rfloor} \times \left\lfloor \frac{n}{\sqrt{n}} \right\rfloor \times . . . \times\left\lfloor \frac{n}{2} \right\rfloor \times \left\lfloor \frac{n}{1} \right\rfloor $$$
We can use binary exponentiation to find $$$a^b$$$ in $$$O(log(b))$$$ time complexity. Total time complexity would be $$$O(\sqrt{n} log(n))$$$.
#include<bits/stdc++.h>
#define int long long int
using namespace std;
int mod = 1000000007;
int bexp(int x, int n){
if(n == 0) return 1;
if(n == 1) return x;
if(n%2 == 1){
int temp = (bexp(x, (n-1)/2))%mod;
int a = (temp*temp)%mod;
return (a*x)%mod;
}
else{
int temp = (bexp(x, n/2))%mod;
return (temp*temp)%mod;
}
}
void solve(){
int n;
cin>>n;
int ans = 1;
for(int i = 1; i<=n; i++){
int temp = n/i;
int x = n/temp;
ans = (ans*bexp(temp, (x-i+1)))%mod;
i = x;
}
cout<<ans<<endl;
}
int32_t main(){
int t;
cin>>t;
while(t--){
solve();
}
}
Author: 0x4F5F6F
First thing we notice is that $$$2$$$ dimensional data is of no use to us so we will convert it into one dimensional data by taking only the time it will take for each zombie to reach our cell i.e. $$$max(abs(X-x_i),abs(Y-y_i))$$$ (lets call it $$$t1_i$$$) and the time required to kill the zombie with normal bullet i.e. $$$(h_i+d-1)/d$$$ (lets call it $$$t2_i$$$).
Another thing to note is that we will have to kill the zombies that are closer first, so we will sort the zombies in ascending order according to time it will take them to reach us. Now we can maintain a variable $$$T$$$ which will indicate the time that has elapsed, initially it will be zero.
Now we can iterate over all the zombies while doing the following and maintaining a priority for all the zombies that we will kill using normal damage bullet and a variable $$$count$$$ to store the number of high damage bullet used:-
- If you have no time at all to kill the zombie i.e. $$$t1_i \le T$$$.
- If the priority queue is not empty, then we will pop the top element from the priority queue and add that to $$$T$$$ and subtract $$$1$$$ from $$$T$$$ (we take the zombie which took the most normal damage bullet to die so far and kill it using a high damage bullet instead), and add $$$1$$$ to $$$count$$$.
- If the priority queue is empty then we cannot kill all the zombies thus we will break and print $$$-1$$$.
- If you have enough time to kill the zombie with normal bullet before it reaches you i.e. $$$t2_i \le t1_i-T$$$, we will kill the zombie with normal damage bullet for now, while adding the time used to $$$T$$$ and storing this time in the priority queue, then move to next zombie.
- If you don't have enough time to kill the zombies with normal bullet we will compare the current zombie and zombie at the top of the priority queue and whichever will take more normal damage bullet to kill, we will kill with high damage bullet instead, and we will add $$$1$$$ to $$$count$$$.
Finally we will output the count of high damage bullet used i.e. $$$count$$$.
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, m, x, y, d, k;
scanf("%d %d %d %d %d %d", &n, &m, &x, &y, &d, &k);
vector<pair<int, long long>> v(k);
for(int i = 0; i < k; i++) {
int xi, yi;
long long hi;
scanf("%d %d %lld", &xi, &yi, &hi);
v[i] = {max(abs(x - xi), abs(y - yi)), (hi + d - 1) / d};
}
sort(v.begin(), v.end());
priority_queue<long long> pq;
long long c{}, tm{};
for(int i = 0; i < k; i++) {
if(v[i].first <= tm) {
if(pq.size() == 0) {
c = -1;
break;
}else {
tm -= pq.top();
pq.pop();
c++;
}
}
if(v[i].first - tm < v[i].second) {
if(pq.size() == 0 || v[i].second - 1 >= pq.top()) {
tm++;
c++;
continue;
}else {
tm -= pq.top();
pq.pop();
c++;
}
}
tm += v[i].second;
if(v[i].second > 1) {
pq.push(v[i].second - 1);
}
}
printf("%d\n", c);
}
return 0;
}



