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 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
随机推荐
- 修改Tomcat服务中的端口配置
1.修改Tomcat服务中的端口配置: 分别修改安装目录下的conf子目录中的server.xml文件(注意:两个文件中对应的端口号要不一样),修改如下 : a. 修改Shutdown端口(默认为80 ...
- dedecms图片列表效果调用
效果如图 代码如下: <div class="listbox"> <ul class="e1"> {dede:list pagesize ...
- NOIP 2014 D2T3 解方程 Hash大法好
题目大意:给定高次方程an*x^n+...+a1*x^1+a0*x^0=0 求[1,m]区间内有多少个整数根 ai<=10^10000.m<=100W 懒得高精,考场上写的long dou ...
- antialiasing
Aliasing发生的原因在于,采样频率过低, 这样就无法重建更接近原来texture的图像, Temporal aliasing在时域上 采的texture 次数太少 不能重建真实 是帧与帧之间,s ...
- Codis的了解和操作
1.Codis的基本架构 2.Codis各组件 Codis-server:就是redis服务,可以使用codis修改的reids和原生的redis Codis-proxy:客户端连接的代理服务,客户端 ...
- RPM命令使用
RPM是RedHat Package Manager(RedHat软件包管理工具)的缩写 •rpm的常用参数 i:安装应用程序(install) e:卸载应用程序(erase) vh:显示安装进度:( ...
- 2017.2.21 Java中正则表达式的学习及示例
学习网站:菜鸟教程 http://www.runoob.com/java/java-regular-expressions.html 1 正则表达式的基本使用 (1)类 正则表达式并不仅限于某一种语言 ...
- 2016.10.17 yaml文件里的labels和Pod、RC、Service的对应关系
在看kubernetes的例子时,出现了一个疑问. Pod.RC.Service的yaml文件里,都出现了labels,还有labelSelector.有些不太清楚,因此就这点来学习下. 接上文: ...
- JAVA基础针对自己薄弱环节总结01(循环之前的知识)
java中的标识符 组成:数字.字母.下划线.美元$符号组成. 规则:不能由数字开头. 类名:每一个单词的首字母大写 包名:所有小写 变量名.方法名:第一个单词首字母小写.后面首字母大写 常 ...
- UNP学习笔记(第五章 TCP客户/服务程序实例)
我们将在本章使用前一章中介绍的基本函数编写一个完整的TCP客户/服务器程序实例 这个简单得例子是执行如下步骤的一个回射服务器: TCP回射服务器程序 #include "unp.h" ...