Painting the Fence Gym - 101911E(构造)
There is a beautiful fence near Monocarp's house. The fence consists of nn planks numbered from left to right. The ii -th plank has color aiai .
Monocarp's father have decided to give his son mm orders. Each order is a color cjcj . After each order Monocarp finds leftmost and rightmost planks currently having color cjcj and repaints all planks between them into color cjcj .
For example, if, at first, fence looked like (from left to right) [1,2,3,1,4,1,5,6][1,2,3,1,4,1,5,6] , then after fulfilling an order with color 11 fence will look like [1,1,1,1,1,1,5,6][1,1,1,1,1,1,5,6] .
Assume that Monocarp fulfills all orders in the order they come, one by one.
Note that if current order is about color xx and there is no more than one plank in the fence having color xx , then Monocarp doesn't repaint anything, so he can skip this order and skip to the next one.
Find out the color of each plank after Monocarp has done all the given orders.
Input
The first line contains one integer nn (1≤n≤3⋅105)(1≤n≤3⋅105) — the number of planks in the fence.
The second line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤3⋅105)(1≤ai≤3⋅105) , where aiai is the initial color of the ii -th plank.
The third line contains one integer mm (1≤m≤3⋅105)(1≤m≤3⋅105) — the number of orders.
The fourth line contains mm space-separated integers c1,c2,…,cmc1,c2,…,cm (1≤cj≤3⋅105)(1≤cj≤3⋅105) , where cjcj is the color of the jj -th order.
Output
Print nn space-separated integers — the colors of planks in the fence after processing all mm orders.
Examples
4
1 2 1 2
2
2 1
1 2 2 2
8
7 1 7 1 23 9 23 1
4
23 4 7 1
7 7 7 1 1 1 1 1
Note
In the first example initial appearance of the fence is [1,2,1,2][1,2,1,2] . After the first order (color 22 ) fence will look like [1,2,2,2][1,2,2,2] . After the second order (color 11 ) appearance of the fence will not change.
In the second example initial appearance of the fence is [7,1,7,1,23,9,23,1][7,1,7,1,23,9,23,1] . After the first order (color 2323 ) the fence will look like [7,1,7,1,23,23,23,1][7,1,7,1,23,23,23,1] . After the second order (color 44 ) appearance of the fence will not change. After the third order (color 77 ) the fence will look like [7,7,7,1,23,23,23,1][7,7,7,1,23,23,23,1] . After the fourth order (color 11 ) the fence will look like [7,7,7,1,1,1,1,1][7,7,7,1,1,1,1,1] .
题意:给你n个位置,每个位置都有颜色,然后m个操作,每个操作给你一种颜色,然你从该颜色的出现的最左端涂色到最右端,最后输出涂色情况
思路:记录下每个颜色出现的位置,然后按照操作进行涂色,涂色的时候顺便清楚这一区间内所有颜色的涂色情况。用set实现是不错的,毕竟自带erase,但是要注意不要每次涂色都直接去数组上更改,可以先将最后答案每个颜色的最左端和最右端求出来再进行涂色(做的时候思路没错,实现起来太沙雕了,tle到崩溃,借鉴(抄袭)网上题解代码)
#include<bits/stdc++.h>
using namespace std; const int maxn = 3e5+;
bool vis[maxn];
set<int>s[maxn];
int n,m;
int ans[maxn];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&ans[i]);
s[ans[i]].insert(i);
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int tmp;
scanf("%d",&tmp);
if(s[tmp].size() < || vis[tmp])
{
vis[tmp] = ;
continue;
}
int l = *(s[tmp].begin());
int r = *(s[tmp].rbegin());
for(int j=l+;j<r;j++)
{
s[ans[j]].erase(j);
if(vis[ans[j]] && s[ans[j]].size() >= ) ///如果涂了色,那么就不必一个个向后erase,直接跳至该色右端点,再erase掉该端点就可,有效防止tle
{
j = *(s[ans[j]].begin());
s[ans[j]].erase(j);
}
}
vis[tmp] = ;
}
for(int i=;i<maxn;i++)
{
if(vis[i] && s[i].size()>)
{
int l = *(s[i].begin());
int r = *(s[i].rbegin());
for(int j=l;j<=r;j++)ans[j] = i;
}
}
int tmp = ;
for(int i=;i<=n;i++)
{
printf(i != n?"%d ":"%d\n",ans[i]);
}
}
Painting the Fence Gym - 101911E(构造)的更多相关文章
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
- [luogu P2205] [USACO13JAN]画栅栏Painting the Fence
[luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...
- 洛谷 画栅栏Painting the Fence 解题报告
P2205 画栅栏Painting the Fence 题目描述 \(Farmer\) \(John\) 想出了一个给牛棚旁的长围墙涂色的好方法.(为了简单起见,我们把围墙看做一维的数轴,每一个单位长 ...
- Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)
传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...
- CodeForces-1132C Painting the Fence
题目链接 https://vjudge.net/problem/CodeForces-1132C 题面 Description You have a long fence which consists ...
- 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence
题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...
- Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化
题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...
- Educational Codeforces Round 61 Editorial--C. Painting the Fence
https://codeforces.com/contest/1132/problem/C 采用逆向思维,要求最大的覆盖,就先求出总的覆盖,然后减去删除两个人贡献最少的人 #include<io ...
- C. Painting the Fence
链接 [https://codeforces.com/contest/1132/problem/C] 题意 就是有个n长的栅栏,然后每个油漆工可以染的区域不同 给你q让你选出q-2个人使得被染色的栅栏 ...
随机推荐
- 创建表空间、新增用户、给用户赋予DBA权限 、删除用户下的上有数据表
正文原创 一:查询数据库实例有多少用户: [oracle@localhost ~]$ sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3.0 Product ...
- 为什么在移动端用rem圆角不圆
rem是根据网页效果图的尺寸来计算的,当然还要借助媒体查询来完成.例如你的设计稿如果是宽720px的话,那你的文字就要以原始大小除以11.25,就是对应下面媒体查询720px:例如16px的话就要16 ...
- Java编程之前的复习和练习
日期:2018.7.14 星期六 博客期:001 今天先是试着写一下博客,最近去青海旅游了,学习时间有点少,但空余时间还是有学习的,不管怎么样吧!先说一下我的这几天的成果——“Bignum”类,虽然很 ...
- day 13 装饰器
装饰器基础 装饰器的目的是为了给被装饰 对象,增加新功能,或者说增加某种能力 在程序中工具就是函数 如此一来,装饰器指的就是一个函数,被装饰着也是一个函数 总结;装饰器就是用一个函数去拓展另外一个已存 ...
- python(1):数据类型/string/list/dict/set等
本系列文章中, python用的idle是spyder或者pycharm, 两者都很好用, spyder 是在anaconda 中的, 自带了很多包可以用, pycharm 只是个编译器, 没有很多包 ...
- bzoj 1415
莫名互测题... 这题一看就是期望dp,可是不会转移,所以考试写50分暴力走人... #include <cstdio> #include <cmath> #include & ...
- Distance
1191: Distance 时间限制: 1 Sec 内存限制: 32 MB 题目描述 There is a battle field. It is a square with the side l ...
- Nginx详解二十:Nginx深度学习篇之HTTPS的原理和作用、配置及优化
一.HTTPS原理和作用: 1.为什么需要HTTPS?原因:HTTP不安全1.传输数据被中间人盗用.信息泄露2.数据内容劫持.篡改 2.HTTPS协议的实现对传输内容进行加密以及身份验证 对称加密:加 ...
- solt插槽简单使用实例
在父组件内可以定义方法,变量 等,还可以在父组件中使用呢. 样式可以在子组件里写,也可以在父组件写. 子组件: <template> <div class="admin-u ...
- python Com接口测试
import comtypes.client as cc import comtypes tlb_id = comtypes.GUID("{D85C6069-D628-4276-93C3-9 ...