zoukankan      html  css  js  c++  java
  • poj 2823 Sliding Window (STL超时版)

    http://poj.org/problem?id=2823

      五一假期最大的收获不是学会了什么算法,而是学会了用STL(Standard Template Library)中的set, priority_queue, pair, map等的基本操作。

      看到priority_queue的时候就让我想起单调队列,想起了poj 2823这题。于是,我就拿这题来做我的STL练习工具,写了一个用STL完成的单调队列。本地测试通过,理论上也是不会出错的,可惜的是,提交上去因为STL的处理方式以及题目的输出格式,使得我每个case的常数太大了,所以最终超时结束。

      昨晚跟一位师兄讨论STL相关问题的时候,他说了,国内的OJ很多都会卡STL,poj尤其严重,今天总算遇见到了。

      好吧,我还是上我的STL练习代码吧~虽然是超时了,但是我觉得对我的学习是有帮助的,也因此保存!

      哪位神牛知道怎么用priority_queue可以不超时的?诚心请教!

    View Code
     1 #include "cstdio"
     2 #include "vector"
     3 #include "algorithm"
     4 #include "queue"
     5 using namespace std;
     6 
     7 struct temp{
     8     int p;
     9     int v;
    10     temp(){
    11     }
    12     temp(int pos, int val):
    13         p(pos), v(val){
    14     }
    15     bool operator <(const temp x) const{
    16         return v < x.v;
    17     }
    18     bool operator ==(const temp x) const{
    19         return v == x.v;
    20     }
    21     bool operator >(const temp x) const{
    22         return v >x.v;
    23     }
    24 };
    25 
    26 int in[1000005];
    27 priority_queue< temp, vector<temp>, less<temp> > T;
    28 priority_queue< temp, vector<temp>, greater<temp> > S;
    29 
    30 int main(){
    31     int n, k;
    32 
    33     while (~scanf("%d%d", &n, &k)){
    34         temp a;
    35 
    36         for (int i=1; i<=n; i++)
    37             scanf("%d", &in[i]);
    38         for (int i=1; i<k; i++){
    39             a=temp(i, in[i]);
    40             S.push(a);
    41         }
    42         for (int i=k; i<=n; i++){
    43             a=temp(i, in[i]);
    44             S.push(a);
    45             while (S.top().p <= i-k)
    46                 S.pop();
    47             printf("%d ", S.top().v);
    48         }
    49         printf("\n");
    50 
    51         for (int i=1; i<k; i++){
    52             a=temp(i, in[i]);
    53             T.push(a);
    54         }
    55         for (int i=k; i<=n; i++){
    56             a=temp(i, in[i]);
    57             T.push(a);
    58             while (T.top().p <= i-k)
    59                 T.pop();
    60             printf("%d ", T.top().v);
    61         }
    62         printf("\n");
    63     }
    64 
    65     return 0;
    66 }

    p.s.:感谢lzq师兄的指导,感谢PKKJ师兄的STL教程!

    如果PKKJ师兄看到,我想问,set, map等中的查找的判断:

      s.find("content") != s.end()

      s.count("content")

    等价吗?

  • 相关阅读:
    一 基础--进制转化
    七牛云上传视频并截取第一帧为图片(js实现)
    FNScanner二维码接口openView自定义扫码Demo
    UIPickerView 模块示例demo
    vPlayer 模块Demo
    doT的高级用法及loadData的使用
    acmPush模块示例demo
    UIChatBox模块示例demo
    分享一款基于aui框架的图文发布界面
    基于js的APP多语言处理
  • 原文地址:https://www.cnblogs.com/LyonLys/p/poj_2823_STL_Lyon.html
Copyright © 2011-2022 走看看