zoukankan      html  css  js  c++  java
  • HDU5127 神坑题---vector 、 list 、 deque 的用法区别

    题意:三个操作

            1  a b : 队列中加入(x = a, y = b);

            -1  a b : 队列中减去(x = a, y = b);

            0  p q :从队列的数对中查询哪一对x,y能够让 p * x + q * y最大;

    分析:因为一开始就觉得如果暴力绝对会超时,但是时限是30 000 ms,而且看见FB的又是8800ms过的,所以就暴力一次试试,但是TLE.

     1 #include <iostream>
     2 #include <cmath>
     3 #include <vector>
     4 #include <cstdio>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <string>
     8 #include <sstream>
     9 #include <algorithm>
    10 #define Max 2147483647
    11 #define INF -0x7fffffff
    12 #define N 910
    13 #define ll long long
    14 #define mem(a,b) memset(a,b,sizeof(a))
    15 #define repu(i, a, b) for(int i = (a); i < (b); i++)
    16 const double PI=-acos(-1.0);
    17 using namespace std;
    18 vector<pair<ll,ll> > v;
    19 int main()
    20 {
    21     ll a,b,c;
    22     int n;
    23     while(scanf("%d",&n) && n)
    24     {
    25         v.clear();
    26         repu(i,0,n)
    27         {
    28             scanf("%I64d%I64d%I64d",&a,&b,&c);
    29             vector<pair<ll,ll> > ::iterator it;
    30             if(a == 1)
    31                 v.push_back(pair<ll,ll>(b,c));
    32             else if(a == -1)
    33             {
    34                 pair<ll,ll> t = pair<ll,ll>(b,c);
    35                 for(it = v.begin(); it!=v.end(); it++)
    36                 {
    37                     if(*it == t)
    38                     {
    39                         v.erase(it);
    40                         break;
    41                     }
    42                 }
    43             }
    44             else
    45             {
    46                 ll maxn = 0;
    47                 int flag = 1;
    48                 for(it = v.begin(); it!=v.end(); it++)
    49                 {
    50                     if(flag)
    51                         maxn = it->first * b + it->second * c,flag = 0;
    52                     else
    53                         maxn = max(it->first * b + it->second * c,maxn);
    54                 }
    55                 printf("%I64d
    ",maxn);
    56             }
    57         }
    58     }
    59     return 0;
    60 }
    TLE代码
     1 #include <iostream>
     2 #include <cmath>
     3 #include <list>
     4 #include <cstdio>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <string>
     8 #include <sstream>
     9 #include <algorithm>
    10 #define Max 2147483647
    11 #define INF -0x7fffffff
    12 #define N 910
    13 #define ll long long
    14 #define mem(a,b) memset(a,b,sizeof(a))
    15 #define repu(i, a, b) for(int i = (a); i < (b); i++)
    16 const double PI=-acos(-1.0);
    17 #define pii pair<ll,ll>
    18 using namespace std;
    19 list<pii > v;
    20 int main()
    21 {
    22     ll a,b,c;
    23     int n;
    24     while(scanf("%d",&n) && n)
    25     {
    26         v.clear();
    27         ///list<pii> v   每次被定义一遍更费时间,还不如clear时间少
    28         repu(i,0,n)
    29         {
    30             scanf("%I64d%I64d%I64d",&a,&b,&c);
    31             list<pii> ::iterator it;
    32             if(a == 1)
    33                 v.push_front(pii(b,c));
    34             else if(a == -1)
    35             {
    36                 pii t = pii(b,c);
    37                 for(it = v.begin(); it!=v.end(); it++)
    38                 {
    39                     if(*it == t)
    40                     {
    41                         v.erase(it);
    42                         ///v.remove(*it);也会超时,题目卡的很严
    43                         break;
    44                     }
    45                 }
    46             }
    47             else
    48             {
    49                 ll maxn = 0;
    50                 int flag = 1;
    51                 for(it = v.begin(); it!=v.end(); it++)
    52                 {
    53                     if(flag)
    54                         maxn = it->first * b + it->second * c,flag = 0;
    55                     else
    56                         maxn = max(it->first * b + it->second * c,maxn);
    57                 }
    58                 printf("%I64d
    ",maxn);
    59             }
    60         }
    61     }
    62     return 0;
    63 }
    AC代码

    AC代码也是看了某个大神的代码仅仅用Ctrl+r把TLE代码中的vector改成了list,就A了,但是时间有点慢,我也是醉了;

    其实就是vector和list的区别(http://w57w57w57.blog.163.com/blog/static/9607473520094751136967/):

    具体细节看链接,只写用法:

      在实际使用时,如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面的原则:
      1、如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector
      2、如果你需要大量的插入和删除,而不关心随即存取,则应使用list
      3、如果你需要随即存取,而且关心两端数据的插入和删除,则应使用deque。

    而这个题目,即使用vector也并不是随即随取,也得是从头到尾遍历,因此插入和删除的时间复杂度更重要一些,所以就选择了list。

    list的时间复杂度:         insert-------O(1)             erase-------O(1)

    vector的时间复杂度:      insert------O(log(n))        erase------O(log(n))

     感觉并不是正规解,只能说是讨了后台样例的便宜。。。

  • 相关阅读:
    魔兽世界中的一些公式
    T端大灾变版本传送大师(NPC 脚本)
    T端带数据库查询的假人系统
    T端GM上线提示
    T端mysql优化设置
    T端根据玩家职业来显示不同颜色的角色名字的C++代码
    IOS复习-UIButton
    从明天开始认真写博客
    为什么到今天还要坚持写博客
    cocos2d—1—环境安装
  • 原文地址:https://www.cnblogs.com/ACMERY/p/4697023.html
Copyright © 2011-2022 走看看