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. Centos 7 ASP.NET Core 1.0 Docker部署

    先决条件 64位,内核3.10以上,查看当前的内核版本,打开一个终端使用uname -r显示您的内核版本             安装 sudo yum update     sudo tee /et ...

  2. bootstrap简单的过一遍

    注:.xxxx 表示类(class=xxxx) <h1>到<h6>均可使用.另外还提供了.h1到.h6的class .lead可以让段落突出显示 <small>  ...

  3. JAVA-android 更改APP名称与图标

    首先要在你的资源文件放入你想换的图标图片拖到drawable-XX文件夹下,然后你打开AndroidManifest.xml这个配置清单文件找到application标签里的这句android:ico ...

  4. eclipse设置显示代码行数(转)

    (转自:http://jingyan.baidu.com/article/b2c186c89b7023c46ef6ff27.html) 载入eclipse的主页面,默认以英文版的eclipse为例 点 ...

  5. S5PV210的内存分配研究分析

    S5PV210内存一般会使用SDRAM和DDR2 (DDR SDRAM),SDRAM的uboot启动网络已经有很多资料的,对于DDR2还有有很多疑惑,如果有错误的地方,请大家一定指出,醍醐灌顶,不胜感 ...

  6. C# 计算文件MD5

    因工作需要对文件进行是否被修改判断,整理的一段生成文件MD5码的代码: 1: public class FileHelper 2: { 3: /// <summary> 4: /// 对文 ...

  7. NYOJ-取石子(六)

    取石子(六) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 最近TopCoder的PIAOYI和HRDV很无聊,于是就想了一个游戏,游戏是这样的:有n堆石子,两个人轮流 ...

  8. Unity StartCoroutine 和 yield return 深入研究

    http://www.cnblogs.com/fly-100/p/3910515.html

  9. 由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例。该连接将关闭。

    错误:由于启动用户实例的进程时出错,导致无法生成SQL Server的用户实例. 原因:添加安装SQLEXPRESS时,估计装在了不同的目录下: 解决方法:关闭Sqlserver及相关的程序,删除目录 ...

  10. oracl函数

    一:大小写函数 1:lower()全部小写 select lower('HEHE') lowerwords from dual 2:upper()全部大写 3:initcap()首字母大写 4:con ...