【Codeforces Beta Round #45 D】Permutations
阅读原文时间:2021年04月20日阅读:1

【题目链接】:http://codeforces.com/problemset/problem/48/D

【题意】

给你n个数字;

然后让你确定,这n个数字是否能由若干个(1..x)的排列连在一起打乱顺序后组成;

如果可以的话,确定每个数字属于哪一个排列;

否则输出-1

【题解】

把各个数字映射到数组上去;

然后如果大的数字的个数大于小的数字的个数的话,无解;

其他情况都能有解;

贪心地构造排列就好;

【Number Of WA】

1

【反思】

判断无解的循环的里,上界弄错了;

把数字的范围和数组的大小搞混了;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5;

int n,num = 0;
int ans[N+100];
vector <int> a[N+100];

int main(){
    //Open();
    //Close();
    scanf("%d",&n);
    rep1(i,1,n){
        int x;
        scanf("%d",&x);
        a[x].pb(i);
    }
    int ma = 0;
    rep2(i,N,1){
        ma = max(ma,(int) a[i].size());
        if ((int) a[i].size() < ma){
            cout << -1 << endl;
            return 0;
        }
    }
    while ( !a[1].empty()){
        num++;
        for (int i = 1;i <= N;i++){
            if ( (int) a[i].size() == 0 ) break;
            ans[a[i].back()] = num;
            a[i].pop_back();
        }
    }
    cout << num << endl;
    rep1(i,1,n)
        cout << ans[i] << ' ';
    cout << endl;
    return 0;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章