Read problems statements in Mandarin Chinese and Russian.

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.

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

  • 2N100000
  • 0Kmin(10, N-2)
  • -109Ai109

Example

  1. Input:
  2. 4 2
  3. 1 2 1 4
  4.  
  5. Output:
  6. -5 -2 1 4
  7.  
  8. 真·脑洞题。
    原来想从差分入手。。。然后发现n小的时候比较棘手啊QWQ
  9.  
  10. 不过正解也非常的开脑洞。。。因为k<=n-2,所以至少会有两个数不变。。。。
    所以? n小的时候可以直接暴力枚举哪两个数不变,然后暴力更新答案就行了。。。。
  11.  
  12. 但是n大的时候呢???
    考虑最后会有n-k个数不变,如果假设1s最多能进行1e7次基本运算,那么我们就随机 1e7/n 次,每次选两个位置。
    这样的话每次选错的概率是 1-C(n-k,2)/C(n,2) [这是正好改k个数的,不到k个数的错误概率更小],总的随机之后还错的概率是 (1 - C(n-k,2)/C(n,2) ) ^ (1e7/n),随便带个n就会发现概率极小,所以就直接这么做行了2333
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. const int maxn=100005;
  5. ll a[maxn],n,A,B,k,lef;
  6. ll X=1ll<<62ll,Y=1ll<<62ll,ans[maxn];
  7.  
  8. int main(){
  9. srand(time(0));
  10.  
  11. scanf("%lld%lld",&n,&k),lef=5e6;
  12. for(int i=1;i<=n;i++) scanf("%lld",a+i);
  13.  
  14. while(lef>0){
  15. A=rand()%n+1,B=rand()%n+1;
  16. if(A>B) swap(A,B);
  17. if(A==B||llabs(a[A]-a[B])%(B-A)) continue;
  18.  
  19. ll der=(a[A]-a[B])/(A-B),sx;
  20. lef-=n;
  21.  
  22. int now=k;
  23. for(int i=1;i<=n&&now>=0;i++) if(a[i]!=a[A]+der*(ll)(i-A)) now--;
  24.  
  25. if(now<0) continue;
  26.  
  27. sx=a[A]+der*(ll)(1-A);
  28.  
  29. if(sx<X||(sx==X&&der<Y)){
  30. X=sx,Y=der;
  31. for(int i=1;i<=n;i++) ans[i]=sx+(i-1)*der;
  32. }
  33. }
  34.  
  35. for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
  36. puts("");
  37. return 0;
  38. }
  1.  

CodeChef - UASEQ Chef and sequence的更多相关文章

  1. [Codechef CHSTR] Chef and String - 后缀数组

    [Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...

  2. CodeChef CHEFSOC2 Chef and Big Soccer 水dp

    Chef and Big Soccer   Problem code: CHEFSOC2 Tweet     ALL SUBMISSIONS All submissions for this prob ...

  3. 【Codechef】Chef and Bike(二维多项式插值)

    something wrong with my new blog! I can't type matrixs so I come back. qwq 题目:https://www.codechef.c ...

  4. 【CodeChef】Chef and Graph Queries

    Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...

  5. [CodeChef - GERALD07 ] Chef and Graph Queries

    Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...

  6. Codechef FNCS Chef and Churu

    Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...

  7. CodeChef - FNCS Chef and Churu(分块)

    https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...

  8. 【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组

    题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r ...

  9. codechef T2 Chef and Sign Sequences

    CHEFSIGN: 大厨与符号序列题目描述 大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串. 记字符串长度为 N,大厨想要在字符串的开头.结尾 ...

随机推荐

  1. HTTP请求中同步与异步有什么不同

    普通的B/S模式就是同步,而AJAX技术就是异步,当然XMLHttpReques有同步的选项. 同步:提交请求->等待服务器处理->处理完毕返回.这个期间客户端浏览器不能干任何事. 异步: ...

  2. CSS网页宽度怎么定比较合适

    设计网页的时候,确定宽度是一件很苦恼的事.以nowamagic.net为例,根据Google Analytics的统计,半年多以来,访问者的屏幕分辨率一共有81种.最小的分辨率是122x160,这应该 ...

  3. git使用笔记(一)入门

    By francis_hao    Nov 17,2016 本来是想把git的使用笔记写在一个文件里,但是越写越长,最后也不得不分开了.这样也好,每一篇一个侧重,可以写的详细一点.   初学乍练 在l ...

  4. Maven如何打包本地依赖包

    有的jar包,在maven中心库里面是没有的,那么,如何在项目中使用呢? 假设我们需要使用:apache-ant-zip-2.3.jar 将该jar包,放在项目的lib目录,例如: 在pom.xml里 ...

  5. [bzoj 2115]线性基+图论

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 给定一个带权无向图,要找出从1到n路径权值异或和最大的那一条的路径异或和. 考虑1到 ...

  6. (转)Django常用命令

    转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .

  7. [05]Git查看、删除、重命名远程分支和tag

    Git查看.删除.重命名远程分支和tag 2015-06-15:加入姊妹篇: 2013-11-06:加入重命名远程分支的内容: 2013-01-09:加入删除远程tag的内容. 姊妹篇:使用Git.G ...

  8. Activiti工作流引擎核心介绍

    引言 Activiti 作为一个遵从 Apache 许可的工作流和业务流程管理开源平台,其核心是基于 Java 的超快速.超稳定的 BPMN 2.0 流程引擎,强调流程服务的可嵌入性和可扩展性,同时更 ...

  9. (转载)--SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  10. 「6月雅礼集训 2017 Day2」C

    [题目大意] 有一棵n个点的完全二叉树,边权均为1,每个点有小鸟容量c[i] 依次来了m只小鸟,第i只小鸟初始位置在pos[i]上,问来了x只小鸟的时候,怎样安排小鸟的路线可以使得小鸟移动的边权和最小 ...