Intersection(ZOJ3862)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5474

一开始自己写的wa了。因为所有的答案只要换的合理,不过多操作总是可以有答案的。但是当时交换次数过多导致wa了。然后就不知道怎么省略。

而且这道题的交换让人思路混乱。简直不要太痛苦。

后来参考了队员的代码,思路是类似的但是,她就是能对。swap()操作省了很多代码量。

参考:http://blog.csdn.net/loy_184548/article/details/50967627

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int x,y,id;
} a[200005];

map<int,int> mp;

bool cmp(node a,node b)
{
    if(a.x!=b.x)
        return a.x<b.x;
    return a.y<b.y;
}

int anslist[200005];

int pos[200005];

int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(anslist,0,sizeof(anslist));
        memset(pos,0,sizeof(pos));
        mp.clear();
        int n;
        scanf("%d",&n);
        for(int i=0; i<2*n; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].id=i+1;
        }
        for(int i=0; i<n; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x]=y;
            mp[y]=x;
        }

        sort(a,a+2*n,cmp);

        int cnt=0;
        int ans=0;
        for(int i=0; i<2*n; i++)
            pos[a[i].id]=i;

        for(int i=0; i<2*n; i+=2)
        {
            int u=a[i].id;
            int v=a[i+1].id;
            if(mp[u]==v)continue;

            int u_con=mp[u];
            anslist[cnt++]=v;
            anslist[cnt++]=u_con;
            swap(a[i+1].id,a[pos[u_con]].id);
            ans++;
            swap(pos[v],pos[u_con]);
        }
        printf("%d\n",ans);
        for(int i=0; i<cnt; i+=2)
            printf("%d %d\n",anslist[i],anslist[i+1]);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值