C. Different Differences
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
An array aa consisting of kk integers is strictly increasing if a1<a2<⋯<aka1<a2<⋯<ak. For example, the arrays [1,3,5][1,3,5], [1,2,3,4][1,2,3,4], [3,5,6][3,5,6] are strictly increasing; the arrays [2,2][2,2], [3,7,5][3,7,5], [7,4,3][7,4,3], [1,2,2,3][1,2,2,3] are not.
For a strictly increasing array aa of kk elements, let's denote the characteristic as the number of different elements in the array [a2−a1,a3−a2,…,ak−ak−1][a2−a1,a3−a2,…,ak−ak−1]. For example, the characteristic of the array [1,3,4,7,8][1,3,4,7,8] is 33 since the array [2,1,3,1][2,1,3,1] contains 33 different elements: 22, 11 and 33.
You are given two integers kk and nn (k≤nk≤n). Construct an increasing array of kk integers from 11 to nn with maximum possible characteristic.
Input
The first line contains one integer tt (1≤t≤8191≤t≤819) — the number of test cases.
Each test case consists of one line containing two integers kk and nn (2≤k≤n≤402≤k≤n≤40).
Output
For each test case, print kk integers — the elements of the strictly increasing array aa with the maximum possible characteristic. If there are multiple answers, print any of them.
Example
input
Copy
75 94 123 33 44 44 68 11
output
Copy
1 3 4 7 8
2 4 7 12
1 2 3
1 3 4
1 2 3 4
2 4 5 6
1 2 3 5 6 7 8 11
题意就是说给你k和n,k是要求构造出的数组的长度,n是构造出的数组中最大的数不能超过n,题目要求构造出的数组满足
1,是严格的单调递增
2,数组中相邻两个数的差的值不相同的数量最多。比如k=5,n=9时,输出1 3 4 7 8,那么差值为
2 1 3 1,不同的值为3。
思路:我们可以采用贪心的策略,使得差值为1 2 3 4...,构造一个形如1 2 4 7 11..这样的数组,但是光这样构造难免会因为k的长度一直累加下去最大值会超过n,无法将k完全填充,而剩下来的数无法保证严格递增,那么该怎么办呢。
这时我们发现题目中数字在数组中位置的值是有一个上限的,也就是说每个位置都有一个最大的值,超过这个值就不能构造下去了。比如比如k=5,n=9时,可以得到这样的数组5 6 7 8 9,这样是满足严格递增,但是如果数组中第一个值为6,那么会构造出6 7 8 9 10,显然是非法的,也就是该数组第一位的值不能大于5,同理第二位的值不能大于6.,如果大于了,就直接把这个位置的值赋于最大值,因为此时最大的特征值已经构造完毕,后面随便怎么处理。
此时得到的约束条件能帮助我们构造出数组了。
代码如下。
#include<iostream>
#include<set>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
using namespace std;
const int manx = 2e5 + 10;
typedef long long ll;
int a[manx];
int b[manx];
void solve()
{
int n, k;
cin >> k>> n;
int t = 1, cnt = 1;
cout << t << " ";
for (int i = 2; i <= k; i++)
{
t += cnt;
cnt++;
if (t > n - k + i)
{
t = n - k + i;//该位置的最大值
cout << t << ' ';
}
else
{
cout << t << " ";
}
}
cout << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
}