1486A - Shifting Stacks - CodeForces Solution


greedy implementation *900

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    curr_sum = 0
    is_possible = True
    for i, x in enumerate(a):
        curr_sum += x
        if 2 * curr_sum < i * (i + 1):
            is_possible = False
            break
    if is_possible:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <iomanip>
#include <climits>

using namespace std;

#define ll long long int
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vch vector<char>
#define vvch vector<vector<char>>
#define vvll vector<vector<ll>>
#define mod 1000000007

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

void rvec(vll &arr, ll n)
{
    for (ll i = 0; i < n; i++)
        cin >> arr[i];
}

void pvec(vll &arr, ll n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}

ll power(ll a, ll b)
{
    if (b == 0)
        return 1;
    if (b == 1)
        return a;
    if (b % 2 == 0)
        return power(a, b / 2) * power(a, b / 2);
    else
        return a * power(a, b / 2) * power(a, b / 2);
}
unsigned ll factorial(unsigned ll n)
{
    if (n == 0 || n == 1)
        return 1;
    return (n * factorial(n - 1)) % mod;
}

int main()
{

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);

    ll t = 1;
    cin >> t;

    while (t--)
    {
        ll n, val = 0;
        bool flag = false;
        cin >> n;
        vll arr(n);
        rvec(arr, n);
        ll need = 0, sum = 0;
        // TRY TO MAKE ARRAY LIKE 0,1,2,3,4,5....
        // THUS FOR EVERY INDEX THE SUM OF BLOCKS TILL THEN SHOULD BE GREATER THAN WE IDEAL WANT
        for (ll i = 0; i < n; i++)
        {
            need += i;
            sum += arr[i];
            if (sum < need)
            {
                flag = true;
                cout << "NO\n";
                break;
            }
        }
        if (!flag)
            cout << "YES\n";
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String