Codeforces 652C Foe Pairs 【dp】
2016-04-13 22:06
405 查看
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a permutation p of length n. Also you are given m foe pairs (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi). Your task is to count the number of different intervals (x, y) (1 ≤ x ≤ y ≤ n) that do not contain any foe pairs. So you shouldn't count intervals (x, y) that contain at least one foe pair in it (the positions and order of the values from the foe pair are not important). Consider some example: p = [1, 3, 2, 4] and foe pairs are {(3, 2), (4, 2)}. The interval (1, 3) is incorrect because it contains a foe pair (3, 2). The interval (1, 4) is also incorrect because it contains two foe pairs (3, 2) and (4, 2). But the interval (1, 2) is correct because it doesn't contain any foe pair. Input The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the length of the permutation p and the number of foe pairs. The second line contains n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p. Each of the next m lines contains two integers (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi) — the i-th foe pair. Note a foe pair can appear multiple times in the given list. Output Print the only integer c — the number of different intervals (x, y) that does not contain any foe pairs. Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. Examples Input 4 2 1 3 2 4 3 2 2 4 Output 5 Input 9 5 9 7 2 3 1 4 6 5 8 1 6 4 5 2 7 7 2 2 7 Output 20 Note In the first example the intervals from the answer are (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).
题意:给你1-n的一个排列和m组数对,问有多少区间不包含任意一个数对。
思路:倒着做,用总方案数去掉那些不符合的就是结果。先预处理数对的区间[l[i],r[i]] (1<=i<=m),考虑以第i个数开始的区间,我们需要的信息的是min(r[j]) l[j]>=i,这样不合法的区间数有n−dp[i]+1。
用dp[i]=min(r[j]) l[j]>=i,这样我们到着做一遍就把dp全求出来了。
dp[i]表示i到右边最近一个与它可构成数对的那个数的位置。
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=3e5+5; int f ,pos ; int main() { int n,m; scanf("%d%d",&n,&m); int u,v; for(int i=1;i<=n;i++){ scanf("%d",&v); pos[v]=i;f[i]=n+1; } ll ans=1ll*n*(n-1)/2+n; for(int i=0;i<m;i++){ scanf("%d%d",&u,&v); if(pos[u]>pos[v])swap(u,v); f[pos[u]]=min(f[pos[u]],pos[v]); } for(int i=n-1;i>=1;i--)f[i]=min(f[i],f[i+1]); for(int i=1;i<n;i++){ ans-=(n-f[i]+1); } cout<<ans; return 0; }
相关文章推荐
- Aizu 2200 Mr. Rito Post Office【特复杂的floyd+dp】
- 220. Contains Duplicate III set的应用
- bailian--oj--2757(最长上升子序列)
- sleep和wait的区别
- POJ-1691 Painting A Board
- inflater.inflate(R.layout.main_tab_01, null)与inflater.inflate(R.layout.main_tab_01, container, false
- leetcode之Contains Duplicate II
- 使用UltraISO(软碟通)制作U盘启动盘完整教程
- mybaits 一对多 关联查询
- 错误:Command /usr/bin/codesign failed with exit code 1
- leetcode——11——Container With Most Water
- 错误: error linker command failed with exit code 1 (use –v to see invocation)
- drupal behavior good explain
- HDU 5228 ZCC loves straight flush 暴力
- Kafka设计解析(三) : Kafka High Availability (下)
- Kafka设计解析(二):Kafka High Availability (上)
- opencv cvcopy() error :assertion faild (src.depth()==dst.depth()&&src.size==dst.size)
- Aizu 2249 Road Construction【最短路变形】
- char *p[]={"Shanghai","Beijing","Honkong"};则结果为j字符的表达式是
- leetCode(28):Contains Duplicate II