We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: BitWizz
Observe the sample test cases.
The minimum number of boxes required is the maximum number of occurrences of a colour. If we use less boxes, then some box will have two or more stones of the same colour (According to Pigeonhole Principle).
We do not need to use more boxes either since stones of different colours can be present in one box.
Time Complexity : $$$O(N + maxA_i)$$$
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl "\n"
void solve(){
int n;
cin >> n;
map<int, int> cnt;
for(int i = 0; i < n; i++){
int x;
cin >> x;
cnt[x]++;
}
int ans = 0;
for(auto &it : cnt){
ans = max(ans, it.second);
}
cout << ans << endl;
}
signed main(){
int tc;
cin >> tc;
while(tc--) solve();
return 0;
}
Author: BitWizz
The number of shakes that can be bought from a particular stall is atmost $$$a_i$$$. Out of these, Maddy can only buy upto $$$n/q_i$$$ shakes. So, finally the number of shakes that can be bought from a particular stall is $$$min(a_i, n/q_i)$$$. Now, just maximise this quantity over all the stalls to get the answer.
The overall Time Complexity would be : $$$O(m)$$$
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl "\n"
void solve(){
int n, m;
cin >> n >> m;
int ans = 0;
int idx = -1;
for(int i = 0; i < m; i++){
int a, q;
cin >> a >> q;
int x = min(n / q, a);
if(x > ans){
ans = min(n / q, a);
idx = i + 1;
}
}
cout << idx << endl;
}
signed main(){
int tc;
cin >> tc;
while(tc--) solve();
return 0;
}
Author: AdityaDargan
Try to find the range of positions that can be attacked by the Irish Army.
The Irish soldiers can attack German soldiers in the positions from $$$l−k$$$ to $$$l+n−1+k$$$. We need to select soldiers with positive strength in this range, and we can select a maximum of $$$n$$$ opponents.
Also, it doesn't matter which opponents we select from this range, as there will always be a possible arrangement for the Irish soldiers to attack the selected ones.
Time Complexity : $$$O(m + mlogm)$$$
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--){
int k,l,n,m,r;
cin>>k;
cin>>l>>n;
cin>>r>>m;
vector<int> M(m);
int sum = 0;
for(int i=0;i<m;i++){
cin>>M[i];
sum += M[i];
}
int range_start = l-k;
int range_end = l+n-1+k;
vector<int> ans;
for(int i=0;i<m;i++){
int pos = r+i;
if(pos>=range_start && pos<=range_end){
if(M[i]>0)ans.push_back(M[i]);
}
}
sort(ans.begin(),ans.end());
int sz = ans.size();
for(int i=0;i<min(n,sz);i++){
sum -= ans.back();
ans.pop_back();
}
cout<<sum<<endl;
}
}
E – Geometric Progression Subsequence
Author: JainWinn
Fixing one among $$$(l,r)$$$, could you find the other one?
We can utilize dynamic programming to keep track of the rightmost indices for elements $$$(1 \leq A_i \leq 30)$$$.This will help us find the rightmost $$$l$$$ for each $$$r$$$ by fixing $$$r$$$ and iterating over all geometric progressions where $$$A_k = A_r$$$ and finding $$$l_{max}[r]= max(l_{max}[r-1]$$$, all $$$i$$$ where $$$A_i \times A_k = A_j \times A_j )$$$.
Adding $$$(l_{max}[r]+1)$$$ (considering $$$0-$$$based indexing) to the answer for each $$$r$$$ gives the desired result.
The overall Time Complexity would be : $$$O(30 N)$$$
For better understanding, refer the implementation below.
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#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 countbits(x) __builtin_popcount(x)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
int mod = 1e9+7;
//int mod = 998244353;
void solve(){
int n, ans = 0;
cin >> n;
vector<int> vec(n);
for (auto &ele : vec){
cin >> ele;
}
//stores rightmost index for each element(1 to 30 inclusive) while iterating for each index
vector<vector<int>> closest;
//stores rightmost index for each element(1 to 30 inclusive) while iterating for last index
vector<int> prevdata(31, -1);
//stores max left pointer(l) for each r.
vector<int> maxl(n, -1);
for (int r = 0; r < n; r++)
{
closest.push_back(prevdata);
//finding maxl for current r by checking all possible Geometric Progressions(size 3) ending at r.
//It could also be the maxl of previous index(i.e (r-1)).
int maxind = -1;
if (r > 1)
{
maxind = maxl[r - 1];
for (int mid = 1; mid < 31; mid++)
{
if ((mid * mid) % vec[r] == 0 && (mid * mid) / vec[r] < 31 && closest[r][mid] != -1)
{
int midind = closest[r][mid];
int first = (mid * mid) / vec[r];
maxind = max(maxind, closest[midind][first]);
}
}
}
//there would be (maxind+1) l,r pairs, so adding this to answer
ans = ans + (maxind + 1);
//storing maxl for r
maxl[r] = maxind;
//updating rightmost index for vec[r]
prevdata[vec[r]] = r;
}
cout << ans << '\n';
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
while(t--){
solve();
}
return 0;
}
Author: -tsukasa-
Expand $$$x$$$ into $$$l-r+1$$$ and rearrange the elements to make something meaningful.
Think in terms of prefix and try to apply Dynamic Programming.
The equation given in operation $$$1$$$ can also be written as $$$r \times (a_1+a_2+...+a_r) - (l-1) \times (a_1+a_2+...a_{l-1})$$$ which is nothing but $$$r \times prefix[r] - (l-1) \times prefix[l-1]$$$
Now lets try to solve the subproblem for some arbitrary index $$$r$$$.We have $$$2$$$ options here:
$$$1.$$$Directly add the element to the total points making the total points $$$a_r + maxpoints[r-1]$$$.
$$$2.$$$Make $$$a_r$$$ part of a segment.In this case the maximum points that one can obtain will be the maximum possible value of $$$r \times prefix[r]-(l-1) \times prefix[l-1] + maxpoints[l-1]$$$.
We will take the max of both the possibilities for the value of $$$maxpoints[r]$$$
We will maintain minimum value of $$$i \times prefix[i] + maxpoints[i]$$$ while traversing the array to optimize the time complexity.
The overall Time Complexity would be : $$$O(N)$$$
#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> vec(n + 1, 0), dp(n + 1, 0);
for (int i = 1; i <= n; i++)
cin >> vec[i];
int prefsum = 0, dpmax = 0;
for (int i = 1; i <= n; i++)
{
prefsum += vec[i];
dp[i] = max(i * prefsum + dpmax, dp[i - 1] + vec[i]);
dpmax = max(dpmax, dp[i] - i * prefsum);
}
cout << max(dp[n], 0ll) << '\n';
}
}
Author: JainWinn
Think of Segment Tree.
The problem can be solved by using Segment Tree of multiset. The type $$$-1$$$ operation of adding new items and type $$$-2$$$ operation of cancelling one of the previous type $$$-1$$$ operation or in other words removing item can be thought of as adding and removing elements in multisets in $$$O(log N)$$$ time. Similarly for type $$$-3$$$ operation, the most expensive item $$$A_i$$$ can be found in similar fashion. Overall time taken by multiset over $$$Q$$$ operations/queries is $$$O(log Q)$$$.
The overall Time Complexity would be : $$$O((N+Q) logN logQ)$$$
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#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 countbits(x) __builtin_popcount(x)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
int mod = 1e9+7;
//int mod = 998244353;
class SGTree{
vector<multiset<int>> seg;
public :
SGTree(int n){
seg.resize(4*n+1);
}
void add(int idx,int low,int high,int l,int r,int val){
//no overlap
if(l>high || r<low || low>high){
return;
}
//complete overlap
if(low>=l && high<=r){
seg[idx].insert(val);
return;
}
int mid=low+((high-low)/2);
add(2*idx+1,low,mid,l,r,val);
add(2*idx+2,mid+1,high,l,r,val);
}
void rmv(int idx,int low,int high,int l,int r,int val){
//no overlap
if(l>high || r<low || low>high){
return;
}
//complete overlap
if(low>=l && high<=r){
seg[idx].erase(seg[idx].find(val));
return ;
}
int mid=low+((high-low)/2);
rmv(2*idx+1,low,mid,l,r,val);
rmv(2*idx+2,mid+1,high,l,r,val);
}
void query(int idx,int low,int high,int id,int &ans){
//no overlap
if(id>high || id<low || low>high ){
return;
}
//complete overlap
if(id>=low && id<=high && (!seg[idx].empty())){
ans=max(ans,*(seg[idx].rbegin()));
}
if(low==high){
return;
}
int mid=low+((high-low)/2);
query(2*idx+1,low,mid,id,ans);
query(2*idx+2,mid+1,high,id,ans);
}
};
void solve(){
int n;
cin>>n;
vector<int> a(n,0);
for(int i=0 ; i<n ; i++){
cin>>a[i];
}
SGTree seg(n);
for(int i=0 ; i<n ; i++){
seg.add(0,0,n-1,i,i,a[i]);
}
int q;
cin>>q;
vector<vector<int>> v(q+1);
for(int i=1 ; i<=q ; i++){
int ch;
cin>>ch;
if(ch==1){
int l,r,x;
cin>>l>>r>>x;
l--;
r--;
v[i]={l,r,x};
seg.add(0,0,n-1,l,r,x);
}
else if(ch==2){
int j;
cin>>j;
seg.rmv(0,0,n-1,v[j][0],v[j][1],v[j][2]);
}
else{
int id;
cin>>id;
id--;
int ans=0;
seg.query(0,0,n-1,id,ans);
cout<<ans<<endl;
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
while(t--){
solve();
}
return 0;
}



