codeforces 980C Posterized

题意:

255个像素格子,可以把这个255个分组,每组的大小不能超过k。

给出n个像素,要求每个像素用这组的key代表,并且表示出来的字典序要最小。

思路:

感谢js教本智障。

很自然的会想到贪心,也就是说,每次对当前的数,都要找到最小的可以当它的key的数。

那么这种数只能有两种情况,一种是这个数还没有使用过,另一种就是这个数是自己的key,其它情况都不满足。

比如

4 3
2 14 3 4

2找到0,然后把0到2的key都变为0;

找3的时候,由于1,2已经被使用,所以3只能找到自己。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 int a[300];
 6 int main()
 7 {
 8     int n,k;
 9     scanf("%d%d",&n,&k);
10     for (int i = 0;i < 256;i++) a[i] = -1;
11     while (n--)
12     {
13         int x;
14         scanf("%d",&x);
15         if (a[x] == -1)
16         {
17             for (int i = max(0,x-k+1);i <= x;i++)
18             {
19                 if (a[i] == -1 || a[i] == i)
20                 {
21                     for (int j = i;j <= x;j++) a[j] = i;
22                     break;
23                 }
24             }
25         }
26         printf("%d ",a[x]);
27     }
28     return 0;
29 }

猜你喜欢

转载自www.cnblogs.com/kickit/p/9012076.html