Exemple #1
0
int fGetMovement(const HMap &appearances, const HMap &disappearances, AMap (&actors)){
  for(HMap::const_iterator itA = appearances.begin(); itA != appearances.end(); ++itA){
    int directions[] = {(*itA).first-(matrixSize+1), (*itA).first-matrixSize, (*itA).first-(matrixSize-1), (*itA).first-1, (*itA).first+1, (*itA).first+matrixSize-1, (*itA).first+matrixSize, (*itA).first+matrixSize+1};
    HMap::const_iterator itD;
    for(int i=0; i<8;++i){
      itD = disappearances.find(directions[i]);
      if(itD != disappearances.end()){
        actors.insert(APair((*itA).first, (new Person((*itD).first, (*itA).first, (*itA).first-(*itD).first))));
        break;
      }
    }
  }
  return 0;
}
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        //在每个后面加入远近坐标
        //想了一下似乎跟zindex没什么关系。先做吧
        int id = 0;
        vector<Building> bvec;
        for(auto& v : buildings)
        {
            Building left,right;
            left.x = v[0];
            left.isEnd = false;
            left.id = id;
            left.height = v[2];
            right = left;
            right.x = v[1];
            right.isEnd = true;
            bvec.push_back(left);
            bvec.push_back(right);
            id++;
        }
        //按X轴坐标排
        sort(bvec.begin(),bvec.end(),
             [](const Building& a,const Building& b)
        {
            if(a.x < b.x)return true;
            else if(a.x == b.x)
            {
                if(a.isEnd && b.isEnd)
                {
                    if(a.height > b.height)return true;
                    else return false;
                }
                else if(!a.isEnd && !b.isEnd)
                {
                    if(a.height < b.height)return true;
                    else return false;
                }
                else if(a.isEnd && !b.isEnd)
                {
                    //一个开始,一个结束,开始的放前面
                    return false;
                }
                else
                {
                    return true;
                }
                return false;
            }
            else return false;
        });

        typedef multiset<Building,MapComp> HMap;
        HMap heightMap;
        map<int,HMap::iterator> iterMap;
        int currHeight = -1;
        int currX = -1;
        vector<pair<int,int>> result;
        for(auto& b : bvec)
        {
            //开头
            if(!b.isEnd)
            {
                auto& pos = iterMap[b.id];
                pos = heightMap.insert(b);
            }
            else
            {
                auto iter= iterMap.find(b.id);
                heightMap.erase(iter->second);
                iterMap.erase(iter);
            }

            auto nearestIter = heightMap.begin();
            if(nearestIter == heightMap.end())
            {
                currHeight = 0;
            }
            else if(currHeight != nearestIter->height)
            {
                currHeight = nearestIter->height;

            }
            else
                continue;

            if(currX == b.x)
            {
                auto& tmp = result[result.size() - 1];
                if(b.isEnd && tmp.second > currHeight)tmp.second = currHeight;
                else if(!b.isEnd && tmp.second < currHeight)tmp.second = currHeight;
            }
            else
            {
                result.push_back(make_pair(b.x,currHeight));
            }
            currX = b.x;
        }
        return result;
    }