九校联考-长沙市一中NOIP模拟Day2T1 旋转子段(rotate)

问题描述

ZYL有N张牌编号分别为1, 2,……,N。他把这N张牌打乱排成一排,然后他要做一次旋转使得旋转后固定点尽可能多。如果第i个位置的牌的编号为i,我们就称之为固定点。
旋转可以被认为是将其中的一个子段旋转180度,这意味着子段的第一张牌和最后一张牌交换位置,以及第二张牌和倒数第二张牌交换位置,等等。写一个程序,找到旋转子段(子段长度可以为1)。

输入

第一行包含一个整数 N (1 ≤ N ≤100 000)。
第二行有N个数,第i个数表示旋转之前第i个位置的牌的编号。

输出

找到固定点最多的旋转所选的子段,输出旋转之后固定点的个数。

输入输出样例

样例 1 样例 2
rotate.in rotate.out rotate.in rotate.out

样例一

-rotate.in

4
3 2 1 4

-rotate.out

4

样例二

-rotate.in

2
1 2

-rotate.out

2

样例解释:

在样例1中,只需要旋转的子段[3,2,1],将排列变成1 2 3 4,旋转后所有的牌都为固定点。答案为4。
在样例2中,所有的牌已经在固定点,旋转子段[1]或者子段[2],答案为2。

数据范围

30%的数据满足:N ≤ 500;
60%的数据满足:N ≤ 5000;
100%的数据满足:1 ≤ N ≤ 100 000

依然不会,只会60pts的暴力,枚举每一个旋转中心判断最佳的旋转方式

先放标程

#include<bits/stdc++.h>
using namespace std;

const int Maxn = 5e5+5;

inline int read(){
    int x = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch <= '9' && ch >= '0')
        x = x*10+ch-'0',ch = getchar();
    return x*f; 
}

vector<int>G[Maxn<<1];
int N,ctr,L,R,ans;
int A[Maxn],Sum[Maxn],Suf[Maxn]; 

bool cmp(int x,int y){
    return abs(2*x - ctr) < abs(2*y - ctr); 
}

int main(){
    freopen("rotate.in","r",stdin);
    freopen("rotate.out","w",stdout);
    N = read();
    for(int i = 1 ; i <= N ; ++i){
        A[i] = read();
        G[A[i] + i].push_back(i);
        if(A[i] == i) Sum[i] = 1;
        Sum[i] += Sum[i - 1];
    }
    for(int i = N ; i ; --i){
        if(A[i] == i) Suf[i] = 1;
        Suf[i] += Suf[i + 1];
    }
    for(int i = 2 ; i <= 2*N ; ++i)
    if(!G[i].empty()){
        ctr = i;
        int l , r , ret , n;
        sort(G[i].begin(),G[i].end(),cmp);
        n = G[i].size();
        for(int j = 0 ; j < n ; ++j){
            l = G[i][j];
            r = i - G[i][j];
            if(l > r) swap(l , r);
            ret = Sum[l-1] + Suf[r + 1] + j + 1; 
            if(ret > ans) ans = ret,L = l,R = r;
        }   
    }
    printf("%d\n",ans);
    return 0;
}

转载于:https://www.cnblogs.com/shulker/p/9678229.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值