City SelectionZJU3041 STL定义排序

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3041

应该按照x从小到大,y从大到小排序,xy同按照工厂优先排序;

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

struct Node
{
    int x;
    int y;
    bool f;//ture时表示工厂factory
    Node(int x,int y,bool f):x(x),y(y),f(f) {}
    bool operator<(const Node &rhs)const
    {
        return x<rhs.x || (x==rhs.x && y<rhs.y);//升序排列
    }
};

bool cmp(const Node &lhs,const Node &rhs)
{
    return lhs.x<rhs.x || (lhs.x==rhs.x && lhs.y>rhs.y)
           || (lhs.x==rhs.x && lhs.y==rhs.y && lhs.f);//x升序排列y降序排列,xy同工厂优先排列;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        //所有点都保存在vc1中
        vector<Node> vc1;
        for(int i=1; i<=n; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vc1.push_back(Node(x,y,false));
        }
        for(int i=1; i<=m; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vc1.push_back(Node(x,y,true));
        }
        sort(vc1.begin(),vc1.end(),cmp);

        //vc2中只保存可建城市的点
        vector<Node> vc2;
        int max_y=-1e9-1;//当前最大的y坐标
        for(int i=0; i<vc1.size(); i++)
        {
            if(vc1[i].f==true)//工厂点
            {
                max_y=max(max_y, vc1[i].y);
            }
            else//城市点
            {
                if(vc1[i].y > max_y) vc2.push_back(vc1[i]);
            }
        }

        sort(vc2.begin(),vc2.end());

        printf("%d\n",vc2.size());
        for(int i=0; i<vc2.size(); i++)
            printf("%d %d\n", vc2[i].x, vc2[i].y);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值