0
点赞
收藏
分享

微信扫一扫

C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)

原题链接: ​​http://codeforces.com/contest/1409/problems​​

C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_贪心算法
测试样例

input
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22
output
1 49
20 40 30 50 10
26 32 20 38 44 50
8 23 18 13 3
1 10 13 4 19 22 25 16 7

题意: 给定数组元素的个数C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_贪心算法_02和数组中的两个元素C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_ios_03C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_ios_04,又知道这个数列排序之后为等差数列,要你找到使得C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_贪心算法_05最小的等差数列。

解题思路: 今天的div3思维题比较少,好多都是贪心算法或者动态规划。这道题也是一道典型的贪心算法,我被这道题卡了挺长时间,要注意很多细节。OK,我们开始。

首先我们要知道一个固定的信息就是数组元素必须要有C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_贪心算法_02个,还有就是C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_ios_03C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_ios_04都在数组中。这里我们要利用到的一个重要解题点就是数列为等差数列,那么我们可以设公差为C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_贪心算法_09,由性质可得C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_#define_10必定是C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_贪心算法_09的倍数,那么我们不就可以枚举C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_等差数列_12了吗?因为C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_ios_13都很小,这完全不用担心超时。知道了这些我们现在就要知道这个核心了,怎么贪? 我们是想要让数列中最大值越小越好不是吗?那么我们是不是要利用C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_#define_14之前的元素作为等差数列中的值,使得最大值变小一点。 OK,知道了这些信息,我们解题就so easy了。值得注意的点:我们要判断在公差为d 时,C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_ios_15的元素小于等于C. Yet Another Array Restoration(贪心算法)Codeforces Round #667 (Div. 3)_ios_16。具体看代码,我贴了详细注释。

AC代码

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int a[maxn];
int n;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
int t;
int x,y;
while(cin>>t){
while(t--){
cin>>n>>x>>y;
if(n>=y){
rep(i,1,n)cout<<i<<" ";
cout<<endl;
continue;
}
int temp=y-x;
int minn=inf,d,result,start; //输出一个等差数列我们必然要知道的就是a1和公差d。
//这里的result变量是为了存储当公差为d时的最优的最大值,minn则是存储最优解。
for(int i=temp;i>=1;i--){ //枚举公差d
//接下来的这两个if语句都是为了判断公差d可不可行。
if((y-x)%i!=0)continue;
if(((y-x)/i+1)>n)continue;
//接下来则是获取在x之前能填充多少个符合的值数量。
int temp1=min((x-1)/i,n-((y-x)/i+1));
result=x+(n-temp1)*i;//获取等差数列中的最大值。
if(minn>result){
d=i; //记录当前最优公差。
minn=result;
start=x-(temp1)*i;//记录a1.
}
}
rep(i,1,n){
cout<<start<<" ";
start+=d;
}
cout<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论