cf1305E——Kuroni and the Score Distribution(构造)

原题链接

思路:

首先容易确定的是,按照(1,2,3,……,n)的方法填充,三元组的数量是最多的。
由于序列满足单调性,所以前面的值一定不会相交。对于(a[k]=k)他贡献的答案为((k-1)/2)
先考虑无解的情况,当补全n个数之后三元组的数量还小于(m)时,一定无解;
如果说按照最优策略构造的三元组的数量等于(m),这正是最好的情况;
但是如果超出(m),应该如何处理呢。假设这个位置的数多了(k)个,这个位置的数每增加(2),答案就是减少(1),所以只需要在这个位置加上(2k)就好了。再考虑后面的数如何构造,很容易想到从大的数开始填,每次固定间隔就可以。
间隔为i+2,个人的理解为取这个间隔保证前面的数相加不会等于后面的数。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const double eps = 1e-8;
const int maxn =1e6+7;
const ll mod=998244353;
const int N = 5e3 + 7;
int n, m, a[N];

int main() {
	cin>>n>>m;
	int t = 0;
	for (int i = 0; i < n; i++) {
		a[i] = i + 1;
		t += i >> 1;
		if (t >= m) {
			a[i] += (t - m) << 1;
			for (int j = n - 1, k = 1e9; j > i; j--, k -=2+i)
				a[j] = k;
			for (int j = 0; j < n; j++)
				cout<<a[j]<<" ";
			return 0;
		}
	}
	cout<<"-1";
	return 0;
}









原文地址:https://www.cnblogs.com/OvOq/p/14750991.html