单调队列1001 HDU 3706 Second My Problem First 单调队列入门题
2016-06-21 21:52
309 查看
Problem Description
Give you three integers n, A and B.
Then we define Si = Ai mod B and Ti = Min{ Sk | i-A <= k <= i, k >= 1}
Your task is to calculate the product of Ti (1 <= i <= n) mod B.
题意:
给n个数,第i个数是A^i%B,求sigma(T[i])
T[i]=min(Val[j])|j>=1&&j<=i&&j>=i-A
思路:
维护一个单调队列的入门题
单调队列是一个同时维护头指针和尾指针的双端队列
假设我们维护一个单调递减的单调队列
必须保证越靠近头指针越小
如果此时我们插入一个数x,我们从尾指针到头指针遍历
直到队列为空或者有个数小于x,如果遍历的时候不满足前边的条件
那么把这个队列里的元素扔出队列
由于我们每个元素只入出队列各一次,所以总是时间复杂度是O(n)的
而且单调队列中,入队时间永远是队头>=队尾
神奇的数据结构…ORZ
Give you three integers n, A and B.
Then we define Si = Ai mod B and Ti = Min{ Sk | i-A <= k <= i, k >= 1}
Your task is to calculate the product of Ti (1 <= i <= n) mod B.
题意:
给n个数,第i个数是A^i%B,求sigma(T[i])
T[i]=min(Val[j])|j>=1&&j<=i&&j>=i-A
思路:
维护一个单调队列的入门题
单调队列是一个同时维护头指针和尾指针的双端队列
假设我们维护一个单调递减的单调队列
必须保证越靠近头指针越小
如果此时我们插入一个数x,我们从尾指针到头指针遍历
直到队列为空或者有个数小于x,如果遍历的时候不满足前边的条件
那么把这个队列里的元素扔出队列
由于我们每个元素只入出队列各一次,所以总是时间复杂度是O(n)的
而且单调队列中,入队时间永远是队头>=队尾
神奇的数据结构…ORZ
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<queue> #include<stack> #include<string> #include<vector> #include<map> #include<set> using namespace std; #define lowbit(x) (x&(-x)) typedef long long LL; const int maxn = 100005; const int inf=(1<<28)-1; deque<pair<LL,int> >Que; int main() { int n,A,mod; while(~scanf("%d%d%d",&n,&A,&mod)) { Que.clear(); LL tmp=1,ans=1; for(int i=1;i<=n;++i) { tmp=(tmp*A)%mod; while(Que.empty()== 0 && tmp<=Que.back().first) Que.pop_back(); Que.push_back(make_pair(tmp,i)); while(Que.empty()== 0 &&i-Que.front().second>A) Que.pop_front(); ans=(ans*Que.front().first)%mod; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 浅谈单调队列、单调栈
- sjtu online judge 1034 二哥的金链
- poj2823 单调队列 ------scanf/printf/G++/C++
- [BZOJ1499][NOI2005][DP+优化]瑰丽华尔兹
- hdu4193 hoj3107
- HDU3530
- 【bzoj1047】【单调队列】【HAOI2007】理想的正方形
- poj2823(单调队列入门)
- 2015 Multi-University Training Contest 1 Hdu 5289 Assignment
- HDU 3415 (单调队列优化DP)
- 【Monotonic-queue】【dp】【Segment-tree】【STL】Codeforces 487B - Strip
- poj2823Sliding Window
- bzoj 1717: [Usaco2006 Dec] Milk Patterns
- HDU 4193 Non-negative Partial Sums 【单调队列】
- 【Jason's_ACM_解题报告】Defense Lines
- poj 3415 后缀数组+单调队列
- poj 1821 dp+单调队列
- zoj 3699 单调队列
- 单调队列的应用
- 单调队列学习