CodeForces 48D. Permutations

这是我第一次写acm题解,也是我进入校队以来在校队的排位赛内做出的第一道题目,题目的大致意思是给你一堆数字,将它分成若干个子集,每个子集从1开始连续,可以出现若干种情况,如果没有,输出-1,如果有输出其中一种情况。

上网搜了下题解:只搜到了贪心

我的思路大致是开两个数组,一个二维数组,一个记录数字,一个记录它在第几个子集,另外一个数组记录它出现的次数,然后输入每个数字,它出现是第几次就在第几个子集,最后判断是否连续就从小到大开始,小的数字出现次数大于或等于大的数字出现次数。

不足之处,希望大神指教..............

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int s[200000][2]= {0},s1[200000]= {0};
int main()
{
    for(int i=0; i<200000; i++) s1[i]=0;
    int num=0,n,tr=1,mid=0;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>s[i][0];
        if(s[i][0]>mid)
        mid=s[i][0];
        s1[s[i][0]]++;
        s[i][1]=s1[s[i][0]];
    }
    for(int i=1; i<=mid-1; i++)
        if(s1[i]<s1[i+1])
        {
            tr=0;
            break;
        }
    if(tr)
    {
        cout<<s1[1]<<endl;
        for(int i=0; i<n; i++)
        {
            cout<<s[i][1]<<' ';
        }
    }
    else
        cout<<"-1";
    return 0;
}

 

 

9
1 2 3 1 2 1 4 2 5
output
3
3 1 2 1 2 2 2 3 2
input
4
4 3 2 1
output
1
1 1 1 1
input
4
1 2 2 3
output
-1

 

posted @ 2013-07-18 22:04  Qioayang,Allan,Zheng  阅读(294)  评论(0)    收藏  举报