1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
| #include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <ctime>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <utility>
#include <complex>
#include <vector>
#include <bitset>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <windows.h>
#include <process.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) int(a.size())
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define x1 ___x1
#define y1 ___y1
#define X first
#define ft first
#define Y second
#define sc second
const int N = 10 * 1000;
const int M = 10;
int a[N][M], b[N][M], c[N][M];
inline void rand_gen (int a[N][M])
{
forn (i, N)
forn (j, M)
a[i][j] = rand() % 20 - 10;
}
inline int cmp (int a[M], int b[M])
{
forn (i, M)
{
if (a[i] > b[i])
return -1;
if (a[i] < b[i])
return 1;
}
return 0;
}
inline void qSort(int a[N][M], int l, int r)
{
int i = l, j = r;
int x[M];
memcpy(x, a[(l + r) / 2], sizeof(x));
do
{
while (cmp(a[i], x) == 1)
i++;
while (cmp(a[j], x) == -1)
j--;
if (i <= j)
{
int tmp[M];
memcpy(tmp, a[i], sizeof(tmp));
memcpy(a[i], a[j], sizeof(tmp));
memcpy(a[j], tmp, sizeof(tmp));
i++;
j--;
}
}
while(i < j);
if (l < j)
qSort(a, l, j);
if (i < r)
qSort(a, i, r);
}
unsigned __stdcall ThreadFunc1(void * arg)
{
qSort(a, 0, N - 1);
_endthreadex( 0 );
return 0;
};
unsigned __stdcall ThreadFunc2(void * arg)
{
qSort(b, 0, N - 1);
_endthreadex( 0 );
return 0;
};
unsigned __stdcall ThreadFunc3(void * arg)
{
qSort(c, 0, N - 1);
_endthreadex( 0 );
return 0;
};
inline void solve ()
{
rand_gen(a);
rand_gen(b);
rand_gen(c);
unsigned uThreadIDs[3];
HANDLE hThreads[3];
hThreads[0] = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc1, NULL, 0, &uThreadIDs[0]);
hThreads[1] = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc2, NULL, 0, &uThreadIDs[1]);
hThreads[2] = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc3, NULL, 0, &uThreadIDs[2]);
WaitForMultipleObjects(3, hThreads, TRUE, INFINITE );
CloseHandle( hThreads[0] );
CloseHandle( hThreads[1] );
CloseHandle( hThreads[2] );
//qSort(a, 0, N - 1);
//qSort(b, 0, N - 1);
//qSort(c, 0, N - 1);
forn (i, N)
{
forn (j, M)
cout << a[i][j] << ' ';
cout << endl;
}
cout << "------------" << endl;
forn (i, N)
{
forn (j, M)
cout << b[i][j] << ' ';
cout << endl;
}
cout << "------------" << endl;
forn (i, N)
{
forn (j, M)
cout << c[i][j] << ' ';
cout << endl;
}
}
int main ()
{
freopen ("output.txt", "w", stdout);
srand(time(NULL));
solve();
cerr << clock() << endl;
return 0;
} |