CodeForces 652C Foe Pairs
只要计算每个位置最多能到哪个位置,累加即可,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相关推荐
- Codeforces 652C Foe Pairs 【dp】
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- CodeForces - 652C Foe Pairs
思路:记录下来每一个数在序列里的位置,然后记录每一个位置向右最远能到达哪个位置 #include <stdio.h> #include <algorithm>using nam ...
- CodeForces 652C Foe Pairs(思维)
题意:给你1-n的一个排列和m对关系,问有多少区间不包含任意一个关系. 思路:用一个数组表示每个数字可以向右延生的最大长度,也就是右边哪些点可以和这个数字形成一个区间. 注意:在给定关系,更新完数组之 ...
- Code Forces 652C Foe Pairs
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Educational Codeforces Round 10 C. Foe Pairs —— 后缀和
题目链接:http://codeforces.com/problemset/problem/652/C C. Foe Pairs time limit per test 1 second memory ...
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- 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 ...
- 【Codeforces 652 C Foe Pairs 】
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 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 ...
最新文章
- TestLink1.9.3测试用例:Excel转换XML工具一
- html的marquee滚动标签
- Q2财报凸显“服务”,“巨人”百度正在转身
- iPhone 的 Push(推送通知)功能原理浅析
- Python动态导入模块、类
- C++多重继承时调用相应的父类函数
- 怎么选择跨境电商ERP系统?
- 二层交换:网桥(交换机)
- 读书笔记-----Oracle字符处理函数列表
- 用计算机关闭无线网络连接,我的DELL笔记本电脑无线网络已关闭,怎么进行再连接?...
- 苹果处理器性能排行榜天梯图2022 苹果处理器排行榜2022
- LINUX 用户 及 组
- 亚洲杯:国足告别阿联酋
- python代码雨_教你用200行Python代码“换脸”
- require.js的简单使用
- Tensorflow常用函数汇总
- linux怎么读取群辉数据,Linux下读取群晖SHR/RAID硬盘组 Data Recovering of Synology DSM SHR/RAID Volume...
- 物联网平台分为几层,你了解吗
- 罗技 UE3100 蓝牙耳机使用
- 《西游记》中真假美猴王的分析