-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_score.cpp
61 lines (55 loc) · 1.29 KB
/
calc_score.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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int di[] = {+1,-1, 0, 0};
const int dj[] = { 0, 0,+1,-1};
struct CalcScore {
const int N;
vector<vector<double>> b;
CalcScore(int N, const vector<vector<double>>& b) : N(N), b(b) {}
pair<int, double> f(int i, int j) {
if (b[i][j] <= 0) return {0, 1e+300};
int n = 1;
double m = b[i][j];
b[i][j] = 0;
for (int k = 0; k < 4; ++ k) {
int ii = i + di[k];
int jj = j + dj[k];
if (0 <= ii && ii < N && 0 <= jj && jj < N) {
auto r = f(ii, jj);
n += r.first;
m = min(m, r.second);
}
}
return {n, m};
}
double calc() {
double score = 0;
for (int i = 0; i < N; ++ i) for (int j = 0; j < N; ++ j) if (b[i][j] > 0) {
auto r = f(i, j);
score += r.first * r.second;
}
return score;
}
};
int main(int argc, char** argv) {
int N;
ifstream fin(argv[1]);
fin >> N;
vector<vector<double>> A(N, vector<double>(N));
for (int i = 0; i < N; ++ i) for (int j = 0; j < N; ++ j) fin >> A[i][j];
int n;
while (cin >> n) {
vector<vector<double>> b(N, vector<double>(N));
for (int k = 0; k < n; ++ k) {
int idx;
double cnt;
cin >> idx >> cnt;
int i = idx / N, j = idx % N;
b[i][j] = A[i][j] / cnt;
}
printf("%.15f\n", CalcScore(N, b).calc());
fflush(stdout);
}
}