Poj1009

最近开始玩poj, 做了几个后发现个难的。代码基本上是http://leonacwa.github.io/acmicpc/2011/04/27/poj-1009-edge-detection/的。明确思路后改动了两个地方,一个是二分查找Length那块,另外是把原作者对大数据的优化取消了(因为现在自带了对大数据的优化)。

题目见http://poj.org/problem?id=1009

因为edge其实只和上下左右一个元素相关,于是可以把矩阵展开成三行,然后对开头做特殊处理即可。

数据结构

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include<iostream>
#include<fstream>
//Thanks to http://leonacwa.github.io/acmicpc/2011/04/27/poj-1009-edge-detection/
using namespace std;
struct Pair{
int a, b, s;
};
struct Pair p[1024];
int w, px, pos, pre, pres, totalLen;
inline int abs(int a){
return a >= 0 ? a : -a;
}
inline int max(int a, int b){
return a >= b ? a : b;
}
inline int min(int a, int b){
return a <= b ? a : b;
}
void printO(void){
cout << pre << ' ' << pres << endl;
}
int getUp(int pos){
int tpos = pos - w;
if (tpos < 0)
tpos = pos;
return tpos;
}
int getDown(int pos){
int tpos = pos + w;
if (tpos >= totalLen)
tpos = pos;
return tpos;
}
int getLeft(int pos){
int tpos = pos - 1;
if (tpos / w != pos / w)
tpos = pos;
return tpos;
}
int getRight(int pos){
int tpos = pos + 1;
if (tpos / w != pos / w || tpos >= totalLen)
tpos = pos;
return tpos;
}
int getLeftUp(int pos){
int tpos = pos - w - 1;
if (tpos < 0 || tpos / w + 1 != pos / w)
tpos = pos;
return tpos;
}
int getRightUp(int pos){
int tpos = pos - w + 1;
if (tpos < 0 || tpos / w == pos / w)
tpos = pos;
return tpos;
}
int getLeftDown(int pos){
int tpos = pos + w - 1;
if (tpos / w == pos / w || tpos >= totalLen)
tpos = pos;
return tpos;
}
int getRightDown(int pos){
int tpos = pos + w + 1;
if (tpos / w != pos / w + 1 || tpos >= totalLen)
tpos = pos;
return tpos;
}
int getValue(int pos){
pos++;
int lo = 1, hi = px - 1, mid = px / 2;
while (lo < hi){
mid = (lo + hi) >> 1;
if (pos <= p[mid].s)
hi = mid;
else
lo = mid + 1;
}
return p[hi].a;
}
int getLenPos(int pos){
pos++;
int lo = 1, hi = px - 1, mid = px / 2;
while (lo < hi){
mid = (lo + hi)>>1;
if (pos <= p[mid].s)
hi = mid;
else
lo = mid + 1;
}
return p[hi].s;
}
int getLen(int pos){
return getLenPos(pos) - pos ;
}
int getMax(int pos){
int value = getValue(pos);
int a = 0;
int tpos;
tpos = getUp(pos);
if (pos != tpos)
a = abs(value - getValue(tpos));
tpos = getDown(pos);
if (pos != tpos)
a = max(a, abs(value - getValue(tpos)));
tpos = getLeft(pos);
if (pos != tpos)
a = max(a, abs(value - getValue(tpos)));
tpos = getRight(pos);
if (pos != tpos)
a = max(a, abs(value - getValue(tpos)));
tpos = getLeftUp(pos);
if (pos != tpos)
a = max(a, abs(value - getValue(tpos)));
tpos = getLeftDown(pos);
if (pos != tpos)
a = max(a, abs(value - getValue(tpos)));
tpos = getRightUp(pos);
if (pos != tpos)
a = max(a, abs(value - getValue(tpos)));
tpos = getRightDown(pos);
if (pos != tpos)
a = max(a, abs(value - getValue(tpos)));
return a;
}
int getMidMax(int pos){ //when a same row;
int value = getValue(pos);
int a = abs(value - getValue(getUp(pos)));
return max(a, abs(value - getValue(getDown(pos))));
}
int main(){
ifstream cin("input.txt");
while (cin>>w && w){
cout << w << endl;
int a, b, c;
p[0].s = 0;
px = 1;
while (cin>>a>>b && !(a==0 && b==0)){
p[px].a = a;
p[px].b = b;
p[px].s = p[px - 1].s + b;
px++;
}
totalLen = p[px - 1].s;
int value = p[1].a;
pres = 1;
pre = abs(value - getValue(getDown(0)));
pre = max(pre, abs(value - getValue(getRight(0))));
pre = max(pre, abs(value - getValue(getRightDown(0))));
pos = 1;
int pos_len, up_len, down_len, min_len,pos_up,pos_down,pos_left;
while (pos<totalLen){
pos_len = getLen(pos);
pos_up = getUp(pos);
pos_down = getDown(pos);
pos_left = getLeft(pos);
up_len = getLen(pos_up);
if (pos_down != pos)
down_len = getLen(getDown(pos));
else
down_len = pos_len; //special for last row
if (pos_up==pos)
up_len = up_len <= (w-pos)?up_len:(w-pos); // special for first row
min_len = min(pos_len, min(up_len, down_len));
if (min_len == 1){
a = getMax(pos);
if (a == pre){
pres++;
}
else{
printO();
pre = a;
pres = 1;
}
pos++;
}
else if (min_len == 2){
a = getMax(pos);
b = getMax(getRight(pos));
if (a == pre){
pres++;
}
else{
printO();
pre = a;
pres = 1;
}
if (b == pre){
pres++;
}
else{
printO();
pre = b;
pres = 1;
}
pos += 2;
}
else{
//more than 3 cols
a = getMax(pos);
b = getMax(pos + min_len - 1);
c = getMidMax(pos + 1);
if (a == pre){
pres++;
}
else{
printO();
pre = a;
pres = 1;
}
if (c == pre){
pres += min_len - 2;
}
else{
printO();
pre = c;
pres = min_len - 2;
}
if (b == pre){
pres++;
}
else{
printO();
pre = b;
pres = 1;
}
pos += min_len;
}
}
printO();
cout << "0 0" << endl;
}
cout << 0 << endl;
//system("Pause");
}

评论