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 $$$(n*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();
}
}




