cf#ecr10-C. Foe Pairs- two pointers
来源:互联网 发布:萧山网络问政供电局 编辑:程序博客网 时间:2024/05/18 21:09
http://codeforces.com/contest/652/problem/C
给n个数的数组, n=3e5
给m个敌对pair,
让你选区间,一个合法区间不能包括任何一对敌对pair
首先我们把a[i]和i映射起来先。。
初始化一个end【】数组为0,记录的是第i个数为右端点的最大的敌对左端点(如果end[i]=0表示不存在一个敌对左端点)
然后遍历m对pair,对于每一对pair,找到在原始数组的位置,称为x,y
那么我们就更新 end[y]=x,如果end[y]已存在,则选一个大的 (x大可以包含小较小的情况)
预处理完之后。。
遍历i=1到n,设立flag=1;
如果【end[y]>=flag,则flag更新为end[i]+1,表示当前可以选的最远的左端点,因为如果选到flag左边的点,那么就会选到之前的敌对pair】
对每个位置,如果end[i]为0,则表示这个点没有敌对左端点,
则以i为右端点的合法区间,可以选 i-flag+1个点作为左端点,ans+=i-flag+1
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <map>#include <set>#include <vector>#include <iostream>using namespace std;const double pi=acos(-1.0);double eps=0.000001; int END[3*100005];int tm[3*100005]; int max(int a,int b){return a>b?a:b;}int main(){int x,y,i;int m;int n;cin>>n>>m;for (i=1;i<=n;i++){scanf("%d",&x);tm[x]=i;} __int64 ans=0; for (i=1;i<=m;i++){scanf("%d%d",&x,&y);x=tm[x];y=tm[y];if (y<x) swap(x,y); if (!END[y]) END[y]=x;else END[y]=max(END[y],x);} int st=1;for (i=1;i<=n;i++){if (END[i]&&END[i]>=st)st=END[i]+1;ans+=i-st+1;}printf("%I64d\n",ans); printf("\n");return 0;}
0 0
- cf#ecr10-C. Foe Pairs- two pointers
- C. Foe Pairs
- C. Foe Pairs
- 【Codeforces 652 C Foe Pairs 】
- Codeforences Educational Round10 C. Foe Pairs
- Code Forces 652C Foe Pairs
- Codeforces 652C Foe Pairs 【dp】
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Codeforces 652C Foe Pairs 【dp】
- Educational Codeforces Round 10 C. Foe Pairs
- CodeForces 652C Foe Pairs(思维)
- Educational Codeforces Round 10C Foe Pairs
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- codeforces_652C. Foe Pairs
- 【Educational Codeforces Round 10C】【脑洞 SET】Foe Pairs 不含有敌对pair的区间数
- Codeforces 652C Foe Pairs【二分+RMQ】好像这题Dp做法很多啊
- android:exported 属性详解
- HD 2048 数塔 DP(简单递推)
- C++primer第九章习题解答
- ActiveMQ 的使用
- c++实现归并排序
- cf#ecr10-C. Foe Pairs- two pointers
- 关于C++ 引用<初学写代码小记>
- C语言程序设计,实验报告程序
- 码农小汪-struts2学习8-Action中获取ActionContext,ServletActionContext,HttpServletRequest......
- ZooKeeper Watch Java API浅析getChildren
- 仿微信图片多选
- git删除未跟踪文件
- Kafka分布式消息系统
- rz: command not found