设为首页 收藏本站
查看: 1179|回复: 0

[经验分享] codechef Chef and sequence

[复制链接]

尚未签到

发表于 2015-11-26 07:05:13 | 显示全部楼层 |阅读模式
Problem Description
  You are given an array that consists of n integer numbers. You have to change at most K elements of this array, so that the resulting array will be a arithmetic progression. From all the possible arithmetic progressions, you
should choose most beautiful.

You can uniquely define the arithmetic progression by two numbers a0 and d - the first element of the given progression and the step that defines next element. (ai = a0+i * d). The progression A(a0 , d0) is more beautiful than the progression B(b0, d1) iff
(a0 < b0 or (a0 = b0 and d0 < d1))



Input

The first line contains two integers N and K denoting the number of elements in the given array and the number of elements that you can change

The second line contains N space-separated integers A1, A2, ..., AN denoting the given array.
  

题目大意:

  给你一个长度为n的序列,要你改动至多K个数,使之变为等差数列,我们用一个式子描述改变后的数列:ai=a0&#43;i*d。要求改动后使a0尽量小,a0相同时d尽量小。
  输出改动后的数列。

  

Output

Output a single line containing the resulting array with at most K changes. Mind that among all the arithmetic sequences you have to choose the most beautiful.

In the given test data, it is always possible to recover at least one arithmetic progression under the constraints of the problem.


Constraints

2 ≤ N ≤ 100000

0 ≤ K ≤ min(10, N-2)

-109 ≤ Ai ≤ 109


Example

Input:

4 2

1 2 1 4



Output:

-5 -2 1 4

  

题解
  

  这道题怪怪的,感觉codechef上的不少题想法都怪怪的。感谢lwher的思路。
  K最多只有10个,意味着原数列已经很接进答案了——在n较大的情况下,我们随机抓两个数,很可能得到最终的a0和d(因为这两个数很有可能是不需要改动的),而在n较小的情况下,也一定保证至少有两个数不需要改动。
  所以我们只要枚举前23位(其实13就够了,因为最多只改十个),选出两个数算出a0和d,带回原数组检验是否有<=K个数不满足ai的表达式即可。


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
int n,m,a[100002];
ll ansa=1<<30,ansd=1<<30;
void init()
{
scanf(&quot;%d%d&quot;,&n,&m);
int i;
for(i=1;i<=n;i++) scanf(&quot;%d&quot;,&a);
}
void work1()
{
int i,j,k,w,ct;
ll t,d,a0;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{t=a[j]-a; w=j-i;
if(t%w!=0) continue;
d=t/w; a0=a-i*d;
ct=0;
for(k=1;k<=n;k++)
{if(a[k]!=a0+k*d) ct++;
if(ct>m) break;
}
if(ct<=m)
{if(ansa>a0)
{ansa=a0; ansd=d;}
else if(ansa==a0&&ansd>d) ansd=d;
}
}
for(i=1;i<n;i++)
printf(&quot;%lld &quot;,ansa+i*ansd);
printf(&quot;%lld\n&quot;,ansa+n*ansd);
}
void work2()
{
int i,j,k,w,ct;
ll t,d,a0;
for(i=1;i<23;i++)
for(j=i+1;j<=23;j++)
{t=a[j]-a; w=j-i;
if(t%w!=0) continue;
d=t/w; a0=a-i*d;
ct=0;
for(k=1;k<=n;k++)
{if(a[k]!=a0+k*d) ct++;
if(ct>m) break;
}
if(ct<=m)
{if(ansa>a0)
{ansa=a0; ansd=d;}
else if(ansa==a0&&ansd>d) ansd=d;
}
}
for(i=1;i<n;i++)
printf(&quot;%lld &quot;,ansa+i*ansd);
printf(&quot;%lld\n&quot;,ansa+n*ansd);
}
int main()
{
init();
if(n<=22) work1();
else work2();
return 0;
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143574-1-1.html 上篇帖子: HDU Task Schedule && ZOJ Talented Chef 下篇帖子: codechef Chef and Bracket-Pairs
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表