We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: aaryamannx
To maximize the height of the tower, we wish to pick as many blocks as possible. Thus, we choose every block with a unique height and count the total number of blocks in the array after removing duplicates.
Our answer boils down to the number of unique elements in the array.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int32_t main(){
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
set<ll> v;
while(n--){
ll x;
cin>>x;
v.insert(x);
}
cout<<v.size()<<"\n";
}
return 0;
}
Author: JainWinn
The expression $$$(x$$$ & $$$y)$$$ $$$+$$$ $$$(x \oplus y)$$$ $$$=$$$ $$$n$$$ reduces to $$$(x \ | \ y) = n $$$. So for each set bit in $$$n$$$ , we have $$$3$$$ possibilities:
- set in $$$x$$$ and unset in $$$y$$$
- unset in $$$x$$$ and set in $$$y$$$
- set in both $$$x$$$ and $$$y$$$
So answer is simply $$$3^b$$$ where $$$b$$$ is number of set bits in $$$n$$$ which can be calculated in $$$O(log_2(b))$$$ time and space complexity(recursive stack space included).
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define countbits(x) __builtin_popcount(x)
#define endl '\n'
int binpow(int x, int y)
{
int res = 1;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res * x);
}
y = y >> 1;
x = (x * x);
}
return res;
}
void solve(){
int n;
cin>>n;
cout<<binpow(3,countbits(n))<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: tenzen
This problem was based on 2D Prefix Sums concept. For a 1D prefix sum array we only consider the sums before a particular number and itself, here we will have to do the same for the upper column and the left side values of a particular cell of the prefix sum array. This can be done by: $$$pref[i][j] = pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1] + v[i][j]$$$, where $$$pref$$$ is the prefix sum array and $$$v$$$ is the input 2D array.
For a particular query, where $$$(x1, y1)$$$ are the coordinates of the top left of the query and $$$(x2, y2)$$$ are the coordinates of the bottom right for the query, the answer will consist of $$$pref[x2][y2] - pref[x1-1][y2] - pref[x2][y1-1] + pref[x1-1][y1-1]$$$, we first add the prefix sum of the bottom right cell and then remove the unwanted sums and finally add the prefix sum for the missing portion.
Let the prefix sum for a particular query be $$$s$$$, then the final answer for that query will be $$$2*s$$$ which shall be added for all the queries for comparing with $$$Bulma's$$$ HP.
#include<bits/stdc++.h>
#define int long long int
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n; cin>>n;
vector<vector<int>> v(n+1, vector<int>(n+1, 0));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int x; cin>>x;
v[i+1][j+1] = x;
}
}
vector<vector<int>> pref(n+1, vector<int>(n+1, 0));
for(int i = 1; i <= n; i++){
for(int j = 0; j <= n; j++){
pref[i][j] = pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1] + v[i][j];
}
}
int q; cin>>q;
int tot = 0;
while(q--){
int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2;
x1++; y1++; x2++; y2++;
if(x1>x2) swap(x1, x2);
if(y1>y2) swap(y1, y2);
tot += pref[x2][y2] - pref[x1-1][y2] - pref[x2][y1-1] + pref[x1-1][y1-1];
}
int k; cin>>k;
if(tot*2>k){
cout<<"YES\n";
}
else{
cout<<"NO\n";
}
return 0;
}
Author: pattagobhii
The first observation to make was that only prime numbers out of all the numbers have an odd number of factors. We can simply calculate the number of kids using binary search. If the number of kids is more than the number of gifts, then it is not possible to distribute the gifts. Now our task boils down to dividing the array of gifts into subarrays such that the maximum sum of any subarray is minimized. This is a basic implementation of Binary search on Answers, we can run it on the maximum sum and get to the answer.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define hint __int128_t
#define repp(n) for(int i=0;i<n;i++)
#define double long double
const int mod = 1e9 + 7;
const int mod2 = 998244353 ;
const int mod3 = 7 * 1e18 ;
const double limit = 1e-7;
void yes(){ cout << "YES" << endl;}
void no(){ cout << "NO" << endl;}
void solve(int tc){
int n , k ;
cin>>n>>k ;
int a[n] ;
repp(n) cin>>a[i] ;
int sqs = 1 ;
int l = 1 , h = 1e9 ;
while(l <= h){
int m = (l+h)/2 ;
if(m*m <= k){
sqs = m ;
l = m+1 ;
}
else h = m-1 ;
}
int kids = sqs ;
if(n < kids){
cout<<-1<<endl ;
return ;
}
l = 0 , h = 0 ;
repp(n){
l = max(l , a[i]) ;
h += a[i] ;
}
int ans =0 ;
while(l <= h){
int m = (l+h)/2 ;
int cnt = 0 , tmp = 0 ;
repp(n){
if(tmp+a[i] <= m) tmp += a[i] ;
else{
cnt++ ;
tmp = a[i] ;
}
}
cnt++ ;
if(cnt <= kids){
h = m-1 ;
ans = m ;
}
else l= m+1 ;
}
cout<<ans<<endl ;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// cout<<fixed<<setprecision(6);
bool multi = 1;
int tc = 1;
// build() ;
if(multi) cin >> tc;
for (int t = 1; t <= tc; t++) {
solve(t);
}
}
Author: JainWinn
Considering $$$0-$$$ based indexing , for each index $$$i$$$ $$$(1 \leq i \leq n-2 )$$$ we can find out number of ordered pairs $$$(x,y)$$$ for which $$$a_i$$$ is the V-Score . We find for each $$$i$$$, the index $$$l_i$$$ such that $$$a_{l_i}$$$ is the previous smaller or equal element and $$$r_i$$$ such that $$$a_{r_i}$$$ is the next smaller element of $$$a_i$$$ . This can easily be done using stack in $$$O(n)$$$ time and space complexity.
Number of ordered pairs for which $$$a_i$$$ is the V-Score is given by : $$$(i-l_i) \times (r_i-i)$$$ . So the answer is given by :
$$$\sum_{i=1}^{n-2}V_i$$$ where $$$V_i=(i-l_i) \times (r_i-i) \times a_i$$$ .
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define endl '\n'
//Previous smaller or equal Element
vector<int> prevSmaller(vi &arr)
{
stack<int> s;
s.push(0);
vi prevg(arr.size(),0);
for (int i = 1; i < arr.size(); i++) {
while (s.empty() == false && arr[s.top()] >= arr[i]){
s.pop();
}
if(!s.empty()){
prevg[i]=s.top();
}
s.push(i);
}
return prevg;
}
//Next smaller Element
vector<int> nextSmaller(vi &arr)
{
stack<int> s;
s.push(arr.size()-1);
vi nextg(arr.size(),arr.size()-1);
for (int i = arr.size()-2 ; i>=0 ; i--) {
while (s.empty() == false && arr[s.top()] > arr[i]){
s.pop();
}
if(!s.empty()){
nextg[i]=s.top();
}
s.push(i);
}
return nextg;
}
void solve(){
int n;
cin>>n;
vi a(n,0);
for(int i=0 ; i<n ; i++){
cin>>a[i];
}
vi left=prevSmaller(a);
vi right=nextSmaller(a);
int ans=0;
for(int i=1 ; i<n-1 ;i++){
ans+=(right[i]-i)*(i-left[i])*a[i];
}
cout<<ans<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: tt12
In how much time can a racer $$$A$$$ overtake a racer $$$B$$$?
Think about how we can calculate the total overtakes by a single racer against the opposite team in less than $$$O(n)$$$.
The time in which racer $$$A$$$ would overtake a racer $$$B$$$ one time would be $$$t = p / v_A - v_B$$$.
Total overtakes $$$(N) = d / t = (d / p) * (v_A - v_B)$$$.
Since $$$d$$$ is an integral multiple of $$$p$$$, the total number of overtakes would always be an integer. Also, the number of overtakes will be reduced by $$$1$$$ since the formula for $$$t$$$ actually calculates the time after which racer $$$A$$$ will again catch up with $$$B$$$. A small time $$$dT \gt 0$$$ would be required after each $$$t$$$ for a valid overtake to occur. Since this cannot occur after the last second, the last overtake would noṭ happen and racer $$$A$$$ would just reach racer $$$B$$$.
So, total overtakes = $$$N - 1$$$.
These overtakes can be calculated for all racers in $$$O(n^2)$$$, which is too slow.
We can deduce that a certain racer would always overtake a set of racers of the opposing racing team which would form a prefix array. With the help of the two-pointer method, we can optimise this solution to $$$O(n)$$$.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 998244353
void solve(){
ll n, p, d;
cin >> n >> p >> d;
vector<ll> a(n), b(n), pref_a(n), pref_b(n);
for(int i=0; i<n; i++){
cin >> a[i];
if(i) pref_a[i] = (pref_a[i - 1] + a[i]) % mod;
else pref_a[i] = a[i];
}
for(int i=0; i<n; i++){
cin >> b[i];
if(i) pref_b[i] = (pref_b[i - 1] + b[i]) % mod;
else pref_b[i] = b[i];
}
ll l = n - 1;
ll a_points = 0, b_points = 0;
ll k = d / p;
for(int i=n-1; i>=0; i--){
while(l > 0 && a[i] <= b[l]){
l--;
}
if(a[i] > b[l]){
ll count = l + 1;
a_points = (a_points + (((((count * a[i]) % mod - pref_b[l] + mod) % mod) * k) % mod - count + mod) % mod) % mod;
}
}
l = n - 1;
for(int i=n-1; i>=0; i--){
while(l > 0 && b[i] <= a[l]){
l--;
}
if(b[i] > a[l]){
ll count = l + 1;
b_points = (b_points + (((((count * b[i]) % mod - pref_a[l] + mod) % mod) * k) % mod - count + mod) % mod) % mod;
}
}
cout << a_points << ' ' << b_points << '\n';
}
int main(){
fastio();
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}




