ALDS1_5_B-MergeSort.

Description:

Write a program of a Merge Sort algorithm implemented by the following pseudocode. You should also report the number of comparisons in the Merge function.

Merge(A, left, mid, right)

n1 = mid - left;

n2 = right - mid;

create array L[0...n1], R[0...n2]

for i = 0 to n1-1

do L[i] = A[left + i]

for i = 0 to n2-1

do R[i] = A[mid + i]

L[n1] = SENTINEL

R[n2] = SENTINEL

i = 0;

j = 0;

for k = left to right-1

if L[i] <= R[j]

then A[k] = L[i]

i = i + 1

else A[k] = R[j]

j = j + 1

Merge-Sort(A, left, right){

if left+1 < right

then mid = (left + right)/2;

call Merge-Sort(A, left, mid)

call Merge-Sort(A, mid, right)

call Merge(A, left, mid, right)

Input:

In the first line n is given. In the second line, n integers are given.

Output:

In the first line, print the sequence S. Two consequtive elements should be separated by a space character.

In the second line, print the number of comparisons.

Constraints:

n ≤ 500000

0 ≤ an element in S ≤ 109

SampleInput:

10

8 5 9 2 6 3 7 1 10 4

SampleOutput:

1 2 3 4 5 6 7 8 9 10

34

Codes:
//#define LOCAL

#include <cstdio>

#define M 500010
#define SENTINEL 2000000000
int s = 0, A[M], L[M], R[M]; void merge(int l, int m, int r) {
int i, j, k, n1 = m-l, n2 = r-m;
L[n1] = R[n2] = SENTINEL;
for(i=0; i<n1; ++i) L[i] = A[l+i];
for(i=0; i<n2; ++i) R[i] = A[m+i];
i = j = 0;
for(k=l; k<r; ++k) {
if(L[i] <= R[j]) A[k] = L[i++];
else A[k] = R[j++]; ++s;
}
} void mergeSort(int l, int r) {
if(l+1 < r) {
int m = (l+r)/2;
mergeSort(l, m);
mergeSort(m, r);
merge(l, m, r);
}
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, n;
scanf("%d", &n);
for(i=0; i<n; ++i) scanf("%d", &A[i]); mergeSort(0, n);
for(i=0; i<n; ++i) {
if(i) printf(" ");
printf("%d", A[i]);
}
printf("\n%d\n", s); return 0;
}

ALDS1_6_B-Partition.

Description:

Quick sort is based on the Divide-and-conquer approach. In QuickSort(A, p, r), first, a procedure Partition(A, p, r) divides an array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, inturn, less than or equal to each element of A[q+1..r]. It also computes the index q.

In the conquer processes, the two subarrays A[p..q-1] and A[q+1..r] are sorted by recursive calls of QuickSort(A, p, q-1) and QuickSort(A, q+1, r).

Your task is to read a sequence A and perform the Partition based on the following pseudocode:

Partition(A, p, r)

1 x = A[r]

2 i = p-1

3 for j = p to r-1

4 do if A[j] <= x

5 then i = i+1

6 exchange A[i] and A[j]

7 exchange A[i+1] and A[r]

8 return i+1

Input:

The first line of the input includes an integer n, the number of elements in the sequence A.

In the second line, n elements of the sequence are given separated by space characters.

Output:

Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character. The element which is selected as the pivot of the partition should be indicated by [ ].

Constraints:

1 ≤ n ≤ 100,000

SampleInput:

12

13 19 9 5 12 8 7 4 21 2 6 11

SampleOutput:

9 5 8 7 4 2 6 [11] 21 13 19 12

Codes:
//#define LOCAL

#include <cstdio>

#define M 100010
int A[M]; int partition(int p, int r) {
int i = p-1, a = A[r], j, t;
for(j=p; j<r; ++j) {
if(A[j] <= a) {
++i;
t = A[i]; A[i] = A[j]; A[j] = t;
}
}
A[r] = A[i+1]; A[i+1] = a;
return i+1;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, n, q;
scanf("%d", &n);
for(i=0; i<n; ++i) scanf("%d", &A[i]); q = partition(0, n-1);
for(i=0; i<n; ++i) {
if(i) printf(" ");
if(i == q) printf("[%d]", A[i]);
else printf("%d", A[i]);
}
printf("\n"); return 0;
}

ALDS1_6_C-QuickSort.

Description:

Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:

Partition(A, p, r)

1 x = A[r]

2 i = p-1

3 for j = p to r-1

4 do if A[j] <= x

5 then i = i+1

6 exchange A[i] and A[j]

7 exchange A[i+1] and A[r]

8 return i+1

Quicksort(A, p, r)

1 if p < r

2 then q = Partition(A, p, r)

3 run Quicksort(A, p, q-1)

4 run Quicksort(A, q+1, r)

Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.

Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).

