[problem:471619A]
Do we really need to create stack ?
What would the smallest element in the first stack ?
every stack can be reduced to any value we want, but how much can it be increased ?
We can simple use a vector to store the sizes of stack, the minimum ascending order series we can form is 0,1,2,3,4....n We can keep a buffer for extra elements, that means set a[0] = 0 and add the remaining value to the buffer. a[i] -= i; and buffer += a[i] — i;
if at any point our buffer becomes negative that will mean that particular sequence till that point isn't possible, and since that is the minimal series that means it isn't possible to form a valid sequence for those set of stacks.
// Aur Bhai Dekhne aagaye ;)
// Author: Abhijit Mishra
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
#define nl cout << "\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int main()
{
fast;
ll t = 1;
cin >> t;
while (t--) // each testcase
{
ll n;
cin >> n;
vi a(n);
for (int i = 0; i < n; i++) { cin >> a[i]; }
ll buffer = 0;
int i = 0;
while (i != n)
{
if (a[i] >= i)
{
buffer += a[i] - i;
}
else
{
ll x = i - a[i];
a[i] += x;
buffer -= x;
}
if (buffer < 0)
{
break;
}
i++;
}
cout << (i == n ? "YES" : "NO");
nl;
}
return 0;
}
[problem:471619B]
Find the max distance between 2 different candies, then that range divided by 2 is our answer.
An edge case exist!?
Simply sort the array remove duplicates, now we find the differences between every 2 consequtive elements, with this we have found the max range, now consider this and the distance between the starting point to first candy and ending point to last candy the max of these 3 values will be our result.
// Aur Bhai Dekhne aagaye ;)
// Author: Abhijit Mishra
#include <bits/stdc++.h>
using namespace std;
#define nl cout << "\n"
#define all(x) x.begin(), x.end()
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
typedef long long int ll;
typedef vector<ll> vi;
int main()
{
fast;
ll t = 1;
// cin >> t;
while (t--)
{
ll n, l;
cin >> n >> l;
vector<ll> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(all(a));
a.resize(unique(a.begin(), a.end()) - a.begin());
if (a.size() == 1)
{
ll x = a[0];
cout << max(x - 0, l - x);
return 0;
}
vector<ll> b;
for (int i = 0; i < a.size(); i++)
{
if (i != 0)
{
b.push_back(a[i] - a[i - 1]);
}
}
sort(all(b));
double ans = (double)b[b.size() - 1] / 2;
double endpoints = max(a[0] - 0, l - a[a.size() - 1]);
cout << setprecision(14);
cout << max(endpoints, ans) << endl;
}
return 0;
}
[problem:471619C]
Open your eyes and observe.
Think in terms of bits in binary format.
// Aur Bhai Dekhne aagaye ;)
// Author: Abhijit Mishra
#include <bits/stdc++.h>
using namespace std;
#define nl cout << "\n"
#define all(x) x.begin(), x.end()
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
typedef long long int ll;
typedef vector<ll> vi;
int main()
{
fast;
ll x, y;
cin >> x >> y;
cout << 1 + __builtin_ctzll(y);
return 0;
}