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个人使得被染色的栅栏 ...
随机推荐
- jquery通过visible来判断标签是否显示或隐藏
if($(".spnTotal").is(":visible")==false) { alert('隐藏'); } else { alert('显示'); }
- 纯css使用线性渐变实现滚动进度条(来自于微信前端早读课)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ionic3 打包Xcode 9 Swift Language Version (SWIFT_VERSION) Ask 报错
解决方案 选择4.0 然后报错17个,类似以下这样的错误 'AVMediaTypeVideo' has been renamed to 'AVMediaType.video' 根据提示更改 AVMed ...
- Socket网络编程(一)
1.什么是网络通讯?(udp.tcp.netty.mina) udp:漂流瓶,每个人都可以向大海里面扔漂流瓶,不管有没有人捡到.(不管接收方有没有,我只往指定的地址发送东西,64kb以内) tcp:电 ...
- LeetCode(107): 二叉树的层次遍历 II
Easy! 题目描述: 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,20,null,null,15,7], ...
- time与datetime模块
在python中,通常用下面几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 格式化的时间字符串(format s ...
- python截图
import time import os, win32gui, win32ui, win32con, win32api def window_capture(dpath,name,srcbmp=[0 ...
- C++中的继承(1) 继承方式
1.继承与派生 继承是使代码可以复用的重要手段,也是面向对象程序设计的核心思想之一.简单的说,继承是指一个对象直接使用另一对象的属性和方法.继承呈现了 面向对象程序设 计的层次结构, 体现了 由简单 ...
- Java享元模式
定义:提供了减少对象数量从而改善应用所需的对象结构的方式 运用共享技术有效支持大量细微度的对象 类型:结构型 应用场景:系统底层的开发啊,以便解决系统的性能问题 系统有大量的相似对象,需要缓存池的场景 ...
- Unicode字符编码表
十进制 十六进制 字符数 编码分类(中文) 编码分类(英文) 起始 终止 起始 终止 (个) 0 127 0000 007F 128 C0控制符及基本拉丁文 C0 Control and B ...