Codeforces 1237B. Balanced Tunnel
这一题有点意思
首先预处理出 $pos[x]$ 表示编号 $x$ 的车是第几个出隧道的
然后按进入隧道的顺序枚举每辆车 $x$ 考虑有哪些车比 $x$ 晚进入隧道却比 $x$ 早出隧道
显然是 $1$ 到 $pos[x]$ 中还没访问过的车,那么暴力做法就是这样枚举然后看看有哪些没标记并打上标记
注意到每辆车只要打上一个标记即可,并且每次打标记都只在一段前缀区间内
那么维护一下 $r$ 表示之前打标记的区间的最右端,每次只要考虑 $[r,pos[x]]$ 即可(注意 $pos[x]$ 本身不用打标记,不产生贡献)
均摊复杂度 $O(n)$(代码实现时可以省略标记数组)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e5+;
int n,pos[N];
struct dat {
int x,y;
inline bool operator < (const dat &tmp) const {
return x!=tmp.x ? x<tmp.x : y<tmp.y;
}
}A[N];
bool vis[N];
int main()
{
n=read(); int ans=;
for(int i=;i<=n;i++) A[i].x=read();
for(int i=;i<=n;i++) A[i].y=read(),pos[A[i].y]=i;
int l=,r=;
for(int i=;i<=n;i++)
{
r=max(r,pos[A[i].x]);
for(int k=l;k<r;k++)
{
ans++;
vis[A[k].y]=;
}
l=r+;
}
printf("%d\n",ans);
return ;
}
Codeforces 1237B. Balanced Tunnel的更多相关文章
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
- Codeforces 1237F. Balanced Domino Placements
传送门 很妙的题 首先先考虑一个简化的问题,现在有一行格子让你填 你要么填一格 要么填两格 有的格子不让你填 问你填了 $a$ 个一格和填了 $b$ 个两格有多少种方案 那么显然先只考虑放两格的方案, ...
- Codeforces 1237E. Balanced Binary Search Trees
传送门 这一题是真的坑人,时间空间都在鼓励你用 $NTT$ 优化 $dp$...(但是我并不会 $NTT$) 看到题目然后考虑树形 $dp$ ,设 $f[i][0/1]$ 表示 $i$ 个节点的树,根 ...
- Codeforces 1237D. Balanced Playlist
传送门 首先显然的,如果一个位置开始播放了两圈还没结束,那么就永远不会结束 先考虑位置 $1$ 开始播放,用一个 $multisetset$ 维护一下当前听的所有歌,直到某一首歌 $r$ 不合法了就停 ...
- Codeforces 1237C2. Balanced Removals (Harder)
传送门 先来考虑一下二维时的情况,那么对于 $x$ 相同的点,我们按 $y$ 排序,然后相邻的一对对消除 最后 $x$ 坐标相同的点最多剩下一个,那么此时所有点的 $x$ 坐标都不一样 再按 $x$ ...
- Codeforces--Balanced Tunnel
问题重述 Codeforces --- Balanced Tunnel 见链接http://codeforces.com/contest/1237/problem/B. Solve 这道题的本质是找递 ...
- Codeforces Global Round 5
传送门 A. Balanced Rating Changes 签到,分正负搞一下就行. B. Balanced Tunnel 题意: 给出\(n\)辆车的进洞顺序和出洞顺序,问有多少量车实现了洞中超车 ...
随机推荐
- C#_switch语句,for循环,do while循环,while循环
1:switch语句,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- RecyclerView只有一行
RecyclerView只有一行 方法1: 将RecyclerView放在父容器RelativeLayout中,并设置RelativeLayout属性 android:descendantFocu ...
- [Java]用 MessageFormat 拼接组合字符串
package com.hy; import java.text.MessageFormat; public class Test3 { public static void main(String[ ...
- nginx不间断服务平滑升级
(1)备份旧的nginx和配置文件 cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/bak_nginx #备份旧版程序 cp /usr/loc ...
- 用key管理可复用元素
先看看不用key管理可复用元素的代码.Vue 会尽可能高效地渲染元素,通常会复用已有元素而不是从头开始渲染.这么做,除了使 Vue 变得非常快之外,还有一些有用的好处,那就是在切换input时不会清楚 ...
- RedHat Enterprise7 修改为CentOS的yum源
报错 解决 1.将自带的yum卸载掉 2.下载centos的yum并安装 wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-met ...
- 1、vinc = vict 胜、征服
- VLAN和VXLAN的区别
VLAN ·概况 VLAN (Virtual Local Area Network)意为虚拟局域网,是在交换机实现过程中涉及到的概念,由802.1Q标准所定义.由于交换机是工作在链路层的网络设备,连接 ...
- Nmap简介
nmap — 网络探测工具和安全/端口扫描器,参考<Nmap参考指南> 描述: Nmap (“Network Mapper(网络映射器)”) 是一款开放源代码的 网络探测和安全审核的工具. ...
- Eslint常用命令
Eslint常用命令 自动修复(针对整个项目) npm run lint -- --fix 运行如上命令,eslint 会自动修复一些简单的错误. 全局安装 npm install -g eslint ...