[映射]JZOJ 6288 旋转子段

Description

 

Input

Output

 

Sample Input

Sample 1:
4
3 2 1 4

Sample 2:
2
1 2
 

Sample Output

Sample 1:
4

Sample 2:
2
 
 

Data Constraint

 

Hint

分析

发现每个点与应达位置都应有一个对称轴,而对称轴相等时,这些点都是固定点,而且旋转区间中原有的非中心点的固定点会消失

弄个前缀和和vector随便搞搞就好了

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int n,cmp,ans,fir;
int to[N],s[2*N];
vector<int> g[2*N];

int Abs(int a) {return a>0?a:-a;}

bool CMP(int a,int b) {
    return Abs(a-cmp)<Abs(b-cmp);
}

int Sum(int x,int r) {
    return s[x+Abs(x-r)]-s[x-Abs(x-r)-1]-s[x]+s[x-1];
}

int main() {
    freopen("rotate.in","r",stdin);
    freopen("rotate.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;i++) {
        scanf("%d",&to[i]);
        if (i==to[i]) s[i*2-1]=-1,ans++;
        else g[i+to[i]-1].push_back(2*i-1);
    }
    fir=ans;
    for (int i=1;i<2*n;i++) s[i]+=s[i-1];
    for (int i=1;i<2*n;i++)
        if (g[i].size()) {
            cmp=i;sort(g[i].begin(),g[i].end(),CMP);
            for (int j=0,m=g[i].size();j<m;j++)
                ans=max(ans,fir+j+1+Sum(i,g[i][j]));
        }
    printf("%d",ans);
}
View Code

 

posted @ 2019-08-12 07:32  Vagari  阅读(153)  评论(0编辑  收藏  举报