Problem C. Sorting- 分组后 快速排序

Problem

Alex and Bob are brothers and they both enjoy reading very much. They have widely different tastes on books so they keep their own books separately. However, their father thinks it is good to promote exchanges if they can put their books together. Thus he has bought an one-row bookshelf for them today and put all his sons' books on it in random order. He labeled each position of the bookshelf the owner of the corresponding book ('Alex' or 'Bob').

Unfortunately, Alex and Bob went outside and didn't know what their father did. When they were back, they came to realize the problem: they usually arranged their books in their own orders, but the books seem to be in a great mess on the bookshelf now. They have to sort them right now!!

Each book has its own worth, which is represented by an integer. Books with odd values of worth belong to Alex and the books with even values of worth belong to Bob. Alex has a habit of sorting his books from the left to the right in an increasing order of worths, while Bob prefers to sort his books from the left to the right in a decreasing order of worths.

At the same time, they do not want to change the positions of the labels, so that after they have finished sorting the books according their rules, each book's owner's name should match with the label in its position.

Here comes the problem. A sequence of N values s0s1, ..., sN-1 is given, which indicates the worths of the books from the left to the right on the bookshelf currently. Please help the brothers to find out the sequence of worths after sorting such that it satisfies the above description.

Input

The first line of input contains a single integer T, the number of test cases. Each test case starts with a line containing an integer N, the number of books on the bookshelf. The next line contains N integers separated by spaces, representing s0s1, ..., sN-1, which are the worths of the books.

Output

For each test case, output one line containing "Case #X: ", followed by t0t1, ..., tN-1 in order, and separated by spaces. X is the test case number (starting from 1) and t0t1, ..., tN-1 forms the resulting sequence of worths of the books from the left to the right.

Limits

1 ≤ T ≤ 30.

Small dataset

1 ≤ N ≤ 100
-100 ≤ si ≤ 100

Large dataset

1 ≤ N ≤ 1000
-1000 ≤ si ≤ 1000

Sample


Input 
 

Output 
 
2
5
5 2 4 3 1
7
-5 -12 87 2 88 20 11
Case #1: 1 4 2 3 5
Case #2: -5 88 11 20 2 -12 87

推荐指数:

来源:https://code.google.com/codejam/contest/2924486/dashboard#s=p2

有一个数组中有若干数,要求区分奇数偶数之后进行排序。

但是,奇数和偶数在原数组中所使用的位置不能变。

思路:在读入数据的时候就将数据分入到两个数组当中a,b。

用一个数组num记录每个位置本来是放的偶数还是奇数。(代码中直接使用了输入的数)。

最后遍历num数组,遇到偶数位置,从b中取一个数加入到数组。遇到奇数位置,从a中取一个数加入到数组。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<stack>
#include<algorithm>
#include<math.h>
using namespace std;
//freopen("a.in", "r", stdin);  
//freopen("a.out", "w", stdout); 
int compare_a(const void *a,const void *b){
	return *((int *)a)- *((int *)b);
}
int compare_b(const void *a,const void *b){
	return *((int *)b)- *((int *)a);
}
int main()
{
	//freopen("a.in", "r", stdin);  
  //  freopen("a.out", "w", stdout); 
	int t,n,i;
	scanf("%d",&t);
	int caseid;
	for(caseid=1;caseid<=t;caseid++){
		scanf("%d",&n);
		int *num=new int [n];
		int *a=new int [n];//odd
		int *b=new int [n];
		int ak=0,bk=0;
		for(i=0;i<n;i++){
			scanf("%d",&num[i]);
			if(abs(num[i])&1==1)
				a[ak++]=num[i];
			else
				b[bk++]=num[i];
		}
		qsort(a,ak,sizeof(int),compare_a);
		qsort(b,bk,sizeof(int),compare_b);
		int ai=0,bi=0;
		for(i=0;i<n;i++){
			if(abs(num[i])&1==1){
				num[i]=a[ai];
				ai++;
			}
			else{
				num[i]=b[bi];
				bi++;
			}
		}
		printf("Case #%d:",caseid);
		for(i=0;i<n;i++)
			printf(" %d",num[i]);
		printf("\n");
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值