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
I think it is obvious here that whenever Bob/Alice pick a character, they will pick up all the indices of that character. So the question boils down to the frequencies of each character.
Let a vector $$$freq$$$ contain frequency of each character in the string.
After the first move : Alice's score = $$$freq['a']$$$; Bob's score = 0;
Now, Bob will choose the first character in a way such that he can maximise his score.
Let $$$score[ch]$$$ contain the maximum score difference Bob can obtain if he picks a character ch as his first character.
It is obvious that score['z'] = freq['z'].
But, score['y'] = freq['z'] — score['z']. This is because if Bob picks 'y', Alice can then pick 'z', and increase her score.
Simultaneously, we can do :
score['y'] = max(score['y'], score['z']);
We did this because now, score['y'] contains the max score difference which can be created by Bob.
So, similarly for each character, we can do :
score[ch] = freq[ch] - score[ch + 1]
score[ch] = max(score[ch], score[ch + 1])
Finally, score['b'] will contain the max score difference which can be created by Bob (since Bob cannot pick 'a').
If score['b'] > freq['a'] : Winner = Bob. If score['b'] < freq['a'] : Winner = Alice. If score['b'] = freq['a'] : It is a draw. Time Complexity = O(n) + O(26)
// 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. $$$\text{max}(\text{abs}(X-x_i),\text{abs}(Y-y_i))$$$ (lets call it $$$t1_i$$$) and the time required to kill the zombie with normal bullet i.e. $$$\frac{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 queue (max heap) 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 subtract that from $$$T$$$ and add $$$1$$$ to $$$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$$$.
As we are initially sorting the array of zombies which will be $$$O(klog(k))$$$ time and later we are iterating over every zombie and doing some operations on priority queue which is also $$$O(klog(k))$$$ time, thus over all time complexity of this solution would be $$$O(klog(k))$$$.
#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;
}



