zoj 3862 Intersection(枚举)

来源:互联网 发布:淘宝密码怎么修改 编辑:程序博客网 时间:2024/04/30 02:01

Intersection

Time Limit: 3 Seconds      Memory Limit: 131072 KB      Special Judge

Edward has 2n points on the plane conveniently labeled with 1,2,…,2n. Each point is connected exactly with another point by a segment.

Edward finds that some segments intersecting with some others. So he wants to eliminate those intersections with the following operation: choose two points i and j (1 ≤ ij ≤ 2n) and swap their coordinates.

swap point 2 and 3

For example, Edward has 4 points (0, 0), (0, 1), (1, 1), (1, 0). Point 1 is connected with point 3 and point 2 is connected with 4. Edward can choose to swap the coordinates of point 2 and point 3.

Edward wants to know whether it is possible to use at most n + 10 operations to achieve his goal.

No two points coincide and no three points are on the same line.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 100000).

Each of the following 2n lines contains 2 integers xiyi which denotes the point (xiyi). (|xi|, |yi| ≤ 109).

Each of the following n lines contains 2 integers aibi (1 ≤ aibi ≤ 2nai ≠ bi), which means point ai and point bi are connected by a segment.

The sum of values n for all the test cases does not exceed 300000.

Output

For each test case, print a line containing an integer m, indicating the number of operations needed. You must assure that m is no larger than n + 10. If you cannot find such a solution, just output "-1" and ignore the following output.

In the next m lines, each contains two integers i and j (1 ≤ ij ≤ 2n), indicating an operation, separated by one space.

If there are multiple solutions, any of them is accepted.

Sample Input

120 00 11 11 01 32 4

Sample Output

12 3

给出一些点的坐标 以及这些点连接的情况 里面可能会有一些交叉的情况 我们要调节点的位置

使得这些点之间没有交叉  求最小的调节数  输出相应调节的点


由于题目中并没有要求 求出最小的调节数 所以我们可以通过枚举的方法

首先按照x坐标小y坐标小在前 进行排序 记录点在排序之前 排序之后的顺序

然后从左下角开始枚举点 如果它对面的点不是应该连接的那个点 那么就要交换点的位置

然后 经过调节 使得所有的线段都是平行的

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 200010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char ch;    int a = 0;    while((ch = getchar()) == ' ' | ch == '\n');    a += ch - '0';    while((ch = getchar()) != ' ' && ch != '\n')    {        a *= 10;        a += ch - '0';    }    return a;}void Print(int a)    //输出外挂{     if(a>9)         Print(a/10);     putchar(a%10+'0');}struct node{    int x,y,id;    bool operator<(const node &p)const    {        if(y==p.y)            return x>p.x;        return y>p.y;    }}no[MAXN];int link[MAXN];//link[i]代表i连接的点int now[MAXN];//排序之前 所在的位置int p[MAXN];//排序之后所在的位置int ans[MAXN+10][2];int main(){//    fread;    int tc;    scanf("%d",&tc);    while(tc--)    {        int n;        scanf("%d",&n);        for(int i=1;i<=2*n;i++)        {            scanf("%d%d",&no[i].x,&no[i].y);            no[i].id=i;        }        for(int i=1;i<=n;i++)        {            int x,y;            scanf("%d%d",&x,&y);            link[x]=y; link[y]=x;        }        sort(no+1,no+2*n+1);        for(int i=1;i<=n*2;i++)        {            now[i]=no[i].id;            p[no[i].id]=i;        }        int num=0;        for(int i=1;i<=n;i++)        {            int s=2*i-1,t=2*i;            if(link[now[s]]==now[t]) continue;            else            {                ans[num][0]=now[t];                ans[num][1]=link[now[s]];                num++;                p[now[t]]=p[link[now[s]]];                now[p[link[now[s]]]]=now[t];                p[link[now[s]]]=t;            }        }        printf("%d\n",num);        for(int i=0;i<num;i++)        {            printf("%d %d\n",ans[i][0],ans[i][1]);        }    }    return 0;}





0 0