Codeforces 652C Foe Pairs 【dp】
2016-03-29 12:10
656 查看
题目链接:Codeforces 652C Foe Pairs
C. Foe Pairs
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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全求出来了。
AC代码:
C. Foe Pairs
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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全求出来了。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #include <stack> #define PI acos(-1.0) #define CLR(a, b) memset(a, (b), sizeof(a)) #define fi first #define se second #define ll o<<1 #define rr o<<1|1 using namespace std; typedef long long LL; typedef pair<int, int> pii; const int MAXN = 3*1e5 + 10000; const int pN = 1e6;// <= 10^7 const int INF = 0x3f3f3f3f; const int MOD = 2009; void add(LL &x, LL y) { x += y; x %= MOD; } int pos[MAXN], p[MAXN]; int dp[MAXN]; int n, m; int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { int v; scanf("%d", &v); pos[v] = i; dp[i] = INF; } LL ans = 1LL * n * (n - 1) / 2 + n; for(int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); if(pos[u] > pos[v]) { swap(u, v); } dp[pos[u]] = min(dp[pos[u]], pos[v]); } for(int i = n-1; i >= 1; i--) { dp[i] = min(dp[i], dp[i+1]); //cout << dp[i] << endl; } for(int i = 1; i < n; i++) { if(dp[i] == INF) continue; ans -= (n - dp[i] + 1); } printf("%lld\n", ans); return 0; }
相关文章推荐
- 【redis】WRONGTYPE Operation against a key holding
- Hive安装报错:Terminal initialization failed; falling back to unsupported
- NSLayoutConstraint
- CDC的四个派生类CClientDC,CPaintDC,CWindowDC,CMetaFileD
- LeetCode 219 Contains Duplicate II
- CodeIgniter基于Email类发邮件的方法
- 用Paint Tool SAI绘制漫画
- svn cleanup failed–previous operation has not finished; run cleanup if it was in
- failed to lazily initialize a collection of role
- Ubuntu raid5+lvm实验
- leetcode 11.Container With Most Water
- mybaits generator自动生成model、xml、dao
- How to enable time stamps for logs in 'dmesg' on Gaia and SecurePlatform
- 设计模式 ( 十二 ) 职责链模式(Chain of Responsibility)(对象行为)
- C++中placement new操作符(经典) http://blog.csdn.net/zhangxinrun/article/details/5940019
- Failure[INSTALL_FAILED_INSUFFICIENT_STORAGE]调试问题
- django种表单post出现CSRF verification failed( CSRF验证失败 ) 的两种解决方式
- Code Forces 652C Foe Pairs
- Code Forces 652C Foe Pairs
- 关于 error: LNK1123: failure during conversion to COFF: file invalid or corrupt 错误的解决