问题重述

Codeforces --- Balanced Tunnel

见链接http://codeforces.com/contest/1237/problem/B

Solve

这道题的本质是找递增序列中出现的非递增数的数目。如果未发生超车情况,则进入的车在出去的时候,应该是一个递增的序列。

于是可以用一个pos[x]数组来记录标号为i的车出去时候的顺序,这样,当我们按照进入时候的顺序进行遍历时,如果车发生过超车现象,那么肯定有某辆入序靠后的车其先出去了,也就是pos[x]的值小于之前的最大值。以样例1为例。

进入顺序:                   1 2 3 4 5

进入时车的标号顺序: 3 5 2 1 4

出去的顺序数组:        2 4 3 5 1

出去时车的标号顺序: 4 3 2 5 1

显然,按照35214的顺序遍历的时候,只需要每次保存遍历到当前位置时的最大出去的序号值,就可以判断出是否有超车了,代码如下,时间复杂度为O(n)。当然,这道题还可以采取求逆序对的方式来求解,复杂度为O(nlogn)。

 #include<bits/stdc++.h>

 using namespace std;

 static const int MAX = ;

 int arr[MAX];
int pos[MAX]; int main(){
int n;
scanf("%d", &n); // read arr
for(int i=;i<=n;i++){
scanf("%d", &arr[i]);
}
// construct pos
int num;
for(int i=;i<=n;i++){
scanf("%d", &num);
pos[num] = i;
} // solve
int sum = ;
int tmp = ;
for(int i=;i<=n;i++){
tmp = max(tmp, pos[arr[i]]);
if(pos[arr[i]]<tmp){
sum++;
}
}
printf("%d\n", sum);
return ;
}

Codeforces--Balanced Tunnel的更多相关文章

  1. Codeforces 1237B. Balanced Tunnel

    传送门 这一题有点意思 首先预处理出 $pos[x]$ 表示编号 $x$ 的车是第几个出隧道的 然后按进入隧道的顺序枚举每辆车 $x$ 考虑有哪些车比 $x$ 晚进入隧道却比 $x$ 早出隧道 显然是 ...

  2. Codeforces Global Round 5

    传送门 A. Balanced Rating Changes 签到,分正负搞一下就行. B. Balanced Tunnel 题意: 给出\(n\)辆车的进洞顺序和出洞顺序,问有多少量车实现了洞中超车 ...

  3. Codeforces Round #544 (Div. 3) Editorial C. Balanced Team

    http://codeforces.com/contest/1133/problem/Ctime limit per test 2 secondsmemory limit per test 256 m ...

  4. [Codeforces 873B]Balanced Substring

    Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s  ...

  5. Codeforces 544E K Balanced Teams (DP)

    题目: You are a coach at your local university. There are nn students under your supervision, the prog ...

  6. Balanced Ternary String CodeForces - 1102D (贪心+思维)

    You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...

  7. CodeForces - 873B Balanced Substring(思维)

    inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...

  8. Codeforces 873 B. Balanced Substring(前缀和 思维)

    题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...

  9. codeforces 1133E K Balanced Teams

    题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...

随机推荐

  1. demjson

    demjson.decode() 可以扩展json的类型

  2. ajax 向php发送请求

    <html> <head> <script src="clienthint.js"></script> </head> ...

  3. netty-2.客户端与服务端互发消息

    (原) 第二篇,客户端与服务端互发消息 与第一篇的例子类似,这里服务端需要三个类,客户端也需要三个类. 服务端关键代码如下:MyServer与上一个例子中的TestServer 差多,这里只列举不同的 ...

  4. hive单机部署

    hadoop,hbase,zookeeper安装好了,现在来安装hive hadoop 版本:2.8.4 hbase 版本:2.1.3 hive 版本:2.3.4 zookeeper 版本:3.4.1 ...

  5. Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader

    Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader ...

  6. netstat Recv-Q和Send-Q判断包在哪端

    通过netstat -anp可以查看机器的当前连接状态:   Active Internet connections (servers and established) Proto Recv-Q Se ...

  7. 【ORACLE语句备份】数据库表同步 ——定时任务管理器(EXPDP导出,IMPDP导入)

    1.C:\Users\Administrator>sqlplus sys/xxx@xxx as sysdba; 2.SQL> create directory dbbak4 as 'e:\ ...

  8. 题解 [HNOI2004]宠物收养场

    解析 这题似乎是裸的平衡树\(+\)模拟...于是用\(treap\)写了个板子. 看上去,我们似乎要维护两颗树(宠物和顾客), 然而,注意到,同一时间宠物点只有一类人(或物qwq), 所以,只要判断 ...

  9. SSH整合框架

    实现登录.新闻增删改查.树形菜单 引入pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  10. 1012 最大公约数和最小公倍数问题 2001年NOIP全国联赛普及组

    1012 最大公约数和最小公倍数问题 2001年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 输入二个 ...