1236: Simple Sort

时间限制: 1 Sec  内存限制: 128 MB

提交: 195  解决: 53

[提交][状态][讨论版]

题目描述

     You are given n two-dimension points randomly. Now you are asked to sort them by the following rule. For example , there are two points ,point A(x1,y1) and point B(x2,y2), to be compared.
We define point A is less than point B if x1<x2. When x1 equals to x2, we define point A is less than point B if y1<y2. If x1 equals to x2 and y1 equals to y2, we say point A and point B are equal.
     Now you need to sort the points in non-descending order according the rules.

输入

    There are serval test cases.
    The first line contians a integer t which deticates the number of test cases.
    For each test case, the first line contians a integer n deticating the number of points in the test case. The next n lines contain two integers per line which are the positions of n points.
(0<t<=100, 0<n<=10000, -1000<=x<=1000, -1000<=y<=1000)

输出

For each test case, the first line print "Test case x:" in which number x is the test case number starting from 1. There are n lines following. Print out the result of the sort.

样例输入

3
3
10 2
5 4
3 9
3
7 8
8 4
7 5
1
4 4

样例输出

Test case 1:
3 9
5 4
10 2
Test case 2:
7 5
7 8
8 4
Test case 3:
4 4
#include<stdio.h>
struct point{ int x, y; };
point  a[10000];
void copy(point &k, point& i){
	k.x = i.x;
	k.y = i.y;
}
bool less(point i, point j){
	if (i.x < j.x)return true;
	if (i.x==j.x&&i.y < j.y)return true;
	return false;
}
bool big(point i, point j){
	if (i.x>j.x)return true;
	if (i.x==j.x&&i.y>j.y)return true;
	return false;
}
void sort(int from, int to){
	if (from >= to)return;
	int i = from, j = to;
	point k;
	copy(k, a[from]);
	while (1){
		while (big(a[j], k))j--;
		if (j == i)break;
		copy(a[i], a[j]);
		copy(a[j], k);
		i++;
		while (less(a[i], k))i++;
		if (j == i)break;
		copy(a[j], a[i]);
		copy(a[i], k);
		j--;
	}
	sort(from, i - 1);
	sort(i + 1, to);
}
int main(){
	freopen("in.txt", "r", stdin);
	int t;
	scanf("%d", &t);
	int tt;
	for(tt=1;tt<=t;tt++){
		int n;
		scanf("%d", &n);
		int i;
		for (i = 0; i < n; i++)scanf("%d%d",&a[i].x,&a[i].y);
		sort(0, n - 1);
		printf("Test case %d:\n", tt);
		for (i = 0; i < n; i++)
			printf("%d %d\n", a[i].x, a[i].y);
	}
	return 0;
}

东大OJ-快速排序的更多相关文章

  1. 东大OJ 2SAT 异或

    看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...

  2. 东大OJ-Max Area

    1034: Max Area 时间限制: 1 Sec  内存限制: 128 MB 提交: 40  解决: 6 [提交][状态][讨论版] 题目描述 又是这道题,请不要惊讶,也许你已经见过了,那就请你再 ...

  3. 清橙OJ 1082 查找第K小元素 -- 快速排序

    题目地址:http://oj.tsinsen.com/A1082 问题描述 给定一个大小为n的数组s和一个整数K,请找出数组中的第K小元素. 这是一个补充程序的试题,你需要完成一个函数: int fi ...

  4. sort(hdu oj 1425)计数排序和快速排序

    Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0 < n,m < 1000000),第二行包含n个各不 ...

  5. SDUT OJ 数据结构实验之排序八:快速排序

    数据结构实验之排序八:快速排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 给定N ...

  6. 【Tsinghua OJ】灯塔(LightHouse)问题

    描述 海上有许多灯塔,为过路船只照明.从平面上看,海域范围是[1, 10^8] × [1, 10^8] . (图一) 如图一所示,每个灯塔都配有一盏探照灯,照亮其东北.西南两个对顶的直角区域.探照灯的 ...

  7. ACM——快速排序法

    快速排序 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:653            测试通过:297 描述 给定输入排序元素数目 ...

  8. Java 泛型快速排序 以sdut 1196为例

    oj链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1196 Java中,Arrays.so ...

  9. PKU OJ 1002 487-3279

    PKU OJ 1002 487-3279 487-3279 Description Businesses like to have memorable telephone numbers. One w ...

  10. 双基准快速排序(Dual-Pivot Quicksort)(转)

    课本上常见的快速排序都是选择一个枢纽元(Pivot),基于这个枢纽元从前后双向扫描分成大于枢纽元和小于枢纽元的.而从JDK 7开始,java.util.Arrays.sort()使用双基准快速排序(D ...

随机推荐

  1. ELF Format 笔记(四)—— 节(Section)

    ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 ELF 文件可以包含很多 section,所有的 section 都在 section header tab ...

  2. 【原】一张图片优化5K的带宽成本

    上周,我参加了公司的一门课程<网站性能优化>,讲师提出了一个问题:一张图片优化后减少5K,1年内可以大概省下多少宽带成本呢?非常好奇,仔细听完讲师分析,计算出来的数据让小伙伴们都惊呆了,仅 ...

  3. Java Web之JSP技术

    JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比htm ...

  4. 中国移动测试大会 PPT 和视频

    PPT网盘链接:http://pan.baidu.com/s/1c0prdoG优酷专辑:http://v.youku.com/v_show/id_XMTI5NjExNjIwOA==.html?f=25 ...

  5. 2016.11.17 NOI plus day0

    今天很乱乱乱乱 根本不想写代码 玩了一早上了 昨晚失眠了 今天又懵逼了 中午就要走了 明天就要考试了 考完试回来就要补文化课了 现在我的内心很平静 因为已经紧张的冻结了 你知道什么叫彷徨么? 机房里的 ...

  6. AppStore ipa (苹果内购)笔记

    内购示意图 准备条件 苹果的开发者证书,已经为应用启用App内购,并在Xcode更新配置文件 itunes store设置 itunes中创建App及其它设置 参考:iOS应用程序内购/内付费(一)  ...

  7. JProfiler学习笔记

    JProfiler学习笔记   一.安装JProfiler        从http://www.ej-technologies.com/下载5.1.2并申请试用序列号   二.主要功能简介 1.内存 ...

  8. log4j日志文件配置说明及使用

    一.log4j.properties文件格式说明:    log4j.rootLogger=info, stdout log4j.appender.stdout=org.apache.log4j.Co ...

  9. memcache分布式 [一致性hash算法] 的php实现

    最近在看一些分布式方面的文章,所以就用php实现一致性hash来练练手,以前一般用的是最原始的hash取模做分布式,当生产过程中添加或删除一台memcache都会造成数据的全部失效,一致性hash就是 ...

  10. 在iframe中获取本iframe DOM引用

    window.frameElement 获取本iframe DOM window.frameElement.contentDocument.getElementById('id') 获取这个ifram ...