zoukankan      html  css  js  c++  java
  • hdu 4268 Alice and Bob(STL贪心)

    题解:

          对Alice和Bob的数据一起排序,再贪Alice离Bob最进的矩形

          做了整整一个下午,我晚饭后找了一会,还是没发现,

          一筹莫展之际,只有使出杀手锏(求教飞机哥!!!)

          正在注释代码准备求助时,终于的发现了坑货的小bug!!!!

        ( bool  cmp()中忘写了return false;以前使用int cmp())

          1。起初是直接查找TLE,各种换数据结构。

          2。数组开100005提交,Runtime Error
                                    (ACCESS_VIOLATION)

            又百度,此错误好像有爆内存的原因(明明就够题目数据量的??)。

          3。最后还得请教baidu先生,找到

    1. iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。  
    2. iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。
      #include<stdio.h>
      #include<list>
      #include<set>
      #include<string.h>
      #include<algorithm>
      #include<iostream>
      using namespace std;
      struct node
      {
          int w,h,flag;//0 B;1 A;
      }a[200005];
      //list<node>s;
      //list<node>::iterator it;
      set<int>s;
      set<int>::iterator it;
      bool cmp(node b,node c)
      {
          if(b.w<c.w)return true;
          else if(b.w>c.w)return false;
          else if(b.h<c.h)return true;
          else if(b.h<c.h)return false;
          else if(b.flag<c.flag)return true;//small falg in the front
          return false;
      }//按依次按宽度,高度,(完全相同)则B在A前排序
      int main()
      {
          int _case,n,m,x;
          scanf("%d",&_case);
          while(_case--)
          {
              //memset(a,0,sizeof(a));
              s.clear();
              scanf("%d",&n);
              m=n*2;
              for(int i=0;i<m;i++)
              {
                  scanf("%d %d",&a[i].h,&a[i].w);
                  a[i].flag=1-i/n;
              }
              sort(a,a+m,cmp);
              //x=0;
              for(int i=0;i<m;i++)
              {
                  if(a[i].flag==0)
                  //s.push_front(a[i]);
                  s.insert(a[i].h);
                  else if(s.size())
                  {
                      /*for(it=s.begin();it!=s.end();it++)
                      {
                          if((*it).h<=a[i].h)
                          {
                              s.erase(it);
                              break;
                          }
                      }*/
                      if(a[i].h<*s.begin())continue;
                      it=s.upper_bound(a[i].h);//lower_bound()正确,数据有点弱
                      it--;//删除前一个,最近的覆盖//x++;
                      s.erase(it);
                  }
                  //printf("%d %d\n",i,s.size());
              }
              printf("%d\n",n-s.size());
          }
          return 0;
      }
       鉴于爆内存的错误,写了vector的程序;
    3. #include<stdio.h>
      #include<list>
      #include<set>
      #include<vector>
      #include<string.h>
      #include<algorithm>
      #include<iostream>
      using namespace std;
      struct node
      {
          int w,h,flag;//0 B;1 A;
      }p;
      vector<node>a;
      set<int>s;
      set<int>::iterator it;
      bool cmp(node b,node c)
      {
          if(b.w<c.w)return true;
          else if(b.w>c.w)return false;
          else if(b.h<c.h)return true;
          else if(b.h<c.h)return false;
          else if(b.flag<c.flag)return true;//small falg in the front
          return false;
      }
      int main()
      {
          int _case,n,m;
          //int x,y;
          scanf("%d",&_case);
          while(_case--)
          {
              //memset(a,0,sizeof(a));
              a.clear();
              s.clear();
              scanf("%d",&n);
              m=n*2;
              for(int i=0;i<m;i++)
              {
                  scanf("%d %d",&p.h,&p.w);
                  p.flag=1-i/n;
                  a.push_back(p);
                  //a[i].w=y;
              }
              sort(a.begin(),a.end(),cmp);
              for(int i=0;i<m;i++)
              {
                  if(a[i].flag==0)
                        s.insert(a[i].h);
                  else if(s.size())
                  {
                      if(a[i].h<*s.begin())continue;
                      it=s.lower_bound(a[i].h);//用upper_bound()也正确
                      it--;
                      //x++;
                      s.erase(it);
                  }
                  //printf("%d %d\n",i,s.size());
              }
              printf("%d\n",n-s.size());
          }
          return 0;
      }
  • 相关阅读:
    代理模式
    spring aop
    mybatis从入门到精通(其他章节)
    mybatis从入门到精通(第6章)
    Java中Compareable和Comparator两种比较器的区别
    Java的equals方法的使用技巧
    Dubbo的配置过程,实现原理及架构详解
    什么是IPFS?IPFS与区块链有什么关系
    leetCode242 有效的字母异位词
    需要多个参数输入时-----------------考虑使用变种的Builder模式
  • 原文地址:https://www.cnblogs.com/XDJjy/p/3055426.html
Copyright © 2011-2022 走看看