codeforces 980C Posterized

Title:

255 pixel grids, you can group these 255, and the size of each group cannot exceed k.

Given n pixels, each pixel is required to be represented by the key of this group, and the lexicographical order represented should be the smallest.

Ideas:

Thanks js for teaching this mentally retarded.

It is natural to think of greed, that is, every time the current number is found, the smallest number that can be used as its key must be found.

Then this number can only have two cases, one is that the number has not been used, the other is that the number is its own key, and the other conditions are not satisfied.

for example

4 3
2 14 3 4

2 find 0, and then change the keys from 0 to 2 to 0;

When looking for 3, since 1 and 2 have been used, 3 can only find itself.

Code:

 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 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325900271&siteId=291194637