只要计算每个位置最多能到哪个位置,累加即可,DP从后往前预处理一下每个位置到达的最远位置。

有坑点:输入的时候如果同一个点出发的,需要保存最小值。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;const int maxn=4*100000+10;int n,m;
int a[maxn],b[maxn];
int p[maxn];int dp[maxn];int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&a[i]);p[a[i]]=i;}for(int i=1;i<=n;i++) b[i]=0x7FFFFFFF;for(int i=1;i<=m;i++){int li,ri;scanf("%d%d",&li,&ri);int fx=p[li];int fy=p[ri];if(fx>fy) swap(fx,fy);b[fx]=min(b[fx],fy);}dp[n]=b[n];for(int i=n-1;i>=1;i--){if(b[i]==0x7FFFFFFF) dp[i]=min(dp[i+1],b[i]);else dp[i]=min(dp[i+1],b[i]-1);}long long ans=0;for(int i=1;i<=n;i++){if(dp[i]==0x7FFFFFFF) ans=ans+(long long)(n-i+1);else ans=ans+(long long)(dp[i]-i+1);}printf("%lld\n",ans);return 0;
}

转载于:https://www.cnblogs.com/zufezzt/p/5322297.html

CodeForces 652C Foe Pairs相关推荐

  1. Codeforces 652C Foe Pairs 【dp】

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. CodeForces - 652C Foe Pairs

    思路:记录下来每一个数在序列里的位置,然后记录每一个位置向右最远能到达哪个位置 #include <stdio.h> #include <algorithm>using nam ...

  3. CodeForces 652C Foe Pairs(思维)

    题意:给你1-n的一个排列和m对关系,问有多少区间不包含任意一个关系. 思路:用一个数组表示每个数字可以向右延生的最大长度,也就是右边哪些点可以和这个数字形成一个区间. 注意:在给定关系,更新完数组之 ...

  4. Code Forces 652C Foe Pairs

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Educational Codeforces Round 10 C. Foe Pairs —— 后缀和

    题目链接:http://codeforces.com/problemset/problem/652/C C. Foe Pairs time limit per test 1 second memory ...

  6. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

  7. Educational Codeforces Round 10 C. Foe Pairs

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  8. 【Codeforces 652 C Foe Pairs 】

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

最新文章

  1. TestLink1.9.3测试用例:Excel转换XML工具一
  2. html的marquee滚动标签
  3. Q2财报凸显“服务”,“巨人”百度正在转身
  4. iPhone 的 Push(推送通知)功能原理浅析
  5. Python动态导入模块、类
  6. C++多重继承时调用相应的父类函数
  7. 怎么选择跨境电商ERP系统?
  8. 二层交换:网桥(交换机)
  9. 读书笔记-----Oracle字符处理函数列表
  10. 用计算机关闭无线网络连接,我的DELL笔记本电脑无线网络已关闭,怎么进行再连接?...
  11. 苹果处理器性能排行榜天梯图2022 苹果处理器排行榜2022
  12. LINUX 用户 及 组
  13. 亚洲杯:国足告别阿联酋
  14. python代码雨_教你用200行Python代码“换脸”
  15. require.js的简单使用
  16. Tensorflow常用函数汇总
  17. linux怎么读取群辉数据,Linux下读取群晖SHR/RAID硬盘组 Data Recovering of Synology DSM SHR/RAID Volume...
  18. 物联网平台分为几层,你了解吗
  19. 罗技 UE3100 蓝牙耳机使用
  20. 《西游记》中真假美猴王的分析

热门文章

  1. 计算机毕业设计(附源码)python智慧小区团购系统
  2. 计算机专业考教师资格证,计算机专业考教师资格证什么学科对口
  3. Java基础篇之什么是BufferedReader
  4. 整理房间真的会让你更快乐吗?
  5. AWE2019厨电成最大发力点?肩负的责任变大了
  6. 基于RFID的模拟停车场管理系统
  7. 【数学】柏拉图多面体只有5种的证明
  8. 【学习笔记】C++语言程序设计(郑莉):数据的共享与保护
  9. Docker部署mqtt服务器(emqx),密码认证
  10. ubuntu安装extundelete