Input:

The first line contains an integer n, the number of cards.

n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.

Output:

In the first line, print the stability ("Stable" or "Not stable") of this output.

In the following lines, print the arranged cards in the same manner of that of the input.

Constraints:

1 ≤ n ≤ 100,000

1 ≤ the number of a card ≤ 109

There are no identical card in the input

SampleInput1:

6

D 3

H 2

D 1

S 3

D 2

C 1

SampleOutput1:

Not stable

D 1

C 1

D 2

H 2

D 3

S 3

SampleInput2:

2

S 1

H 1

SampleOutput2:

Stable

S 1

H 1

Codes:
//#define LOCAL

#include <cstdio>

#define M 100010
#define S 2000000000
struct Card {
char p;
int q;
};
Card A[M], B[M], L[M], R[M]; void merge(int l, int m, int r) {
int i, j, k, n1 = m-l, n2 = r-m;
for(i=0; i<n1; ++i) L[i] = A[l+i];
for(i=0; i<n2; ++i) R[i] = A[m+i];
i = j = 0, L[n1].q = R[n2].q = S;
for(k=l; k<r; ++k) {
if(L[i].q <= R[j].q) A[k] = L[i++];
else A[k] = R[j++];
}
} void mergeSort(int l, int r) {
if(l+1 < r) {
int m = (l+r)/2;
mergeSort(l, m);
mergeSort(m, r);
merge(l, m, r);
}
} int partition(int l, int r) {
int i = l-1, j, x = B[r].q;
for(j=l; j<r; ++j) {
if(B[j].q <= x) {
++i;
Card t = B[j]; B[j] = B[i]; B[i] = t;
}
}
Card t = B[r]; B[r] = B[i+1]; B[i+1] = t;
return i+1;
} void quickSort(int l, int r) {
int m;
if(l < r) {
m = partition(l, r);
quickSort(l, m-1);
quickSort(m+1, r);
}
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, m, n;
char t[10];
scanf("%d", &n);
for(i=0; i<n; ++i) {
scanf("%s%d", t, &m);
A[i].p = B[i].p = t[0];
A[i].q = B[i].q = m;
} mergeSort(0, n);
quickSort(0, n-1);
for(i=0; i<n; ++i) {
if(A[i].p != B[i].p) {
printf("Not stable\n");
break;
}
}
if(i == n) printf("Stable\n");
for(i=0; i<n; ++i) {
printf("%c %d\n", B[i].p, B[i].q);
} return 0;
}

ALDS1_6_A-CountingSort.

Description:

Counting sort can be used for sorting elements in an array which each of the n input elements is an integer in the range 0 to k. The idea of counting sort is to determine, for each input element x, the number of elements less than x as C[x]. This information can be used to place element x directly into its position in the output array B. This scheme must be modified to handle the situation in which several elements have the same value. Please see the following pseudocode for the detail:

Counting-Sort(A, B, k)

1 for i = 0 to k

2 do C[i] = 0

3 for j = 1 to length[A]

4 do C[A[j]] = C[A[j]]+1

5 /* C[i] now contains the number of elements equal to i /

6 for i = 1 to k

7 do C[i] = C[i] + C[i-1]

8 /
C[i] now contains the number of elements less than or equal to i */

