ZOJ 3862 Intersection【贪心】【几何】【模拟】

9 篇文章 0 订阅
5 篇文章 0 订阅

题目链接

http://icpc.moe/onlinejudge/showProblem.do?problemId=5474

思路

题意是给你很多条线段,你可以交换任意两个点,叫你给出一种交换序列,使得最后没有两条线段是相交的。

这里要贪心一下,就是把点按x小优先,x相等y大优先排好序,然后两个两个连起来。
如下图:

这里写图片描述

至于怎么把两个点连起来,只用交换这两个点 其中一个点,和另一个点的相连点 即可。

AC代码

#include <iostream>
#include <cstdio>
#include <set>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
using namespace std;

struct node_t
{
    int x, y;
    int id;
    int to;
    bool friend operator < (node_t a, node_t b)
    {
        if (a.x == b.x)return a.y>b.y;
        return a.x < b.x;
    }
}
node[200000 + 100];

int pos[200000 + 100];
pair<int, int>ans[200000 + 200];
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int cnt_ans = 0;
        memset(pos, 0, sizeof pos);
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= 2 * n; ++i)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            node[i].x = x; 
            node[i].y = y;
            node[i].id = i;
        }
        for (int i = 1; i <= n; ++i)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            node[a].to = b;
            node[b].to = a;
        }
        sort(node + 1, node + 1 + 2 * n);
        for (int i = 1; i <= 2 * n; ++i)
        {
            pos[node[i].id] = i;
        }
        for (int i = 1; i < 2 * n; i += 2)
        {
            if (node[i].to == node[i + 1].id && node[i + 1].to == node[i].id);
            else
            {
                ans[cnt_ans++] = make_pair(node[i].id, node[pos[node[i + 1].to]].id);
                int id1 = node[i].id, id2 = node[i + 1].to;
                int pos2 = pos[id2];
                swap(node[i].id, node[pos2].id);
                swap(node[i].to, node[pos2].to);
                pos[id1] = pos2;
                pos[id2] = i;
            }
        }
        printf("%d\n", cnt_ans);
        for (int i = 0; i < cnt_ans; ++i)
        {
            printf("%d %d\n", ans[i].first, ans[i].second);
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值