ZOJ - 3862 Intersection 【贪心】
题目链接
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 【贪心】的更多相关文章
- zoj 1025Wooden Sticks(贪心)
递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- ZOJ 3829 模拟贪心
2014牡丹江现场赛水题 给出波兰式,推断其是否合法.假设不合法有两种操作: 1:任何位置加一个数字或者操作符 2:随意两个位置的元素对调 贪心模拟就可以 先推断数字数是否大于操作符数,若不大于 an ...
- ZOJ FatMouse' Trade 贪心
得之我幸,不得,我命.仅此而已. 学姐说呀,希望下次看到你的时候依然潇洒如故.(笑~) 我就是这么潇洒~哈哈. 感觉大家比我还紧张~ 我很好的.真的 ------------------------- ...
- ZOJ 3905 Cake(贪心+dp)
动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...
- 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$), ...
- The 15th Zhejiang University Programming Contest
a ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...
- ZOJ Problem Set - 3829Known Notation(贪心)
ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...
- Heap Partition ZOJ - 3963(贪心)
ZOJ - 3963 贪心做一下就好了 反正别用memset #include <iostream> #include <cstdio> #include <sstrea ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
随机推荐
- Android蓝牙开发教程(三)——蓝牙设备相互通讯
在上一篇中已经介绍如何连接我们搜索到的蓝牙设备,如果你还没阅读过,建议先看看上一篇文章Android蓝牙开发教程(二)——连接蓝牙设备 在上一篇文章中,无论是自动连接还是被动连接,连接成功后,都是将获 ...
- 基于django的网站开发一基础项目配置
首先确认电脑上已经安装好了python和django,我的python版本是2.7.13,django版本是1.10.2.数据库我使用的是mysql,版本是5.7.17,我是windows7系统,用的 ...
- 2017.2.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解
深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二) ------配置文件详解 8.2 MyBatis-Spring应用 8.2.1 概述 本文主要讲述通过注解配置MyBa ...
- MQTT---HiveMQ源代码具体解释(十四)Persistence-LocalPersistence
源博客地址:http://blog.csdn.net/pipinet123 MQTT交流群:221405150 简单介绍 HiveMQ的Persistence提供配置包含File和Memory,以解决 ...
- SQL检索语句及过滤语句
首先推荐一款比较好用的数据库管理软件:navicat premium. 数据库中最重要的检索功能:SELECT语句 1.检索单个列:select 列名 from 表名: 2.检索多个列:select ...
- Html简单的整页切换
恩,语言组织不是很好,直接上代码吧.... <!DOCTYPE html> <html> <head lang="en"> <meta c ...
- 【转】【Pycharm大全】
感谢:陈俊岭的程序员之路 [Pycharm大全]:http://blog.csdn.net/u013088062/article/details/50388329
- erlang中通过ip和子网掩码,计算地址范围 【二进制和十进制的转换】
在程序中,难免用的二进制和十进制之间的转换.遇到一个场景,通过ip和子网掩码,计算地址范围. 而地址范围为:网络地址+1—— 广播地址 -1 . 网络地址即ip和子网掩码的与的位运算.广播地址为:网 ...
- IntelliJ IDEA生成 Serializable 序列化 UID 的快捷键
首先创建一个类如Movie,让该类实现Serializable序列化接口. 然后我们需要依次按照以下的方法找到 Settings 之后我们需要以下几个操作,并找到 Serializable class ...
- C# 自定义控件制作和使用实例(winform)(转)
本例是制作一个简单的自定义控件,然后用一个简单的测试程序,对于初学者来说,本例子比较简单,只能起到抛石引玉的效果. 我也是在学习当中,今后会将自己所学的逐步写出来和大家交流共享. 第一步:新建一个 ...