poj 1989 The Cow Lineup

N個數字中每個數字的範圍是1-K,求出最少的數字序列,使得在N個數字組成的序列中不存在。
關鍵在於已經給出的序列的基礎上求出對1-K之間的全排列的總的個數,那麼最少的數字序列即爲全排列的總的個數+1.

#include<iostream>
#include<string.h>
#include<math.h>
#include<fstream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector> 
#define MAXSIZE 100
using namespace std;

int sch[11000]; 
int countn;
int ori[100100];

int main ()
{
    //freopen("data_1989.txt","r",stdin);
    int n, k, i, j, ans;
    scanf("%d %d", &n, &k);
    for (i = 0; i < n; i++)
        scanf("%d", &ori[i]);
    ans = countn     = 0;
    memset(sch, 0, sizeof(sch));
    for (i = 0; i < n; i++)
    {
        if (sch[ori[i]] == 0)
        {
           sch[ori[i]] = 1;

           countn++;
           if (countn == k)
           {
              countn = 0;
              ans++;
              memset(sch, 0, sizeof(sch));
           }
        }
    }
    printf("%d\n", ans + 1);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章