问题重述

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. PHP程序员要掌握的技能

    1. Composer 第一点就要提 Composer ,自从 Composer 出现后,PHP 的依赖管理可以变得非常简单.程序内依赖一些类库和框架,直接使用 Composer 引入即可,通过使用 ...

  2. python小练手题1

    1. """ Write a program which can compute the factorial of a given numbers. The result ...

  3. iOS View的一些操作定义为宏

    #define ViewOf(__View__,__TAG__) [__View__ viewWithTag:__TAG__]#define LabelOf(__View__,__TAG__) ((U ...

  4. JMeter性能测试,完整入门篇(转)

    原文转自:https://blog.csdn.net/lovesoo/article/details/78579547 Apache JMeter是一款纯java编写负载功能测试和性能测试开源工具软件 ...

  5. RobotFramework 截取中文中的数字比较时长

    先看下需求,这个报表中有个时长,需要对昨日和前日的时长进行比较,我们获取到的元素是例如“9分43秒”这样的格式 1.首先要讲中文中的分和秒分别提取出来 提取python代码如下: import res ...

  6. Windows系统下载地址

    地址: https://msdn.itellyou.cn/ 里面给出的是迅雷下载链接,请提前安装好迅雷

  7. 基于node.js的websocket 前后端交互小功能

    一.node var ws = require("nodejs-websocket"); console.log("开始建立连接...") var server ...

  8. Linux下nc或scp命令来实现文件传输

    很实用的小技巧, 可以使用nc或者是scp nc命令,转载自:https://www.cnblogs.com/xuybin/archive/2013/09/27/3343098.html 发送端:ca ...

  9. phpstorm快捷键和激活

    点击PHP中文网->PHPstorm激活, 按照步骤激活 今天遇到了不能激活的情况, your activation code could not be validated.这个解决方法是把上面 ...

  10. App自动化测试介绍