【CS Round #39 (Div. 2 only) B】Circle Elimination

Link:https://csacademy.com/contest/round-39/task/circle-elimination/

Description

Solution

把n个点依次标为1..n;
然后按照数字大小给每个人排序(记录它原来的位置);
走到第一个人,然后依次走到下一个人即可;
每次枚举往左走还是往右走即可;

NumberOf WA

0

Reviw


Code

/*

*/
#include <bits/stdc++.h>
#define int long long
using namespace std;

struct node{
    int num,pos;
};

node a[(int) 1e5+10];
int n;

bool cmp(node a,node b){
    return a.num < b.num;
}

main(){
    scanf("%lld",&n);
    for (int i = 1;i <= n;i++){
        int x;
        scanf("%lld",&x);
        a[i].num = x,a[i].pos = i;
    }
    sort(a+1,a+1+n,cmp);
    int x = a[1].pos,ans = 0;
    for (int i = 2;i <= n;i++){
        int temp;
        if (a[i].pos<=x){
            temp = min(x-a[i].pos,n-x+a[i].pos);
        }else
            temp = min(a[i].pos-x,x+n-a[i].pos);
        ans+=temp;
        x = a[i].pos;
    }
    printf("%lld\n",ans);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(132)  评论(0编辑  收藏  举报