-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path552.cpp
More file actions
109 lines (94 loc) · 3.11 KB
/
552.cpp
File metadata and controls
109 lines (94 loc) · 3.11 KB
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
#include "iostream"
#include "cstdio"
#include "cstdlib"
#include "algorithm"
#include "cmath"
#include "cstring"
#include "string"
#include "map"
#include "set"
#include "stack"
#include "list"
#include "vector"
#include "queue"
using namespace std;
#define PI acos(-1)
#define MP make_pair
#define PB push_back
#define VI vector < int >
#define PII pair < int, int >
#define LL long long
#define SET(v,i) memset(v, i, sizeof(v))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define FORN(i,a,b) for (int i = (a); i < (b); i++)
#define DOWN(i,a,b) for (int i = (a); i > (b); i--)
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FREOPEN freopen("a.in", "r", stdin); freopen("a.out", "w", stdout)
class FoxAndFlowerShopDivOne{
public:
int call (vector < string > f, int md) {
int sump [50][50], suml[50][50];
int upper [2020], lower[2020];
int r, c;
int result = -1;
r = f.size();
c = f[0].size();
//prepare sump and suml
//we convert the grid into 1-indexed for convenience
SET (sump, 0);
SET (suml, 0);
FOR (i, 1, r)
FOR (j, 1, c) {
sump[i][j] = sump[i - 1][j] + sump[i][j - 1] - sump[i - 1][j - 1] + (f[i - 1][j - 1] == 'P');
suml[i][j] = suml[i - 1][j] + suml[i][j - 1] - suml[i - 1][j - 1] + (f[i - 1][j - 1] == 'L');
}
//consider all the possible line D
FOR (d, 1, r) {
SET (upper, -1);
SET (lower, -1);
//amongst all the upper rectangles that have the difference of number 'P' and 'L'
//is x, the rectangle has the maximum number of flowers will cover upper[x + 1000] flowers.
//similar difination for lower array.
//consider all the upper rectangles
FOR (x1, 1, d)
FOR (y1, 1, c)
FOR (x2, x1, d)
FOR (y2, y1, c) {
//counting Ls and Ps
int L = suml[x2][y2] - suml[x1 - 1][y2] - suml[x2][y1 - 1] + suml[x1 - 1][y1 - 1];
int P = sump[x2][y2] - sump[x1 - 1][y2] - sump[x2][y1 - 1] + sump[x1 - 1][y1 - 1];
upper[P - L + 1000] = max(upper[P - L + 1000], P + L);
}
FOR (x1, d + 1, r)
FOR (y1, 1, c)
FOR (x2, x1, r)
FOR (y2, y1, c) {
//counting Ls and Ps
int L = suml[x2][y2] - suml[x1 - 1][y2] - suml[x2][y1 - 1] + suml[x1 - 1][y1 - 1];
int P = sump[x2][y2] - sump[x1 - 1][y2] - sump[x2][y1 - 1] + sump[x1 - 1][y1 - 1];
lower[P - L + 1000] = max(lower[P - L + 1000], P + L);
}
FOR (x, 0, 2000)
if (upper[x] >= 0)
FOR (y, 0, 2000)
if (lower[y] >= 0)
if (abs(x + y - 2000) <= md) result = max (result, upper[x] + lower[y]);
}
return result;
}
int theMaxFlowers(vector < string > flowers, int maxDiff) {
int r, c;
r = flowers.size();
c = flowers[0].size();
//rotated version of flowers
vector < string > rotated;
FOR (i, 0, c - 1) {
string t = "";
FOR (j, 0, r - 1) t += flowers[j][c - i - 1];
rotated.PB(t);
}
return max (call(flowers, maxDiff), call(rotated, maxDiff));
}
};