Codeforces Round #480 (Div. 2)980C Posterized+分组类贪心

传送门:http://codeforces.com/contest/980/problem/C

参考

题意:

给定n个数字,每个数在0~256间,现在给至多连续k的数分为一组,给出字典序最小的答案。 

思路:

贪心,对于每一个a[i],查找$max(0,a[i] - k + 1)$到 a[ i ] 中符合$vis[ t ] ==t || vis[ t ] = -1$的最小数字 T ,把这个T到 a[ i ]间的更新为T;

 

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+9;
const int  inf = 0x3f3f3f3f;
int n,k;
int a[maxn],vis[maxn];
int d[maxn];
int main(){
    scanf("%d%d", &n,&k);
    for(int i=1; i<=n; i++)
        scanf("%d", &a[i]);
    memset(vis,-1,sizeof(vis));
    for(int i=1;i <= n; i++)
    {
        if(vis[a[i]]!=-1)
            printf("%d%c",vis[a[i]]," \n"[i==n]);
        else 
        {
            int j;
            int tot = 0;
            for(j = max(0,a[i] - k + 1); j<=a[i]; j++)
            {
                if(vis[j] == -1||vis[j] == j)
                {
                    break;
                }
                
            }
            for(int g = j;g<=a[i];g++)
            {
                vis[g] = j;
            }
            printf("%d%c",vis[a[i]]," \n"[i==n]);
        }
    }
    return 0;
}
CF980C

 

posted @ 2018-05-25 20:28  ckxkexing  阅读(276)  评论(0编辑  收藏  举报