博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF980C Posterized 贪心 二十五
阅读量:7079 次
发布时间:2019-06-28

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

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.

 

 

 
 题解: 直接贪心,枚举每个数的前面的数,用没有被用过的最早的数做key值,就是字典序最小了
 
#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout << #a << " " << a << endlusing namespace std;const int maxn = 1e5 + 10;const int mod = 1e9 + 7;typedef long long ll;ll x, n, m, vis[maxn], a[maxn];int main(){ std::ios::sync_with_stdio(false); cin >> n >> m; memset( vis, -1, sizeof(vis) ); for( ll i = 0; i < n; i ++ ) { cin >> x; if( vis[x] == -1 ) { for( ll j = max( (ll)0, x-m+1 ); j <= x; j ++ ) { if( vis[j] == -1 || vis[j] == j ) { for( ll k = j; k <= x; k ++ ) { vis[k] = j; } } } } a[i] = vis[x]; } for( ll i = 0; i < n; i ++ ) { if( i == n-1 ) { cout << a[i] << endl; } else { cout << a[i] << " "; } } return 0;}

 

转载于:https://www.cnblogs.com/l609929321/p/9250483.html

你可能感兴趣的文章
C# DataGridView中合并单元格
查看>>
WinXP 无线技巧“区域没有通过无线网络中的发现”一个可能的原因!
查看>>
chrome(转)
查看>>
Java知多少(90)菜单
查看>>
基本语法 protocols Category extension
查看>>
切割图像(一)概要
查看>>
shell重定向
查看>>
IT谁谁说女子不如男行业
查看>>
[全国首发]Swift视频教程
查看>>
矩阵的压缩存储
查看>>
u-boot中添加mtdparts支持以及Linux的分区设置
查看>>
Spring AOP入门——概念和注意事项
查看>>
C#抽象类其中创建一个静态方法
查看>>
在matlab中进行地理坐标和像素坐标的相互转换
查看>>
HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
查看>>
Android分析应用程序的构建过程
查看>>
发布了Android的App,我要开源几个组件!
查看>>
EXCELL中怎么将两列数据对比,找出相同的和不同的数据?
查看>>
(算法)宝石升级问题
查看>>
Android 中的接口回调
查看>>