zoukankan      html  css  js  c++  java
  • Mister B and PR Shifts(思维)

    Mister B and PR Shifts(思维)

    Describe

    Some time ago Mister B detected a strange signal from the space, which he started to study.

    After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.

    Let's define the deviation of a permutation p as img.

    Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.

    Let's denote id k (0 ≤ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:

    • k = 0: shift p 1, p 2, ... p n,
    • k = 1: shift p n, p 1, ... p n - 1,
    • ...,
    • k = n - 1: shift p 2, p 3, ... p n, p 1.

    Input

    First line contains single integer n (2 ≤ n ≤ 106) — the length of the permutation.

    The second line contains n space-separated integers p 1, p 2, ..., p n (1 ≤ p i ≤ n) — the elements of the permutation. It is guaranteed that all elements are distinct.

    Output

    Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.

    Examples

    Input

    3
    1 2 3
    

    Output

    0 0
    

    Input

    3
    2 3 1
    

    Output

    0 1
    

    Input

    3
    3 2 1
    

    Output

    2 1
    

    Note

    In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.

    In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.

    In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts.

    Solution

    数据范围1e6暴力绝对T,所以我我们要找一下规律以简化总偏移量的求和.

    我们知道k是将数组后面k位,移到前面来,所以我们可以每次将末尾1位提到前面,每次的偏移量之和承接上一个来求,复杂度O(n)

    假设末尾数不提前,只是整体向右移,则一个数右移后与大于坐标的对于总和之差贡献是-1,反之则贡献是1(这两种数的个数每次循环都会改变)

    对于提到前面的数特判一下不要乱...

    Code

    数字后加LL表示强转long long

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+5;
    ll p[maxn],h[maxn<<1];//h[i]表示大于初始坐标i的数的个数
    ll n,d,x,eq,ans,k;//d是每次右移前大于坐标的数的个数,x是小于的,eq是等于的
    int main(){
    	scanf("%lld",&n);
    	for(ll i=1LL;i<=n;++i){
    		scanf("%lld",&p[i]);
    		if(p[i]>i)d++,h[p[i]-i]++;
    		else if(p[i]==i)eq++,h[0]++;
    		else x++;
    		ans+=abs(p[i]-i);
    	}
    	ll tmp=ans;
    	for(ll last=n-1LL,now=1LL;last>=1LL;last--,now++){
    		tmp+=(eq+x);tmp-=d;
    		d-=h[now];//常规处理完毕,下面开始特判
    		if(p[last+1]>=last+1)h[p[last+1]-(last+1)]--;
    		h[p[last+1]-1+now]++;
    		eq=h[now];
    		if(p[last+1]>1LL)d++;
    		x=n-eq-d;
    		tmp-=abs(p[last+1]-n-1LL);
    		tmp+=abs(p[last+1]-1LL);
    		if(tmp<ans)ans=tmp,k=now;
    	}
    	printf("%lld %lld
    ",ans,k);
    	return 0;
    }
    
  • 相关阅读:
    超硬核Java工程师秋招回忆录+面经汇总,为了拿BAT的研发offer我都做了那些准备?
    使用Prometheus监控Golang服务-基于YoyoGo框架
    Kubernetes Pod OOM 排查日记
    Golang语言排序的几种方式
    设计公共组件需要注意什么
    有关WebSocket必须了解的知识
    docker容器技术
    【SpringBoot】 中时间类型 序列化、反序列化、格式处理
    安装Scrapy的时候报错error: Microsoft Visual C++ 14.0 is required.
    python爬虫学习05-爬取图片
  • 原文地址:https://www.cnblogs.com/Lour688/p/12858743.html
Copyright © 2011-2022 走看看