Cattle-off practice match 51 ** ** Number of E- columns (half, greedy, construction)

Cattle practice session off 51 the number of columns E-

Links: https://ac.nowcoder.com/acm/contest/1083/E Source: Cattle-off network

Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 32768K, other languages 65536k
Special Judge, the IO 64bit the Format: LLD%

Title Description

Joe has a length of integer n columns, which initially all values ​​are 0, Joe needs to fill a positive integer greater than 0 in each position 1 ... n to obtain a new number of columns, and this the number of columns the number of all does not exceed m, Joe this number will have a likeability column, Joe likeability this series to satisfy 2 <= i <= n and a [i] = a [i-1] + the number of i 1. Now given n, m, please fill in the number you develop a plan to maximize the degree of love Missy number of columns. There are several possible solutions, you only need to output either.

Enter a description:

第一行两个整数n,m。1<=n<=1e5,n<=m<=1e9。

Output Description:

一行n个整数,表示位置1…n填的数。

Example 1

Entry

copy

5 9

Export

copy

1 2 1 2 3

Explanation

小乔对数列的喜爱度为3,没有能使喜爱度大于3的方案了,此外1 2 3 1 2也是一个合法的方案

Ideas:

(Copied from the official explanations)

For a number of columns, if it is a sub-series a [i], a [i + 1] ... a [j-1], a [j] satisfy a [k] = a [k -1] +1 (i +1 <= k <= j) , while also satisfying i-1 <1 or a [i]! = a [ i-1] + 1, j + 1> n , or a [j + 1]! = a [j] +1, then we call such as the number of sub-paragraph. We found that if there is a sum of the number of segments such column, the column number of Joe for likeability is n-sum. Because the number of columns is the number of free fill, so that with each paragraph the a [i] = 1 is optimal, a [i], a [ i + 1] ... a [j-1], a [ j] obviously. Thus, for each section, if this is the length of s, the method according to the optimal number of refills, the sum of this section is s * (s + 1) / 2.
So the question is converted to:
the number of columns is divided into n segments sum, it requires a minimum of sum, and the sum of all the segments <= m.
Provided that the sum of the minimum length of mn length, the maximum length of lengths mx, can be found if we know the sum, then the sum of the minimum sum to make the segment, it is necessary that as close mn and mx , because if the mx-mn> = 2, then the last element of segment mx mx values, the last element of segment mn mn value, then we make the length of segment mx-1, plus a length mn. Then the change in the sum is the sum = sum -mx + mn + 1. Since mx-mn> = 2, so that mx> mn + 1.
So if that sum, the optimal strategy is to be divided equally among segments of length.
The method of sharing is, first, the length of each section is n / sum, a uniform remaining n% sum is assigned to the segment n% sum up.
Then there are n% sum length of segments is n / sum + 1, length of segment nn% sum of n / sum.
For the sum, we bipartite answer, since the answer is monotone, the more segments, the sum can be made smaller.
The answer calculated sum by half the number of columns can be re-constructed. The time complexity of O (logn + n)

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
ll m;
bool check(ll mid)
{
    // cout << mid << endl;
    ll s = n / mid;
    return s * (s + 1) / 2ll * (mid-n%mid) + (s + 1ll) * (s + 2ll) / 2ll * (n % mid) <= m;
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin >> n >> m;
    ll l = 1ll;
    ll r = n;
    ll mid;
    ll ans;
    while (l <= r)
    {
        mid = (l + r) >> 1;
        if (check(mid))
        {
            ans = mid;
            r = mid - 1;
        } else
        {
            l = mid + 1;
        }
    }
    for (int i = 1; i <= n % ans; ++i)
    {
        for (int j = 1; j <= n / ans + 1; ++j)
        {
            cout << j << " ";
        }
    }

    for (int i = 1; i <= ans - n % ans; ++i)
    {
        for (int j = 1; j <= n / ans; ++j)
        {
            cout << j << " ";
        }
    }
    cout << endl;


    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11789645.html