昨天的div3 C题,说来惭愧,居然现在才有那么一点思路,还都是看的其他大佬们的提交的ac代码和B站大佬的直播解题视频才明白的具体实现细节,其实解法昨天晚上就已经想到了,先看题干吧。
C. Different Differences
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard 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
7
5 9
4 12
3 3
3 4
4 4
4 6
8 11
output
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
这道题我简单的翻译一下吧,题干的意思就是说,给出你两个输入变量,n和k,n为你需要输出的数组的长度,k为从1到k的所有的整数集,题目要求你在1到k所有的整数中,任意选出一组满足上述要求,即单调递增且两个数之间相减之后的间隔最大的一组数, 看到任意和最大,我们不难想出,其实题目的答案可以与测试样例的答案不完全一样,再看最大的一组,我们就可以使用贪心的思想,首先是x然后是x+1再然后是x+3以此类推,每一个数和每一个数之间的差值构成的是一个满足1,2,3,4......的等差数列。
那么解法已经知道了,具体的代码实现的时候我却遇到了瓶颈,因为我当时没有理清楚自变量i,和n,k之间的关系,所有就早早的下播了,其实这道题并非完全不可做,先看一段我从cf上找到的一个妙妙的打表做法,我觉得只要能ac,不管什么做法都是可以的,只不过当时是我比较懒惰了,所以没有选择这种打法
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,k;
cin>>t;
int a[41];
while(t--)
{
cin>>k>>n;
int f=1;
for(int i=1;i<=k;i++) a[i]=i;
if(k<n)
{
if(k==3&&n>=5) a[1]=1,a[2]=2,a[3]=5,f=0;
if(k==4&&n>=8) a[1]=1,a[2]=2,a[3]=4,a[4]=7,f=0;
if(k==5&&n>=12) a[1]=1,a[2]=2,a[3]=4,a[4]=7,a[5]=11,f=0;
if(k==6&&n>=17) a[1]=1,a[2]=2,a[3]=4,a[4]=7,a[5]=11,a[6]=16,f=0;
if(k==7&&n>=23) a[1]=1,a[2]=2,a[3]=4,a[4]=7,a[5]=11,a[6]=16,a[7]=22,f=0;
if(k==8&&n>=30) a[1]=1,a[2]=2,a[3]=4,a[4]=7,a[5]=11,a[6]=16,a[7]=22,a[8]=29,f=0;
if(k==9&&n>=38) a[1]=1,a[2]=2,a[3]=4,a[4]=7,a[5]=11,a[6]=16,a[7]=22,a[8]=29,a[9]=37,f=0;
if(f)
{
int add=n-k,plus=0;
for(int i=k;i>=1&&add>0;i--)
{
a[i]+=add;
plus++;
add-=plus;
}
}
}
for(int i=1;i<=k;i++) cout<<a[i]<<" ";
cout<<endl;
}
}
一种非常妙的解题思路,但是cf还是少这种做法比较好,当然在acm或者其他比赛里面,能够靠打表拿到分其实也是一种本事啊
那么我们分析一下正常的做法,我们观察测试样例会发现,当k和n相等的时候,直接输出全部的从1到k的数字就可以了,当k,n不相等的时候,就需要判断了,以最后一个样例为例子,8 11输出为:1 2 3 5 6 7 8 11,而如果输入样例为2 11 的话那么输出样例就会是1 2即可,不难看出,k越大的时候,他的数组之间的差值越小,即越紧密,核心代码其实只有一段,就是等差的那一段,剩下的其实都是一些简单的if else判断那么我们看代码
#include <bits/stdc++.h>
using namespace std;
signed main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<int> ans;
int now = 0;
for (int i = 1; i <= n; i++) {
int nxt = now + i;
if (m - nxt < (n - i)) {
for (int j = i; j <= n; j++) {
ans.push_back(now + j - i + 1);
}
break;
}
else
ans.push_back(nxt);
now = nxt;
}
for(vector<int>::iterator it=ans.begin();it!=ans.end();it++){cout<<*it<<" ";}
cout << endl;
}
return 0;
}
不禁感慨啊,绞尽脑汁想了一天的代码,别人五六分钟就敲出来了,这算是结论题呢还是经验使然呢,我做的题少也不好下结论,总之自己和大佬之间的差距还是非常的大的,希望可以通过自己的不懈努力来追赶上各位大佬们