POJ 2823 Sliding Window(单调队列)

题目链接

貌似POJ挂了,交题 一直RE。。。我去别的OJ交AC了。当做模版了,查了好几遍,还以为模版敲错了呢。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #define N 1000005

 5 int p[N],que[6*N];

 6 int main()

 7 {

 8     int n,m,i,str,end;

 9     scanf("%d%d",&n,&m);

10     for(i = 1;i <= n;i ++)

11     scanf("%d",&p[i]);

12     str = end = 0;

13     for(i = 1;i <= n;i ++)

14     {

15         while(str < end&&p[i] <= p[que[end-1]])

16         end --;

17         que[end++] = i;

18         if(i - que[str] >= m)

19         str ++ ;

20         if(i == m)

21         printf("%d",p[que[str]]);

22         else if(i > m)

23         printf(" %d",p[que[str]]);

24     }

25     printf("\n");

26     str = end = 0;

27     for(i = 1;i <= n;i ++)

28     {

29         while(str < end&&p[i] >= p[que[end-1]])

30         end --;

31         que[end++] = i;

32         if(i - que[str] >= m)

33         str ++ ;

34         if(i == m)

35         printf("%d",p[que[str]]);

36         else if(i > m)

37         printf(" %d",p[que[str]]);

38     }

39     printf("\n");

40     return 0;

41 }

 

你可能感兴趣的:(window)