C. Foe Pairs
来源:互联网 发布:mysql 高性能集群 编辑:程序博客网 时间:2024/05/18 17:42
这个题主要是单调栈的思想,dp表示的是我当前这一位向后最多能取到哪里。
首先每一位初始化为n+1;
然后根据限制更新,假设x在前,y在后,这里x,y表示位置
则x的区间就更新为min(dp[x],y);
然后从后到前更新整个区间的限制,就能求出来了。
具体的看代码,前面的就是一堆头文件和读入优化,,可以忽略
#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>#include <map>#define FOR(i,a,b) for(int i=a;i<=b;i++)#define ROF(i,a,b) for(int i=a;i>=b;i--)#define mem(i,a) memset(i,a,sizeof(i))#define rson mid+1,r,rt<<1|1#define lson l,mid,rt<<1#define ll long long#define LL long longusing namespace std;const double eps = 0.0000001;const int maxn = 1e6+7;const int mod = 1e9+7;template <typename T>inline void read(T &_x_){ _x_=0;bool f=false;char ch=getchar(); while (ch<'0'||ch>'9') {if (ch=='-') f=!f;ch=getchar();} while ('0'<=ch&&ch<='9') {_x_=_x_*10+ch-'0';ch=getchar();} if(f) _x_=-_x_;}int n,m,x,y;int dp[maxn],pos[maxn];int main(){ read(n);read(m); FOR(i,1,n)scanf("%d",&x),pos[x]=i,dp[i]=n+1; FOR(i,1,m){ read(x),read(y); if(pos[x] > pos[y]) swap(x,y); dp[pos[x]] = min(dp[pos[x]],pos[y]); } ROF(i,n-1,1) dp[i]=min(dp[i],dp[i+1]); ll ans=0; FOR(i,1,n) ans+=(dp[i]-i); printf("%I64d\n",ans); return 0;}
阅读全文
0 0
- C. Foe Pairs
- C. Foe Pairs
- 【Codeforces 652 C Foe Pairs 】
- cf#ecr10-C. Foe Pairs- two pointers
- 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做法很多啊
- 【机器学习】Goldstein-Armijo line-search
- JavaScript执行效率小结
- vue项目开发环境篇一
- QSemaphore、QWaitCondition实现线程同步
- 在java中获取变量的类型
- C. Foe Pairs
- Vue.js+Webpack+Sass+Jade(pug)网站构建
- 浅析面对对象 六大原则(六)
- 生产开启特定Debug日志
- sql语句中GROUP BY 和 HAVING的使用 count()
- SQL语句优化
- 出差归来,谈谈出差的想法(一)。
- JavaWeb学习之Servlet(一)----MyEclipse及Tomcat的配置
- 信号处理国际会议排名