zoukankan      html  css  js  c++  java
  • 一个问题

    做题的时候碰到的,不太明白为什么。

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4585

    如果用我注释的写法就会超时,而我用注释上面的写法就会快很多,目前的猜测是set的lower_bound做了针对性的优化。。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 set<int> s;
     4 map<int,int> mp;
     5 int main(){
     6     int n;
     7     while(scanf("%d",&n) && n){
     8         s.clear();
     9         mp.clear();
    10         s.insert(1000000000);
    11         mp[1000000000] = 1;
    12         int id,grade;
    13         for(int i = 0;i<n;i++){
    14             scanf("%d%d",&id,&grade);
    15             
    16             set<int>::iterator it1 = s.lower_bound(grade),it2;
    17             //这样写不知道为什么会超时 
    18             //set<int>::iterator it1,it2;
    19             //it1 = lower_bound(s.begin(),s.end(),grade);
    20             if(it1 == s.begin()){
    21                 printf("%d %d
    ",id,mp[*it1]);
    22                 s.insert(grade);
    23                 mp[grade] = id;
    24                 continue;
    25             }
    26             it2 = it1;
    27             it1--;
    28             if(it2 == s.end()){
    29                 printf("%d %d
    ",id,mp[*it1]);
    30             }else if(grade-*it1 <= *it2-grade){
    31                 printf("%d %d
    ",id,mp[*it1]);
    32             }else{
    33                 printf("%d %d
    ",id,mp[*it2]);
    34             }
    35             s.insert(grade);
    36             mp[grade] = id;
    37         }
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    Web服务器的Socket通信
    Love story
    Excel初探索 - 2008/3
    One Person Game ZOJ Problem Set 3329
    只有程序员才看得懂的情书
    Collecting Bugs_poj2096
    Scout YYF I_poj3744
    hdu 3336 Count the string
    hdu 1669 Jamie's Contact Groups
    poj 1240
  • 原文地址:https://www.cnblogs.com/zqy123/p/6011203.html
Copyright © 2011-2022 走看看