题目链接

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

思路

因为交换次数达到 n + 10

其实我们可以先将他们都重新排序一下

在xoy 坐标系上 从左下角到右上角排序

然后 第2k 和 第 2k+1 之间连一条线

之后只需要交换顺序 来使得原序列变成现在的序列即可

其实在交换的时候 我们就需要交换 新点和旧点

比如这个样例当中

0 0 1

0 1 2

1 1 3

1 0 4

原来的 连接顺序是

1 3

2 4

排序后的连接顺序是

1 2

3 4

所以 我们只需要将 2 3 两个点交换位置即可 这个时候 2 就有 3 那个点所有的状态 3 就有 2 那个点原来的状态 包括 对应关系

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const int MOD = 1e9 + 7; int f[maxn]; struct Node
{
int x, y, id;
void read()
{
scanf("%d%d", &x, &y);
}
}q[maxn]; bool comp(Node x, Node y)
{
if (x.x == y.x)
return x.y < y.y;
return x.x < y.x;
} int main()
{
int T;
cin >> T;
while (T--)
{
CLR(f);
CLR(q);
int n;
scanf("%d", &n);
int m = n << 1;
for (int i = 1; i <= m; i++)
{
q[i].read();
q[i].id = i;
}
int x, y;
for (int i = 0; i < n; i++)
{
scanf("%d%d", &x, &y);
f[x] = y;
f[y] = x;
}
sort(q + 1, q + 1 + m, comp);
map <int, int> M;
for (int i = 1; i <= m; i++)
M[q[i].id] = i;
vector <pii> ans;
for (int i = 1; i <= m; i += 2)
{
int a = q[i].id, b = q[i + 1].id;
if (f[a] != b)
{
int c = f[a];
ans.pb(pii(b, c));
swap(q[i + 1].id, q[M[c]].id);
swap(M[b], M[c]);
}
}
int len = ans.size();
printf("%d\n", len);
vector <pii>::iterator it;
if (len)
{
for (it = ans.begin(); it != ans.end(); it++)
printf("%d %d\n", (*it).first, (*it).second);
}
}
}

ZOJ - 3862 Intersection 【贪心】的更多相关文章

  1. zoj 1025Wooden Sticks(贪心)

    递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  2. ZOJ 3829 模拟贪心

    2014牡丹江现场赛水题 给出波兰式,推断其是否合法.假设不合法有两种操作: 1:任何位置加一个数字或者操作符 2:随意两个位置的元素对调 贪心模拟就可以 先推断数字数是否大于操作符数,若不大于 an ...

  3. ZOJ FatMouse' Trade 贪心

    得之我幸,不得,我命.仅此而已. 学姐说呀,希望下次看到你的时候依然潇洒如故.(笑~) 我就是这么潇洒~哈哈. 感觉大家比我还紧张~ 我很好的.真的 ------------------------- ...

  4. ZOJ 3905 Cake(贪心+dp)

    动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...

  5. ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ...

  6. The 15th Zhejiang University Programming Contest

    a  ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...

  7. ZOJ Problem Set - 3829Known Notation(贪心)

    ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...

  8. Heap Partition ZOJ - 3963(贪心)

    ZOJ - 3963 贪心做一下就好了 反正别用memset #include <iostream> #include <cstdio> #include <sstrea ...

  9. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

随机推荐

  1. MFC中 编辑框输入换行功能

    首先修改编辑框的属性: Multiline 设为true , Auto HScroll 设为true , Auto VScroll 设为 true . 然后响应PreTranslateMessage( ...

  2. MySQL主从同步异常问题解决Client requested master to start replication from position > file size

    MySQL主从同步异常问题解决Client requested master to start replication from position > file size 一.问题描述 MySQ ...

  3. IDEA破解 2017 IDEA license server 激活(可用)

    进入ide主页面,help-register-license server,然后输入 http://idea.iteblog.com/key.PHP(注意:php要小写)即可~

  4. poj1691--Painting A Board(拓扑+dfs)

    题目链接:点击打开链接 题目大意:一个矩形由n个小矩形组成,如今要给小矩形染色,可是颜料会向下滑,为了防止弄乱颜料,所以要先染上面的矩形,后然染以下的矩形.每一次改变颜色都要用一个新的刷子.问最小用多 ...

  5. vue-cil 和 webpack 中本地静态图片的路径问题解决方案

    1.小于8K的图片将直接以base64的形式内联在代码中,可以减少一次http请求. 2.大于8k的呢?则直接file-loader打包, 这里并没有写明file-loader.但是确实是需要安装,否 ...

  6. IOS Audio开发集合

    打算每天抽出一点时间学习音频方面的知识,在此做下汇总: 1. 多媒体层预览  根据结构,明确学习内容.

  7. robot framework selenium2library定位

    进行页面元素操作,最麻烦的莫过于元素定位了,经常提示element is not visible 或者element is not exist 下面介绍常见的定位方法和定位中的问题 1 使用name和 ...

  8. ordinal parameter mismatch

    © 版权声明:本文为博主原创文章,转载请注明出处 错误描述:Caused by: org.hibernate.HibernateException: ordinal parameter mismatc ...

  9. shell脚本学习笔记 (流编辑器sed)

    sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用很普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. sed不 ...

  10. php selenium 测试验证码问题

    $this->pause(10000)这段代码用于停止程序执行,可以在这个空隙内输入验证码