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

Input:
4 2
1 2 1 4 Output:
-5 -2 1 4 真·脑洞题。
原来想从差分入手。。。然后发现n小的时候比较棘手啊QWQ 不过正解也非常的开脑洞。。。因为k<=n-2,所以至少会有两个数不变。。。。
所以? n小的时候可以直接暴力枚举哪两个数不变,然后暴力更新答案就行了。。。。 但是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
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100005;
ll a[maxn],n,A,B,k,lef;
ll X=1ll<<62ll,Y=1ll<<62ll,ans[maxn]; int main(){
srand(time(0)); scanf("%lld%lld",&n,&k),lef=5e6;
for(int i=1;i<=n;i++) scanf("%lld",a+i); while(lef>0){
A=rand()%n+1,B=rand()%n+1;
if(A>B) swap(A,B);
if(A==B||llabs(a[A]-a[B])%(B-A)) continue; ll der=(a[A]-a[B])/(A-B),sx;
lef-=n; int now=k;
for(int i=1;i<=n&&now>=0;i++) if(a[i]!=a[A]+der*(ll)(i-A)) now--; if(now<0) continue; sx=a[A]+der*(ll)(1-A); if(sx<X||(sx==X&&der<Y)){
X=sx,Y=der;
for(int i=1;i<=n;i++) ans[i]=sx+(i-1)*der;
}
} for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
puts("");
return 0;
}
												

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. 【BZOJ 3907】网格 组合数学

    大家说他是卡特兰数,其实也不为过,一开始只是用卡特兰数来推这道题,一直没有怼出来,后来发现其实卡特兰数只不过是一种组合数学,我们可以退一步直接用组合数学来解决,这道题运用组合数的思想主要用到补集与几何 ...

  2. POJ2236:Wireless Network(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 39772   Accepted: 164 ...

  3. MySQL 配置文件及逻辑架构

    配置文件: linux:/etc/my.cnf              默认配置文件:/usr/share/mysql/my-default.cnf windows:my.ini 主要日志文件: 二 ...

  4. JS模块化工具requirejs教程02

    基本API require会定义三个变量:define,require,requirejs,其中require === requirejs,一般使用require更简短 define 从名字就可以看出 ...

  5. peity(jQuery 插件可以将元素内容转换为一个小的 <svg> 饼图,圆环图,条形图和折线图)

    API地址:https://www.awesomes.cn/repo/benpickles/peity 实例效果

  6. Ubuntu pppoe 拨号上网

    -------------蓝色是终端里面的连接方式,可以不看--------------------- ADSL上网,Ubuntu下是可以的,虽然以前没用过拨号上网,不过查了查也不是很麻烦. 打开终端 ...

  7. 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E

    http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...

  8. 一维和二维ST模板

    void init(){ ; i < n; i++) st[i][] = a[i]; ; ( << j) <= n; j++){ ; i + ( << j) - & ...

  9. 【数据结构】bzoj1651专用牛棚

    Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will onl ...

  10. codeforce C. Okabe and Boxes

    题目传送门 这道题 每次删除一个点 那么这个点必然在栈里面 那么如果堆顶不是他 我们就需要一次操作使得堆合理 这时我们可以把他删除然后把他下面的点打个标记表示这下面的点以后想怎么排就怎么排以后都不需要 ...