#include<bits/stdc++.h>
using namespace std;
int a[300007],b[500007],c[500007];
set<int>st[300007];
vector<int>v;
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        v.push_back(a[i]);
    }
    int cnt=0;
    for(int i=1;i<=m;i++){
        scanf("%d%d",&b[i],&c[i]);
        st[b[i]].insert(c[i]);
    }
    v.pop_back();
    int ans=0;
    for(int i=(int)v.size()-1;i>=0;i--){//从后向前是为了保证只要i>=0就可以循环下去,不会使循环跑不完就停止
        int tmp=v[i];
        for(int j=i+1;j<v.size();j++){//从前向后换,可以让a[n]先和原先靠前的换后再和原先靠后的换,从而换的更远
            if(st[tmp].count(v[j])){
                swap(v[j],v[j-1]);
            }
            else
                break;
        }
        if(st[v.back()].count(a[n]))//队尾可以换到a[n]后面的话,彻底删除他
            v.pop_back();
    }
    ans=n-v.size()-1;
    printf("%d",ans);
    return 0;
}

Codeforces Round #546 (Div. 2)D(贪心,思维,SET,VECTOR,模拟)的更多相关文章

  1. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  2. Codeforces Round #546 (Div. 2) 题解

    Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...

  3. Codeforces Round #547 (Div. 3) F 贪心 + 离散化

    https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...

  4. Codeforces Round #595 (Div. 3)D1D2 贪心 STL

    一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...

  5. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  6. Codeforces Round #303 (Div. 2) D 贪心

    D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  7. Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】

    A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  8. Codeforces Round #546 (Div. 2)-D - Nastya Is Buying Lunch

    这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...

  9. Codeforces Round #545 (Div. 2) D 贪心 + kmp

    https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...

随机推荐

  1. 3 ignite windows 上安装

    实在懒得写了,  基本上按照官网的操作就行,很简单 https://apacheignite.readme.io/docs/getting-started

  2. Javascript 推荐一个图形化展示库

    觉得这个库不错: http://almende.github.io/chap-links-library/index.html

  3. String to Integer (atoi) ???

    #define INT_MAX 2147483647 #define INT_MIN -2147483648 class Solution { public: int atoi(const char ...

  4. python's fourteenth day for me 内置函数

    locals:  函数会以字典的类型返回当前位置的全部局部变量. globals:  函数会以字典的了类型返回全部的全局变量. a = def func(): b = print(locals()) ...

  5. python对裤子进行一个查询

    前言: 获取到一个数据库,使用python对其 进简单的查询 代码: import time print('开房记录查询') def chax(): g=input('请输入要查询的>>& ...

  6. Python Twisted系列教程5:由Twisted支持的诗歌客户端

    作者:dave@http://krondo.com/twistier-poetry/  译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列 抽象地构建客户端 在第四部分中,我们构建了第一个使用 ...

  7. openGL 纹理05

    纹理(Texture) 为了能够把纹理映射(Map)到三角形上,我们需要指定三角形的每个顶点各自对应纹理的哪个部分. 这样每个顶点就会关联着一个纹理坐标(Texture Coordinate) 用来标 ...

  8. FatMouse' Trade(Hdu 1009)

    Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wareho ...

  9. Codeforces 919F——A Game With Numbers

    转自大佬博客:https://www.cnblogs.com/NaVi-Awson/p/8405966.html; 题意 两个人 Van♂ 游戏,每人手上各有 8'>88 张牌,牌上数字均为 [ ...

  10. PrimeNG01 angular集成PrimeNG

    1 开发环境 本博文基于angular5 2 步骤 2.1 创建angular5项目 详情参见百度 2.2 下载PrimeNG依赖 npm install primeng --save npm ins ...