zoukankan      html  css  js  c++  java
  • poj 1442

    一个排序的题目。

    题意:给你m个数a[m],和n个数b[n]。

    首先a[0]….a[b[0]]排序。输出第一个数。

    然后a[0]….a[b[1]]排序。输出第二个数。

    以此类推,直到输出第n个数。

    思路:最开始,我就是用快排,对每一次从a[0]到a[b[i]]的进行排序,然后在输出a[i]。

    然后wa了,并不是TLE…我也不知道为什么,有点奇怪。然后看discuss,有人说用优先队列。

    然后就优先队列做的,一个以最大的为优先,一个以最小的为优先的队列

      1 #include <stdio.h>
      2 #include <queue>
      3 
      4 using namespace std;
      5 
      6 int a[30005];
      7 
      8 int main()
      9 {
     10     int m,n,x,c=0,tmp;
     11     scanf("%d%d",&m,&n);
     12     for(int i=0;i<m;i++)
     13         scanf("%d",&a[i]);
     14     priority_queue<int,vector<int>,less<int> >big;   //最大优先。
     15     priority_queue<int,vector<int>,greater<int> >small;
     16     for(int i=0;i<n;i++)
     17     {
     18         scanf("%d",&x);
     19         while(c<x)
     20         {
     21             small.push(a[c]);
     22             c++;
     23         }
     24         while(!big.empty()&&big.top()>small.top())   //目的是为了small队列中的最小值就是那个我们所求的数。而且每一次操作,big数组的容量就会++,这也就保证了我们所求的数是第几小的。
     25         {
     26             tmp=big.top();
     27             big.pop();
     28             big.push(small.top());
     29             small.pop();
     30             small.push(tmp);
     31         }
     32         printf("%d
    ",small.top());
     33         big.push(small.top());
     34         small.pop();
     35     }
     36     return 0;
     37 }
     38 
     39 
     40 
  • 相关阅读:
    vim for python配置
    Python学习的一些好资料
    【Python开发实战】Python环境的配置
    【Python开发实战】Windows7+VirtualBox+Ubuntu环境配置
    linux下shapely的安装
    【python常用模块】os.path
    linux下gdal的python包的安装
    由二叉树的前序遍历和中序遍历,求其后序遍历
    ASCII码表
    C++标准库函数之排列函数
  • 原文地址:https://www.cnblogs.com/Tree-dream/p/5601884.html
Copyright © 2011-2022 走看看