题目链接

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. css :before 和 :after

    :before p:before 在每个 <p> 元素的内容之前插入内容. 2 :after p:after 在每个 <p> 元素的内容之后插入内容. 2 <!DOCTY ...

  2. Checkbox Text 重影问题的解决的方法

    Checkbox有个属性值 <CheckBox android:id="@+id/cb_reg_agree" style="@style/reg_checkbox_ ...

  3. 【音乐App】—— Vue-music 项目学习笔记:搜索页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 搜索歌手歌曲 搜索历史保存 ...

  4. 2017.2.9 开涛shiro教程-第十章-会话管理(一)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十章 会话管理(一) 10.1 会话 shiro提供的会话可以用 ...

  5. 二叉查找树BST----java实现

                                                                    二叉查找树BST----java实现 1.二叉查找树简单介绍 二叉查找树 ...

  6. mongodb副本集的基础概念和各种机制

         从一开始我们就在讲如何使用一台服务器.一个mongod服务器进程,如果只用做学习和开发,这是可以的,但如果在生产环境中,这是很危险的,如果服务器崩溃了怎么办?数据库至少要一段时间不可用,如果 ...

  7. Git-flow 基本使用方法

    本文参考自:https://juejin.im/entry/5ad9a28d6fb9a07ac76e5e22 1.分支模型 master 分支:用于上线的分支,保护性分支,只包含经过测试的稳定代码,开 ...

  8. 面向对象在JavaScript中的接口实现

    接口是面向对象编程的基础.它是一组包括了函数型方法的数据结构,与类一样.都是编程语言中比較抽象的概念.比方生活中的接口.机顶盒.人们利用它来实现收看不同频道和信号的节目,它宛如对不同类型的信息进行集合 ...

  9. srm 541

    资瓷点这里阅读该文章O_o 250 Solution 水题,最暴力的方法枚举就可以 Code #include <bits/stdc++.h> using namespace std; # ...

  10. mysql主从只同步部分库或表

    同步部分数据有两个思路,1.master只发送需要的:2.slave只接收想要的. master端: binlog-do-db      二进制日志记录的数据库(多数据库用逗号,隔开)binlog-i ...