9 for j = length[A] downto 1

10 do B[C[A[j]]] = A[j]

11 C[A[j]] = C[A[j]]-1

Write a program which sorts elements of given array ascending order based on the counting sort.

Input:

The first line of the input includes an integer n, the number of elements in the sequence.

In the second line, n elements of the sequence are given separated by spaces characters.

Output:

Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

Constraints:

1 ≤ n ≤ 2,000,000

0 ≤ A[i] ≤ 10,000

SampleInput:

7

2 5 1 3 2 3 0

SampleOutput:

0 1 2 2 3 3 5

Codes:
//#define LOCAL

#include <cstdio>

#define M 10010
#define VM 2000010
int A[VM], B[M], C[VM]; int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, n;
scanf("%d", &n);
for(i=1; i<=n; ++i) {
scanf("%d", &A[i]);
++B[A[i]];
} for(i=1; i<M; ++i) B[i] += B[i-1];
for(i=n; i>=1; --i) C[B[A[i]]--] = A[i];
for(i=1; i<=n; ++i) {
if(i > 1) printf(" ");
printf("%d", C[i]);
}
printf("\n"); return 0;
}

ALDS1_5_D-TheNumberOfInversions.

Codes:
#include <iostream>
using namespace std; #define MAX 200000
#define SENTINEL 2000000000
typedef long long llong;
int L[MAX/2+2], R[MAX/2+2]; llong merge(int A[], int n, int left, int mid, int right) {
llong cnt = 0;
int i, j, k, n1 = mid-left, n2 = right-mid;
for(i=0; i<n1; ++i) L[i] = A[left+i];
for(i=0; i<n2; ++i) R[i] = A[mid+i];
i = j = 0, L[n1] = R[n2] = SENTINEL;
for(k=left; k<right; ++k) {
if(L[i] <= R[j]) A[k] = L[i++];
else A[k] = R[j++];
cnt += n1-i;
}
return cnt;
} llong mergeSort(int A[], int n, int left, int right) {
int mid;
llong v1, v2, v3;
if(left+1 < right) {
mid = (left+right)/2;
v1 = mergeSort(A, n, left, mid);
v2 = mergeSort(A, n, mid, right);
v3 = merge(A, n, left, mid, right);
return v1+v2+v3;
} else return 0;
} int main()
{
int n, i, A[MAX];
cin >> n;
for(i=0; i<n; ++i) cin >> A[i]; int ans = mergeSort(A, n, 0, n);
cout << ans << endl; return 0;
}

ALDS1_6_D-MinimumCostSort.

Codes:
#include <iostream>
#include <algorithm>
using namespace std; static const int MAX = 1000;
static const int VMAX = 10000;
int n, s, A[MAX], B[MAX], T[VMAX+1]; int solve() {
int i, ans = 0;
bool V[MAX];
for(i=0; i<n; ++i) {B[i] = A[i]; V[i] = false;}
sort(B, B+n);
for(i=0; i<n; ++i) T[B[i]] = i;
for(i=0; i<n; ++i) {
if(V[i]) continue;
int S = 0, an = 0, cur = i, m = VMAX;
while(1) {
V[cur] = true; ++an;
int v = A[cur]; m = min(m, v);
S += v; cur = T[v];
if(V[cur]) break;
}
ans += min(S+(an-2)*m, m+S+(an+1)*s);
}
return ans;
} int main()
{
cin >> n;
s = VMAX;
for(int i=0; i<n; ++i) {
cin >> A[i];
s = min(s, A[i]);
}
int ans = solve();
cout << ans << endl; return 0;
}

