健美猫

题目大意

旋转序列s使得 ni=1|sii| 最小

做法

大致思路是可以拆绝对值记录个数即贡献,顺着扫,对于一个位置有两个位置很关键。
(我都不知道我在说什么

#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=2000000+10;
int s[maxn],h[maxn],go[maxn],nxt[maxn];
bool bz[maxn];//0 + 1 -
int i,j,k,l,t,n,m,tot,cnt;
ll num,sum,ans;
int read(){
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9'){
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
void add(int x,int y){
    go[++tot]=y;
    nxt[tot]=h[x];
    h[x]=tot;
}
int main(){
    n=read();
    fo(i,1,n){
        s[i]=read();
        if (s[i]>1){
            if (s[i]<=i) add(i-s[i]+1,i);
            else add(n-s[i]+i+1,i);
        }
        if (i+1>s[i]) num-=(ll)s[i],num+=(ll)(i+1),cnt--,bz[i]=1;
        else num+=(ll)s[i],num-=(ll)(i+1),cnt++,bz[i]=0;
    }
    ans=100000000000000;
    fo(i,1,n){
        num+=(ll)cnt;
        ans=min(ans,num);
        num-=(ll)abs(s[i]-1);
        num+=(ll)abs(s[i]-(n+1));
        if (bz[i]) cnt++;else cnt--;
        cnt--;
        bz[i]=1;
        t=h[i];
        while (t){
            k=go[t];
            cnt++;
            cnt++;
            bz[k]=0;
            t=nxt[t];
        }
    }
    printf("%lld\n",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值