zoukankan      html  css  js  c++  java
  • Gym

    题目网址链接:https://vjudge.net/problem/387095/origin

    题目大意:给出桌子数和桌子容纳人数,给出几条操作,操作如下:给出顾客人数,问是否有能容纳顾客且桌子标号和桌子容纳人数与顾客人数差值同时最小的桌子,有即输出桌子编号,没有输出-1;期间会有顾客离开,给出离开的桌子编号。

    在做题之前,先学习STL中的set:以集合形式从小到大排列set中的元素。

    先给出几个大牛博客链接:

    https://www.cnblogs.com/zyxStar/p/4542835.html

    https://blog.csdn.net/sunquana/article/details/39855355

    https://blog.csdn.net/bingqingsuimeng/article/details/73480190

    set<pair<int ,int > >的笔记:

    1:> >两个'>'中药有空格.

    2:pair<x,y>,中x,y可以为任何类型的常量,如int,char,但是调用x用first表示,y用second表示,eg:看代码。

    3:将pair<int ,int >放入set中要有make_pair(int,int);至于为什么,本人不清楚,好像是由于将pair类型变下。

    4:set中存入pair,比较的方法是先比较first,在比较second。

    5:end()所指的是最后一个元素后面的位子。

    知道了这些,解决这题就非常简单了。代码如下:

     1 #include<iostream>
     2 #include<set>
     3 using namespace std;
     4 int a[100005];
     5 set<pair<int,int> > ha;
     6 int main()
     7 {
     8     int m,n,x;
     9     char str[5];
    10     while(cin>>m>>n)
    11     {
    12         for(int i=1;i<=m;i++)
    13         {
    14             cin>>a[i];
    15             ha.insert(make_pair(a[i],i));
    16         }
    17         while(n--)
    18         {
    19             cin>>str>>x;
    20             if(str[0]=='i')
    21             {
    22                 set<pair<int,int> >::iterator it=ha.begin();
    23                 set<pair<int,int> >::iterator ti=ha.end();
    24                 it=ha.lower_bound(make_pair(x,0));
    25                 if(it==ti)
    26                 cout<<"-1"<<endl;
    27                 else
    28                 {
    29                     cout<<it->second<<endl;
    30                     ha.erase(it); 
    31                 }
    32             }
    33             else
    34                 ha.insert(make_pair(a[x],x));
    35         }
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    如何判断栈的增长方向
    时间复杂度
    shell基础part3
    shell基础part2
    shell基础part2
    linux基础part5
    linux基础part4
    linux基础part3
    linux基础part2
    shell基础part1
  • 原文地址:https://www.cnblogs.com/wwq-19990526/p/9280035.html
Copyright © 2011-2022 走看看