URAL - 2065 Different Sums (思维题)

题意:

给n和k,让你用不小于 k 个不同的数字构成一个长度为n的序列,使得序列中不同的区间和的数目最小。

n,k<=500

k-1个数填一些数字的一正一负,这样有些区间和为0。

剩下的都填0。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

#define maxn 100010


int main()
{
    int n, m;
    scanf("%d%d", &n, &m);

    int k = 0, a[maxn];
    memset(a, 0, sizeof(a));

    for (int i = 1; i <= m-1; i++)
    {
        if (k < 0) k = -k;
            else k = -k - 1;
        a[i] = k;
    }

    for (int i = 1; i < n; i++)
        printf("%d ", a[i]);
    printf("%d\n", a[n]);
}

猜你喜欢

转载自www.cnblogs.com/ruthank/p/9359747.html