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 ...
随机推荐
- 使用XML定义组件样式
<TextView android:layout_width="match_parent" android:layout_height="wrap_content& ...
- Our Journey of Dalian Ends 乌鲁木齐网络赛 最小费用最大流
Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpecte ...
- ETL增量单表同步简述_根据dateTime增量
ETL增量单表同步简述 1. 实现需求 当原数据库的表有新增.更新.删除操作时,将改动数据同步到目标库对应的数据表. 2. 设计思路 设计总体流程图如下: 步骤简单说明: 1.设置job的执行属性,如 ...
- Kernel与用户进程通信
测试IPv6 ready logo rfc 3315的时候,遇到一个问题,要求在收到ICMPv6 RA的时候,DHCPv6 Client要发Solicit消息.在平常的应用中,都是启动DHCPv ...
- Python进阶系列之怎么写出pythonic的代码
使用 in/not in 检查key是否存在于字典中 判断某个key是否存在于字典中时,一般的初学者想到的方法是,先以列表的形式把字典所有的key返回,在判断该key是否存在于key列表中 d = { ...
- STM32F104VG (一)中断与外部中断
一.基础知识 1.ARM的中断优先级分硬件优先级和软件优先级两种 当中软件优先级又由抢占优先级和响应优先级组成 2.中断的优先级採用编号小优先的原则. 3.普通情况: 1).假设设定了软件优先级.先看 ...
- 【Record】ART:Android RunTime
资料来自url=9xdxrhR45Uj3p450JQvTUO-dmzcWswNmABVgYAaFS0AXYDi8Q2JOzvu7y33GIOAI_8Lz7JmLrl0x6DoRW8e5oa" ...
- redis 主从 及集群
一.redis 主从架构 搭建redis 主从 (可以用一台主机,也可以两台主机) 环境准备: 一台服务器:192.168.206.6 操作系统:CentOS7.5 redis 版本: redis ...
- 《从零開始学Swift》学习笔记(Day67)——Cocoa Touch设计模式及应用之MVC模式
原创文章,欢迎转载.转载请注明:关东升的博客 MVC(Model-View-Controller,模型-视图-控制器)模式是相当古老的设计模式之中的一个,它最早出如今Smalltalk语言中. 如 ...
- VirtualMachineManager
Java Code Examples for com.sun.jdi.VirtualMachineManager https://www.programcreek.com/java-api-examp ...