T - Posterized(贪心思维)
Description
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 i-th integer represents the color of the i-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 k, 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 k 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 n and k (1≤n≤10^5, 1≤k≤256), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains n integers p1,p2,…,pn (0≤pi≤255), where pi is the color of the i-th pixel.
Output
Print n space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
Examples
Input
4 3
2 14 3 4
Output
0 12 3 3
Input
5 2
0 2 1 255 254
Output
0 1 1 254 254
Note
One possible way to group colors and assign keys for the first sample:
Color 2 belongs to the group [0,2], with group key 0.
Color 14 belongs to the group [12,14], with group key 12.
Colors 3 and 4 belong to group [3,5], with group key 3.
Other groups won't affect the result so they are not listed here.
解题思路:题目的意思就是以每一个像素的大小x为区间右端点,从[x-k+1,x]这个区间中尽可能选择一个比较小的值,作为这个区间的key值;用vis数组来判断这个区间(初始值都为-1)是否已经占用,如果已被占用即vis[x]!=-1,则不用处理;如果没有占用,遍历这个区间找最小可能代替的值,最大为x,然后依次用这个最小值t来覆盖区间[x-k+1,x]中还没有被覆盖的元素,最后输出每个数对应区间的最小key值即可。贪心思维填充区间key值~
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int n,k,vis[],a[maxn];
int main(){
memset(vis,-,sizeof(vis));//先初始化为-1,表示该区间还未使用
cin>>n>>k;
for(int i=;i<n;++i){
cin>>a[i];
if(vis[a[i]]==-){//判断是否已处于区间中,这里表示未被使用
int t=a[i]-k+;//如果不在区间中,那么最小值可能为t,最大值不超过本身a[i]
if(t<)t=;//如果t小于0,那么t最小为0
while(vis[t]!=-&&vis[t]!=t)t++;//如果这个数已经处于其他区间了,那么t++,找出该区间可以使用的最小值,最大等于t本身
for(int j=t;j<=a[i];++j)vis[j]=t;//将其他点覆盖为t,同样最大到本身,划分区间完毕
}
}
for(int i=;i<n;++i)//输出每个数对应的区间
cout<<vis[a[i]]<<(i==n-?'\n':' ');
return ;
}
T - Posterized(贪心思维)的更多相关文章
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- CF980C Posterized 贪心 二十五
Posterized time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- C. Coffee Break 贪心 思维 有点难 有意思
C. Coffee Break 这个贪心之前好像写过,还是感觉挺难的,有点不会写. 这个题目大意是:给你一个数列n个元素,然后给你一天的时间,给你一个间隔时间d, 问你最少要用多少天可以把这个数列的所 ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- C. Brutality Educational Codeforces Round 59 (Rated for Div. 2) 贪心+思维
C. Brutality time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
随机推荐
- [NOIP2005] 提高组 洛谷P1053 篝火晚会
题目描述 佳佳刚进高中,在军训的时候,由于佳佳吃苦耐劳,很快得到了教官的赏识,成为了“小教官”.在军训结束的那天晚上,佳佳被命令组织同学们进行篝火晚会.一共有n个同学,编号从1到n.一开始,同学们按照 ...
- 奥多朗WIFI 插座
https://aoduolang.tmall.com/category-1089563810.htm?spm=a1z10.1-b.w11212542-12917613245.12.tTWFSc&am ...
- Java正则表达式过滤出字母、数字和中文
原文:http://blog.csdn.net/k21325/article/details/54090066 1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A ...
- 配置-XX:+HeapDumpOnOutOfMemoryError 对于OOM错误自动输出dump文件
配置-XX:+HeapDumpOnOutOfMemoryError 对于OOM错误自动输出dump文件 学习了:http://blog.csdn.net/stevendbaguo/article/de ...
- 不同VLAN之间相互通信的两种方式
(单臂路由.三层交换) 试验环境:东郊二楼第三机房 试验设备:Catalyst 2950-24(SW3) Cisco 2611(R2) Catalyst 3750 SERIES (带两个SD接口,S8 ...
- OpenFileDiag 使用
MSDN模版 Stream myStream =null; OpenFileDialog openFileDialog1 =newOpenFileDialog(); openFileDialog1.I ...
- linux 打包 压缩
序 1.gzip 2.bzip2 3.tar 序 压缩优点 1.节省空间 2.节省带宽 解决脉络 如今有各种压缩文件形式,原因何在?主要是压缩技术更新换代,压缩方法不全然同样.不同的后缀 ...
- Cannot change version of project facet Dynamic Web Module to 3.1 (Eclipse Maven唯一解决方式)
If you want to use version 3.1 you need to use the following schema: http://xmlns.jcp.org/xml/ns/jav ...
- Delphi各销售版本之间的区别
初步的区别: http://www.embarcadero.com/products/delphi/product-editions http://www.embarcadero.com/fr/pro ...
- go8---函数function
package main /* 函数function Go 函数 不支持 嵌套.重载和默认参数. 但支持以下特性: 无需声明原型(C语言在使用函数之前需要声明函数的原型).不定长度变参.多返回值.命名 ...