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. 做一个所见即所得的CSS效果

    style 也是标签(在非ie内核的浏览器中支持),我们将style设置成 contenteditable的时候,那么那的内容就可以编辑了.仔细的体验下,如果我们将背景修改成红色的.那么只要书写完,立 ...

  2. P2765 魔术球问题(网络流24题)

    题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...

  3. MySQL rpm 版本安装

     准备: [root@localhost moudles]# ls MySQL-client-5.6.36-1.linux_glibc2.5.x86_64.rpm MySQL-server-5.6.3 ...

  4. Nios II 中的缓存和内存数据的读写

    nios 使用地址中31bit来表示访问是否bypass cache.如果bit 31=0 表示不bypass cache,即使用cache里的数据:如果bit 31=1表示bypass cache, ...

  5. 【BZOJ】1708: [Usaco2007 Oct]Money奶牛的硬币

    [算法]DP [题解] 如果每个排列算一种,则令f[i]表示凑成面值为i的方案数,容易推出f[i]+=f[i-a[j]]. 现在是每个组合才算一种,令f[i][j]第二维表示只使用前j种面值,f[i] ...

  6. WebSocket最简易理解,term.js插件的使用

    介绍WebSocket WebSocket我想大家并不陌生,无论你的的工作中是否用到了它,都或多或少听说过,它是h5中新增的,在某些方面可以很好的替代我们一直沿用的ajax技术,甚至更加的出色.但是它 ...

  7. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  8. 字符设备驱动ioctl实现用户层内核层通信

    测试代码实现 memdev.h #ifndef _MEMDEV_H_ #define _MEMDEV_H_ #include<linux/ioctl.h> #ifndef MEMDEV_M ...

  9. Linux内核线程之深入浅出【转】

    转自:http://blog.csdn.net/yiyeguzhou100/article/details/53126626 [-] 线程和进程的差别 线程的分类 1     内核线程 2     轻 ...

  10. time_t转化成日期格式小工具

    time_t转化成日期格式小工具下载  http://files.cnblogs.com/files/lansan0701/TimeTool.zip