1409C. Yet Another Array Restoration(等差数列,暴力)

C. Yet Another Array Restoration

这题考虑起来会有很多细节

但是我们注意到 n n n很小,而给出的 x , y x,y x,y一定在等差数列的某个位置

那我们不就枚举 x , y x,y x,y在数列的哪个位置吗?

这样可以算出公差,进而算出等差数列的首项和尾项

假如首项或尾项小于0就不合法,一直取 m i n min min就好了

但是在代码中,我默认公差是大于0的

因为公差小于0是一样的,只不过是公差大于0的数列倒过来而已

#include <bits/stdc++.h>
using namespace std;
int a,b,x,y;
int main()
{
	int t,n; cin >> t;
	while( t-- )
	{
		cin >> n >> x >> y;
		if( x<y )	swap(x,y);
		int ans=1e9,cha=x-y,ss,first;//???
		for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++)//默认大的在后面,也就是公差大于0,其实你完全可以从j=1枚举过来,一样的 
		{
			int len=j-i;
			if( cha%len!=0 )	continue;
			int w=( cha/len );
			int one=( x-(j-1)*w ),last=x+( n-j )*w;//首项和尾项 
			if( one<=0 )	continue;
			if( last>=ans )	continue;
			ans=last,first=one,ss=w;
		} 
		for(int i=1;i<=n;i++)
		{
			cout << first << " ";
			first+=ss;
		}
		cout << endl;
	}
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值