Http://poj.org/problem? Id = 2823
Basic monotonous queue. The deque of STL is actually TLE !!! Whether C ++ or G ++...
The array is used to submit C ++ AC. Submitting g ++ TLE doesn't understand why.
Heap operation: first, perform operations on the tail. Take a small root heap as an example: When an element is added from the tail, check whether there is a bigger one than the former, and delete the former, this element moves forward until it is no larger than it, and is directly inserted if it is not;
Then, perform operations on the header of the queue: Check whether the subscript is within the specified range. If the subscript is not within the specified range, delete it. The first result is the current result.
Take this topic as an example to learn more about this process.
1 3 -1 -3 5 3 6 7
Queue output
1
1 3
-1 (1 and 3 are larger than-1, So delete)-1
-3-3
-3 5-3
-3 3-3
3 6 (delete because the subscript of-3 has exceeded the range) 3
3 6 7 3
**************************************** ****** **************************************** *************************
AC code:
#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);}#define LL long long#define PI acos(-1.0)#define N 1000010#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-8#define FRE freopen("a.txt","r",stdin)int Minq[N],Maxq[N];int index[N],que[N];int a[N];int n,m;void Min(){ int i,j,k; int head=1,tail=0; for(i=1;i<=n;i++){ while(head<=tail && que[tail]>a[i]) tail--; tail++; que[tail]=a[i]; index[tail]=i; if(i>=m){ while(index[head]<i-m+1) head++; Minq[i-m]=que[head]; } }}void Max(){ int i,j,k; int head=1,tail=0; for(i=1;i<=n;i++){ while(head<=tail && que[tail]<a[i]) tail--; tail++; que[tail]=a[i]; index[tail]=i; if(i>=m){ while(index[head]<i-m+1) head++; Maxq[i-m]=que[head]; } }}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ int i,j,k; for(i=1;i<=n;i++) scanf("%d",&a[i]); Min(); printf("%d",Minq[0]); for(i=1;i<n-m+1;i++)printf(" %d",Minq[i]); printf("\n"); Max(); printf("%d",Maxq[0]); for(i=1;i<n-m+1;i++)printf(" %d",Maxq[i]); printf("\n"); } return 0;}
Tle code:
#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 1000010#define INF INT_MAX#define eps 1e-8#define FRE freopen("a.txt","r",stdin)struct node{ int num; int id;};deque<node> dmin,dmax;int ans[N];int main(){ //FRE; int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int i,j; int cnt=1; dmin.clear(); dmax.clear(); node p; int tot=0; while(n--) { scanf("%d",&p.num); while(!dmin.empty() && dmin.front().id<cnt-m+1)dmin.pop_front(); while(!dmin.empty() && dmin.back().num>p.num)dmin.pop_back(); while(!dmax.empty() && dmax.front().id<cnt-m+1)dmax.pop_front(); while(!dmax.empty() && dmax.back().num<p.num)dmax.pop_back(); p.id=cnt++; dmin.push_back(p); dmax.push_back(p); if(cnt>m) { a[tot++]=dmax.front().num; printf("%d ",dmin.front().num); } } printf("\n"); for(i=0;i<tot;i++)printf("%d ",a[i]); printf("\n"); } return 0;}