您的位置:首页 > 其它

ural 1126【单调队列基础】

2011-08-06 15:16 453 查看
http://acm.timus.ru/problem.aspx?space=1&num=1126

虽说是简单的单调队列,但是足足折磨了我大半天!因为没接触过单调队列,所以刚开始时束手无策,可以用线段树,但你知道线段树的代码量。。。

题意是给出一个数列,给出一个n,问你每n个连续的数中最大的是多少。

具体做法参考我博客这里

代码实现:本人用了STL的deque

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <limits.h>

using namespace std;

int lowbit(int t){return t&(-t);}
int countbit(int t){return (t==0)?0:(1+countbit(t&(t-1)));}
int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);}
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a>b?b:a;}
#define LL __int64
#define pi acos(-1)
#define N  100010
#define INF INT_MAX
#define eps 1e-8
#define FRE freopen("a.txt","r",stdin)
struct node
{
int num;
int id;
};
deque<node> de;
int main()
{
// FRE;
int n;
scanf("%d",&n);
int cnt=1;
node p;
de.clear();
while(scanf("%d",&p.num)&&p.num!=-1)
{
p.id=cnt;
while(!de.empty()&&de.front().id<cnt-n+1 )
{de.pop_front();}
while(!de.empty()&&de.back().num<p.num)
{de.pop_back();}
cnt++;
de.push_back(p);
if(cnt>n)
printf("%d\n",de.front().num);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: