D. Doremy‘s Connecting Plan Codeforces Round 906 (Div. 2)

Problem - D - Codeforces

The general idea of ​​the question: There is an array a of length n, and a graph of n points. The number corresponds to the number of the array. There are no edges initially. If the sum of a[i] in the current connected block + a certain point a[ j]>=A point i*a certain point j*c in the connected block, then i and j can be connected. Is it possible to make all points in a connected block?

2<=n<=2e5;0<=a[i]<=1e12

Idea: Let an existing connected block be s. We want to find a point j connected to s. Then we must choose the point i with the smallest number in the connected block to minimize i*j*c. If within s and +a[j]>=i*j*c, i and j can be connected. Since i*j*c is already less than or equal to the sum of the current connected blocks, then for all numbers i*j*c less than j Must be less than or equal to.

So since we want to choose the smallest i, then we let a[1] be the smallest connected block, because anyway, if we want to connect all the points, we have to connect 1, so it is better to start connecting from 1, so that we can traverse from 1 to n, check the operator Does not meet the connectivity conditions. If it meets the conditions at n, then it can be connected.

#include <bits/stdc++.h>
//#include<__msvc_all_public_headers.hpp>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
const ll MOD = 1e9 + 7;
ll a[N];
int n;
void solve()
{
    ll c;
    cin >> n;
    cin >> c;
    for (ll i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int ma = 1;//最大的能联通的位置
    ll sum = a[1];//前缀和
    ll nsum = a[1];//当前连通块的和
    for (ll i = 2; i <= n; i++)
    {
        sum += a[i];
        if (nsum + a[i] >= i * c)
        {//满足连通条件
            nsum = sum;
            ma = i;
        }
    }
    cout << (ma == n ? "YES" : "NO") << '\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    
}

Guess you like

Origin blog.csdn.net/ashbringer233/article/details/134179758