题应该人人都会做,主要用这题练习一下对拍
因为我的暴力跑得太慢了 就把数据范围改小了一点,用于检测正确性足够了(大概)
BZOJ1609[dp].cpp
#include
#include
#include
using namespace std;
const int maxn = 30007;
#define Max(_A, _B) (_A > _B ? _A : _B)
#define Min(_A, _B) (_A < _B ? _A : _B)
int read(){
int s = 0, f = 1; char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1; c = getchar();
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
}
return s * f;
}
int f[maxn][3], g[maxn][3], n, a[maxn], ans;
int main(){
ans = 0x3f3f3f3f;
n = read();
if (n == 1){
puts("0"); return 0;
}
for (int i = 1; i <= n; i++) {
a[i] = read() - 1;
}
memset(f, 0x3f, sizeof(f));
memset(g, 0x3f, sizeof(g));
for (int i = 0; i < 3; i++) f[1][i] = g[1][i] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 3; j++) {
for (int k = j; k < 3; k++) {
f[i + 1][k] = Min(f[i + 1][k], f[i][j] + (k != a[i]));
}
for (int k = j; k >= 0; k--) {
g[i + 1][k] = Min(g[i + 1][k], g[i][j] + (k != a[i]));
}
}
}
for (int i = 0; i < 3; i++) {
ans = Min(ans, Min(f[n + 1][i], g[n + 1][i]));
}
printf("%d\n", ans);
return 0;
}
BZOJ1609dfs.cpp
#include
using namespace std;
const int maxn = 30007;
#define Min(_A, _B) (_A < _B ? _A : _B)
int a[maxn], b[maxn];
int ans, n;
void dfs(int *a, int cur, int res, int pre){
if (cur == n + 1) {
ans = Min(ans, res);
return;
}
for (int i = pre; i <= 3; i++) dfs(a, cur + 1, res + (a[cur] != i), i);
}
int main(){
cin >> n;
for (int i = 1; i <= n; i++) {cin >> a[i]; b[n - i + 1] = a[i]; }
ans = 0x3f3f3f3f;
for (int i = 1; i <= 3; i++) dfs(a, 1, 0, i), dfs(b, 1, 0, i);
printf("%d\n", ans);
}
BZOJ1609rand.cpp
#include
#include
#include
using namespace std;
int n;
int random(int n){
return (long long)rand() * rand() % n;
}
int main(){
srand((unsigned)time(0));
n = random(2999) + 1;
printf("%d\n", n);
for (int i = 1; i <= n; i++) {
printf("%d\n", random(3) + 1);
}
return 0;
}
BZOJ1609对拍.cpp
#include
#include
#include
using namespace std;
double st, ed;
int main(){
int T;
for (T = 1; T <= 100; T++){
system("BZOJ1609rand > BZOJ1609in.txt");
st = clock();
system("BZOJ1609[dp] < BZOJ1609in.txt > BZOJ1609out.txt");
ed = clock();
system("BZOJ1609dfs < BZOJ1609in.txt > BZOJ1609out2.txt");
if (system("fc BZOJ1609out.txt BZOJ1609out2.txt > nul")) break;
else printf("AC #%d , t = %.0lfms\n", T, ed - st);
} printf("WA #%d\n", T);
return 0;
}