博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 980C Posterized
阅读量:5335 次
发布时间:2019-06-15

本文共 3467 字,大约阅读时间需要 11 分钟。

C. Posterized
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.

Their algorithm will be tested on an array of integers, where the ii-th integer represents the color of the ii-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).

To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than kk, and each color should belong to exactly one group.

Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.

To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing kk to the right.

To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.

Input

The first line of input contains two integers nn and kk (1n1051≤n≤105, 1k2561≤k≤256), the number of pixels in the image, and the maximum size of a group, respectively.

The second line contains nn integers p1,p2,,pnp1,p2,…,pn (0pi2550≤pi≤255), where pipi is the color of the ii-th pixel.

Output

Print nn space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.

Examples
input
Copy
4 3 2 14 3 4
output
Copy
0 12 3 3
input
Copy
5 2 0 2 1 255 254
output
Copy
0 1 1 254 254
Note

One possible way to group colors and assign keys for the first sample:

Color 22 belongs to the group [0,2][0,2], with group key 00.

Color 1414 belongs to the group [12,14][12,14], with group key 1212.

Colors 33 and 44 belong to group [3,5][3,5], with group key 33.

Other groups won't affect the result so they are not listed here.

题意:给你n个数让你分组,每组最多k个数,输出最小的情况。在打重现的时候第二个样例没看懂,我一开始认为应该输出是0 1 0 254 254,但是emmm。补题的时候从后往前搜-1或相等的时候不知道哪错了wa了一发,改成从前搜之后就过了。

1 #include 
2 using namespace std; 3 #define fi first 4 #define se second 5 #define ll long long 6 #define pb push_back 7 const int N=1e5+5; 8 int a[N]; 9 int b[N];10 int main()11 {12 int n,k;13 scanf("%d%d",&n,&k);14 memset(a,-1,sizeof(a));15 for (int i=1;i<=n;i++)16 {17 int t;18 scanf("%d",&b[i]);19 t=b[i];20 if(a[t]==-1)21 {22 int flag;23 for (int j=max(0,t-k+1);j<=t;j++)24 {25 if(a[j]==-1||a[j]==j)26 {27 flag=j;28 break;29 }30 }31 for (int j=flag;j<=t;j++)32 {33 a[j]=flag;34 }35 }36 37 }38 for (int i=1;i<=n;i++)39 {40 if(i!=n)41 printf("%d ",a[b[i]]);42 else printf("%d\n",a[b[i]]);43 }44 /*for (int i=0;i<=150;i++)45 {46 cout<
<<"s";47 }48 cout<

 

转载于:https://www.cnblogs.com/TheSilverMoon/p/9129250.html

你可能感兴趣的文章
213. String Compression【LintCode java】
查看>>
适应环境
查看>>
nexus设备android4.1.1系统下载地址及安装方法
查看>>
Python—语句
查看>>
day20——rac搭建
查看>>
Windows编程
查看>>
java数据结构之线性表
查看>>
Yii2 CSRF
查看>>
高德地图接口测试地址
查看>>
shell 获取不同目录下指定文件,并把文件复制到一个文件夹下面
查看>>
hdu-3790最短路刷题
查看>>
微信小程序开发技巧总结 (一)-- 数据传递和存储
查看>>
Java基础编程题——分别统计出其中汉字、英文字母、空格、数字和其它字符的个数...
查看>>
物理机以及不同的虚拟机的转换方法
查看>>
express get和post方法
查看>>
关于日历实现代码里lunarInfo(农历)数组
查看>>
【转】winrar命令行详解
查看>>
#!/usr/bin/python3 和 #!/usr/bin/env python3的区别
查看>>
AWK中的OFS的问题
查看>>
带你彻彻底底弄懂Scroller
查看>>