zoukankan      html  css  js  c++  java
  • CodeChef

    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;
    }
    
    
    
  • 相关阅读:
    Django的model form
    Django之验证码
    ajax获取跨域数据
    js+css模仿打字效果
    CSS自定义消息提示
    CSS画各种二维图形
    最简单的动态进度条
    利用javascript(自定义事件)记录尺寸可变元素的尺寸变化过程
    超级简单的利用javascript实现文件拖拽事件
    javascript 公历与农历相互转换工具类
  • 原文地址:https://www.cnblogs.com/JYYHH/p/9101912.html
Copyright © 2011-2022 走看看