AOJ/高等排序习题集的更多相关文章

  1. AOJ/初等排序习题集

    ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...

  2. 贪心+拓扑排序 AOJ 2456 Usoperanto

    题目传送门 题意:给出一条链,比如x连到y,x一定要在y的左边,且代价是这条链经过的点的权值和,问如何排序使得代价最小 分析:类似拓扑排序,先把入度为0的点入队,把指向该点的所有点按照权值排序,保证这 ...

  3. AOJ/数据结构习题集

    ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...

  4. AOJ/搜索递归分治法习题集

    ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...

  5. AOJ/树二叉搜索树习题集

    ALDS1_7_A-RootedTree. Description: A graph G = (V, E) is a data structure where V is a finite set of ...

  6. AOJ/堆与动态规划习题集

    ALDS1_9_A-CompleteBinaryTree. Codes: //#define LOCAL #include <cstdio> int parent(int i) { ret ...

  7. AOJ/树与二叉搜索树习题集

    ALDS1_7_A-RootedTree. Description: A graph G = (V, E) is a data structure where V is a finite set of ...

  8. AOJ/搜索与递归及分治法习题集

    ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...

  9. 数据结构与算法之PHP排序算法(归并排序)

    一.基本思想 归并排序算法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,使每个子序列有序,再将已有序的子序列合并,得到完全有序的序列.该算法是采用分治法(Divid ...

随机推荐

  1. ajax传字符串时出现乱码问题的解决

    字符乱码的解决: 第一种在@RequestMapping中添加 @RequestMapping(value="queryAllToTree",method=RequestMetho ...

  2. 测试Kaggle kernel commit 是否会删除以前的output

    在kaggle上创建kernel,加入如下代码. 连续运行两次,可以看到保存的文件名字不一样,且无论运行错少次,都只有一个输出文件. 这说明,kaggle上的kernel每次commit运行,都会清空 ...

  3. excel快捷键如下:

    ALT+ 空格键,然后按下 X ALT+ 空格键,然后按下 R  首先打开表格,在A1对角用鼠标左键单击,界面会全部选中,然后调整字体大小框里的数字,回车,表格就变大了. 同时按Alt和E,再按L   ...

  4. Shell中的运算

    1.运算方式及运算符号 2.SHELL 中常用的运算命令 3.相关操作演示 1.用脚本写一个10秒倒计时 脚本的执行: 2.编写脚本,1分10秒的倒计时 执行脚本: 3.编写脚本,制作一个计算器 脚本 ...

  5. 从零开始实现简单 RPC 框架 7:网络通信之自定义协议(粘包拆包、编解码)

    当 RPC 框架使用 Netty 通信时,实际上是将数据转化成 ByteBuf 的方式进行传输. 那如何转化呢?可不可以把 请求参数 或者 响应结果 直接无脑序列化成 byte 数组发出去? 答:直接 ...

  6. 简单C++线程池

    简单C++线程池 Java 中有一个很方便的 ThreadPoolExecutor,可以用做线程池.想找一下 C++ 的类似设施,尤其是能方便理解底层原理可上手的.网上找到的 demo,基本都是介绍的 ...

  7. 硕盟type-c转接头HDMI+VGA+USB3.0+PD3.0四合一多功能扩展坞

    硕盟SM-T54是一款 TYPE C转HDMI+VGA+USB3.0+PD3.0四合一多功能扩展坞,支持四口同时使用,您可以将含有USB 3.1协议的电脑主机,通过此产品连接到具有HDMI或VGA的显 ...

  8. Python - poetry(4)管理环境

    环境隔离 poetry 核心之一:使项目环境隔离,意味着始终和本地全局 Python 环境隔离 poetry 首先会检查当前项目是否在虚拟环境中运行:如果是将直接使用它,而不创建新的:如果不是,poe ...

  9. 【第二篇】- Maven 环境配置之Spring Cloud直播商城 b2b2c电子商务技术总结

    Maven 环境配置 Maven 是一个基于 Java 的工具,所以要做的第一件事情就是安装 JDK. 如果你还未安装 JDK,可以参考我们的 Java 开发环境配置. 系统要求 项目 要求 JDK ...

  10. 如何在win10中Java中JDK的安装和path,classpath的环境配置

    1,第一步,不用说肯定是去下一个java JDK了.目前最新版本的java JDK应该是JDK 7.0,这个就自己去百度一下了,好多网站都可以找到.2,第二步就是安装JDK虚拟机了,按照它里面的提示一 ...