POJ 3276 [Face The Right Way] 题解

Subject to the effect

n cow in a row, some cows facing forward, facing some cows after each operation can continuously change direction cow K; K a request, such that a minimum number of operations. K, and the output minimum number of operations. When there are a plurality of K satisfying the condition, the minimum output of K.

Topic analysis

For an interval, the multiple inversion operation is meaningless; additional reverse the order of the results is not affected. This question therefore need only consider all operable interval (i.e., the length of the interval K) need to be reversed.

Consider the leftmost cattle, when it faces forward without reversing, when it is facing after it reversed [1, K] time interval. Then we can continue to consider the second head of cattle.

Inverted when each cow are not necessarily operated once, simply to record the number of turns with a current reversal interval, the next state of the cattle would be determined by considering its own condition and a value of the turns. With the movement section to the right, where we change the interval to update turns to the left and right. ComplexityO(n ^ 2)

source code

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

#include <cstring>
const int maxn = 5500;
const int INF = 100243535;
int a[maxn], f[maxn], cnt[maxn];
int () {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
char s[10];
scanf("%s", s);
a[i] = (s[0] == 'F' ? 0 : 1);
}
for (int k = 1; k <= n; k++) {
memset(f, 0, sizeof(f));
int turns = 0;
for (int i = 0; i < n; i++) {

if ((a[i] + turns) % 2 == 1) {
if (i + k > n) {
cnt[k] = INF;
break;
}
f[i] = 1;
cnt[k]++;
turns++;
}
if (i - k + 1 >= 0) turns -= f[i - k + 1];

}
}
int ansK = 1;

for ( int k = 2 ; k <= n; k ++) if (cnt [k] <cnt [ansk]) ansk = k; printf ( "% d% dn" , ansk, cnt [ansk]);



}

Original: Big Box  POJ 3276 [Face The Right Way] problem solution


Guess you like

Origin www.cnblogs.com/chinatrump/p/11615198.html