CodeChef - UASEQ Chef and sequence
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
- 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 真·脑洞题。
原来想从差分入手。。。然后发现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的更多相关文章
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- CodeChef CHEFSOC2 Chef and Big Soccer 水dp
Chef and Big Soccer Problem code: CHEFSOC2 Tweet ALL SUBMISSIONS All submissions for this prob ...
- 【Codechef】Chef and Bike(二维多项式插值)
something wrong with my new blog! I can't type matrixs so I come back. qwq 题目:https://www.codechef.c ...
- 【CodeChef】Chef and Graph Queries
Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...
- [CodeChef - GERALD07 ] Chef and Graph Queries
Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 【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 ...
- codechef T2 Chef and Sign Sequences
CHEFSIGN: 大厨与符号序列题目描述 大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串. 记字符串长度为 N,大厨想要在字符串的开头.结尾 ...
随机推荐
- 解决IE下页面空白或者报错:[vuex] vuex requires a Promise polyfill in this browser
[vuex] vuex requires a Promise polyfill in this browser 上述错误的原因是不支持 Promise 方法,导致页面出现空白无法加载. 解决方法如下: ...
- Django请求原理
总结一下: 1. 进来的请求转入/hello/. 2. Django通过在ROOT_URLCONF配置来决定根URLconf. 3. Django在URLconf中的所有URL模式中,查找第一个匹配/ ...
- 如何把SSL公钥和私钥转化为PFX格式
1.登陆 https://myssl.com/cert_convert.html 2.原格式选择为 “PEM”,目标格式选择为 “PKCS12” 3.上传cer到 ”证书文件“,上传key到 ”私 ...
- Java之戳中痛点 - (1)易变业务使用脚本语言编写
脚本语言的3大特征: 1.灵活:脚本语言一般是动态类型,可以不声明变量类型直接使用,也可以在运行期改变类型:2.便捷:脚本语言是解释性语言,在运行期变更非常方便,而不用重启服务3.简单:脚本语言语法比 ...
- 【BZOJ】ARC083 E - Bichrome Tree
[算法]树型DP [题意]给定含n个点的树的形态,和n个数字Xv,要求给每个点赋予黑色或白色和权值,满足对于每个点v,子树v中和v同色的点的权值和等于Xv.n<=10^5 [题解]首先每个点的权 ...
- python_plot画图参数设置
# coding:utf-8 import pandas as pd import numpy as np import matplotlib.pyplot as plt # one_hot数据的读取 ...
- Selenium tutorial/overview
copy from: http://www.jroller.com/selenium/ Selenium tutorial/overview 1. Selenium Introduction 2. S ...
- Oracle基础 03 回滚表空间 undo
--查询默认的undo表空间 select name,value from v$parameterwhere name like '%undo%'; --创建 undotbs2 表空间 create ...
- 【反演复习计划】【COGS2433】&&【bzoj3930,CQOI2015选数】爱蜜莉雅的冰魔法
同bzoj3930. (日常盗题图) #include<bits/stdc++.h> #define N 1000010 #define yql 1000000007 #define ll ...
- JWT是什么?
# 转载请留言联系 什么是JWT? Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的 ...