coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
来源:互联网 发布:崩坏3矩阵神格buff 编辑:程序博客网 时间:2024/05/14 20:51
You are given a permutation p of lengthn. 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.
The first line contains two integers n andm (1 ≤ n, m ≤ 3·105) — the length of the permutationp and the number of foe pairs.
The second line contains n distinct integerspi (1 ≤ pi ≤ n) — the elements of the permutationp.
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.
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 thelong long integer type and in Java you can use long integer type.
4 21 3 2 43 22 4
5
9 59 7 2 3 1 4 6 5 81 64 52 77 22 7
20
输入:n,m。n代表输入数组范围,m代表pair的组数。下面1行有n个,表示排列中的数。接下来m行输入foe的值
输出:输出不包含foe组合的区间总数,可能会超int。
解法:对于输入的数组,我们可以用pos[i]来保存i的位置,对于任意一个位置,我们可以定义mark[i]为以位置i结尾的不能取到的区间数。根据这个我们可以推出mark[i]=max(mark[i],l),l表示foe的左边界。然后如果没有不可以取得pair的话,以i结尾的能取的组数为i。所以答案的总数为:i-mark[i]的总和。
AC:
#include <string>
#include <iostream>
#include <string.h>
#include<stdio.h>
using namespace std;
#define maxn 1000100
#define ll __int64
int mark[maxn],pos[maxn];
int main()
{
int n,m,v,x,y,l,r;
ll ans=0;
cin>>n>>m;
memset(mark,0,sizeof(mark));
for(int i=1;i<=n;i++)
{
scanf("%d",&v);
pos[v]=i;
}
while(m--)
{
scanf("%d%d",&x,&y);
l=pos[x],r=pos[y];
if(l>r) swap(l,r);
mark[r]=max(mark[r],l);
}
mark[0]=0;
for(int i=1;i<=n;i++)
{
mark[i]=max(mark[i],mark[i-1]);
ans+=(i-mark[i]);
}
cout<<ans<<endl;
}
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10C Foe Pairs
- 【Educational Codeforces Round 10C】【脑洞 SET】Foe Pairs 不含有敌对pair的区间数
- Educational Codeforces Round 10 C. Foe Pairs —— 后缀和
- Educational Codeforces Round 12(C)贪心
- Codeforences Educational Round10 C. Foe Pairs
- 【Codeforces 652 C Foe Pairs 】
- Codeforces Educational Codeforces Round 3 C. Load Balancing(贪心)
- Educational Codeforces Round 2C. Make Palindrome(贪心构造)
- Educational Codeforces Round 6(C)尺取法+贪心
- Codeforces 652C Foe Pairs 【dp】
- Codeforces 652C Foe Pairs 【dp】
- CodeForces 652C Foe Pairs(思维)
- Educational Codeforces Round 5(B)贪心
- Java多线程之volatile变量
- 【BZOJ2157】旅游,树链剖分练习
- NSUserDefaults
- 产品面试指南
- redis windows 官方下载
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- myeclipse 启动时 server Could not create the view: An unexpected exception was thrown.
- linux文件系统简介
- LeetCode笔记:104.Maximum Depth of Binary Tree
- tomcat 404错误
- Android 第七天重置版_BroadCastReceiver
- 第一次开通博客,见证你我技术成长之路
- SDWebImage 原理及使用
- Shell脚本中执行sql语句,操作mysql数据库