-
Notifications
You must be signed in to change notification settings - Fork 0
/
young.cpp
129 lines (105 loc) · 2.75 KB
/
young.cpp
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
/*************************************************************************
> File Name: young.cpp
> Author: Louis1992
> Mail: [email protected]
> Blog: http://gzc.github.io
> Created Time: Tue Jun 23 20:05:44 2015
************************************************************************/
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
int EXTRACT_MIN(vector<vector<int> > &young)
{
int minimum = young[0][0];
int i(0),j(0);
while(i < young.size() && j < young[0].size())
{
int cur = young[i][j];
int ori(i),orj(j);
int right(INT_MAX),down(INT_MAX);
if(i < young.size()-1) down = young[i+1][j];
if(j < young[0].size()-1) right = young[i][j+1];
if(right == INT_MAX && down == INT_MAX)
{
young[i][j] = INT_MAX;
break;
}
else if(down <= right)
i++;
else
j++;
swap(young[i][j], young[ori][orj]);
}
return minimum;
}
void INSERT(vector<vector<int> > &young, int v)
{
int i = young.size()-1;
int j = young[0].size()-1;
young[i][j] = v;
while(i >= 0 && j >= 0)
{
int left(INT_MIN),up(INT_MIN);
if(i > 0) up = young[i-1][j];
if(j > 0) left = young[i][j-1];
if(v >= up && v >= left)
break;
else if(v < up)
{
swap(young[i][j], young[i-1][j]);
i--;
} else {
swap(young[i][j], young[i][j-1]);
j--;
}
}
}
bool EXIST(vector<vector<int> > &young, int v)
{
int i(young.size()-1),j(0);
while(i >= 0 && j < young[0].size())
{
cout << i << " " << j << endl;
if(young[i][j] == v)
return true;
if(young[i][j] < v)
j++;
else
i--;
}
return false;
}
void print_young(vector<vector<int> > &young)
{
for(int i = 0;i < young.size();i++)
{
for(int j = 0;j < young[0].size();j++)
{
cout << young[i][j] << " ";
}
cout << endl;
}
}
int main() {
int arr1[4] = {2,3,12,INT_MAX};
int arr2[4] = {4,8,16,INT_MAX};
int arr3[4] = {5,9,INT_MAX,INT_MAX};
int arr4[4] = {14,INT_MAX,INT_MAX,INT_MAX};
vector<int> v1(arr1,arr1+4);
vector<int> v2(arr2,arr2+4);
vector<int> v3(arr3,arr3+4);
vector<int> v4(arr4,arr4+4);
vector<vector<int> > v;
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
for(int i = 0;i < 9;i++)
cout << EXTRACT_MIN(v) << endl;
for(int i = 0;i < 9;i++)
INSERT(v, i);
print_young(v);
cout << EXIST(v, 8) << endl;
return 0;
}