-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDD4.cpp
68 lines (60 loc) · 1.02 KB
/
PDD4.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Board
{
public:
Board(int n, int m):
rows_(n), cols_(m), mat_(n * m, 0) {}
void BuildMat()
{
for (int i = 0; i != rows_; ++i)
{
for (int j = 0; j != cols_; ++j)
mat_[i * cols_ + j] = (i+1) * (j+1);
}
}
int KthLarge(int k)
{
std::sort(mat_.begin(), mat_.end(), std::greater<int>());
return mat_[k-1];
}
private:
int rows_;
int cols_;
vector<int> mat_;
};
int KthLarge(int n, int m, int k) // find the (n*m-k) least
{
auto IsValid = [n, m](int i, int j) -> bool
{
return (i >= 1) && (i <= n)
&& (j >= 1) && (j <= m);
};
k = n * m + 1 - k;
for (int i = 1; i <= k; ++i)
{
int j = k / i;
if (IsValid(i, j))
{
if (i * j == k)
{
return i*j;
}
else
{
return min( (i-1)*j, i*(j-1));
}
}
}
return 0;
}
int main()
{
int n, m, k;
cin >> n >> m >> k;
// io done
cout << KthLarge(n, m, k);
return 0;
}