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个人使得被染色的栅栏 ...
随机推荐
- Eclipse切换工作空间(Workspace)
通过主菜单 File(文件) -> Switch WorkSpace(切换工作空间).可以切换不同的工作空间,工作空间本身对应的是一个文件夹,其中会包含一个 .metadata 文件夹,应该是用 ...
- ncnn编译安装
1.git clone https://github.com/Tencent/ncnn 2.按照wiki说明来编译,根据需要,选择不同的编译方式.在ncnn/CMakeLists.txt中,可选择编译 ...
- Java的动手动脑(四)
日期:2018.10.18 星期四 博客期:019 Part1:回答为啥会报错 答案:当然会报错啦!因为平常的编程过程中,系统会对我们写的类自动生成一个默认无参形式的构造方法,类似于C++中的体制!这 ...
- LeetCode(98): 验证二叉搜索树
Medium! 题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右 ...
- VGG-Net
论文下载 源码GitHub 目的 这篇文章是以比赛为目的——解决ImageNet中的1000类图像分类和定位问题.在此过程中,作者做了六组实验,对应6个不同的网络模型,这六个网络深度逐渐递增的同时,也 ...
- 基于“MVC”框架集设计模式,开发用户管理系统!
MVC----(Model View Controller)设计模型: M:表示业务数据和业务规则.包括DAO(beans).DBHelper(DBHelper),用于封装数据库连接,业务数据库处理. ...
- Jmeter-JDBC Request参数化
一.参数化 1.选择Query Type(查询类型)为Prepared Select Statement 2.写好sql 3.在Parameter Values中输入参数,多个参数用‘,’隔开 4.P ...
- K8s-Pod控制器
在K8s-Pod文档中我们创建的Pod是非托管的Pod,因为Pod被设计为用后就弃的对象,如果Pod正常关闭,K8s会将该Pod清除,它没有自愈的能力.Pod控制器是用来保持Pod状态的一种对象资 ...
- 饮冰三年-人工智能-linux-09 服务
1:SSH服务(提供远程连接服务) 客户端使用Xshell 链接成功 加快连接速度 关闭防火墙 2:apache 服务(提供网页服务) 2.0 准备环境 关闭防火墙:service iptables ...
- Redis都有哪些数据类型
string 这是最基本的类型了,就是普通的set和get,做简单的kv缓存 hash 这个是类似map的一种结构,这个一般就是可以将结构化的数据,比如一个对象(前提是这个对象没嵌套其他的对象)给缓存 ...