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. cookie 是存储于访问者的计算机中的变量

    今天把javascript如何用来创建及存储cookie复习了一下,其中的一点体会拿出来和大家讨论,首先看一下基础知识: 什么是cookie cookie 是存储于访问者的计算机中的变量.每当同一台计 ...

  2. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 351[Submit][Sta ...

  3. Codeforces Round #532 (Div. 2) 题解

    Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...

  4. ng父组件调用子组件的方法

    https://www.pocketdigi.com/20170204/1556.html 组件之间方法的调用统一用中间人调用.数据传递直接input和output即可

  5. bzoj 2427 [HAOI2010]软件安装 Tarjan缩点+树形dp

    [HAOI2010]软件安装 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2029  Solved: 811[Submit][Status][Dis ...

  6. php 计算两个日期的间隔天数

    使用php内部自带函数实现 1.使用DateTime::diff 实现计算 参考阅读>>PHP DateTime::diff() 上代码: <?php $start = " ...

  7. kubernetes 参考资料

    kubernetes 参考资料 非常建议先花20分钟,完成这个官方的交互式指南:https://kubernetes.io/docs/tutorials/kubernetes-basics/ 这个教程 ...

  8. jquery序列化表单

    没有使用其他的东西 , 数据传送是最基本的. 前台: var info = $('#dataForm').serialize() ; alert(decodeURIComponent(info,tru ...

  9. js删除一个父元素下面的所有子元素

    比如<div id="ok"><button tpye='button'>111111</button><p>22222</p ...

  10. 从一段字符串中去除数字的shell方法