Colored stones
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1854 Accepted: 870
Description
You are given a row of m stones each of which has one of k different colors. What is the minimum number of stones you must remove so that no two stones of one color are separated by a stone of a different color?
Input
The input test file will contain multiple test cases. Each input test case begins with a single line containing the integers m and k where 1 ≤ m ≤ 100 and 1 ≤ k ≤ 5. The next line contains m integers x1, …, xm each of which takes on values from the set {1, …, k}, representing the k different stone colors. The end-of-file is marked by a test case with m = k = 0 and should not be processed.
Output
For each input case, the program should the minimum number of stones removed to satisfy the condition given in the problem.
Sample Input
10 3
2 1 2 2 1 1 3 1 3 3
0 0
Sample Output
2
Hint
In the above example, an optimal solution is achieved by removing the 2nd stone and the 7th stone, leaving three “2” stones, three “1” stones, and two “3” stones. Other solutions may be possible.
题目链接
http://poj.org/problem?id=2978
题意
求去掉一些石子,来使满足条件(相同颜色的石子挨在一起)的序列长度最大
分析
因为1 <= k <= 5,因为颜色相同的石子要挨在一起,因此可考虑将石子的颜色组合作为状态进行压缩,最大2^5;
状态表示:d[i][j][st] 代表第i个石子,最后一个石子颜色为j,目前状态为st的最长序列长度
感觉思路有一点像0-1背包
代码
#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)
#define pr(x) cout << #x << " = " << x << endl
#define lc o << 1
#define rc o << 1 | 1
#define pl() cout << endl
const int maxn = 105;
int d[maxn][6][1 << 6], a[maxn];
int N, K;
void solve() {
memset(d, 0, sizeof(d));
d[0][a[0]][1 << a[0]] = 1;
int maxs = 1 << K;
for (int i = 1; i < N; i++) {
int x = a[i];
for (int s = 0; s < maxs; s++) {
//如果x还没有在颜色组合中出现过
if (!(s & (1 << x))) {
for (int k = 0; k < K; k++) {
if (!(s & (1 << k))) continue;
//选x
d[i][x][s ^ (1 << x)] = max(d[i][x][s ^ (1 << x)], d[i - 1][k][s] + 1);
//不选x
d[i][k][s] = max(d[i][k][s], d[i - 1][k][s]);
}
} else {
//x在颜色组合中已经出现过了,所以只有和前一个颜色相同序列长度才能增加
for (int k = 0; k < K; k++) {
if (!(s & (1 << k))) continue;
//选x
if (k == x) d[i][k][s] = max(d[i][k][s], d[i - 1][k][s] + 1);
//不选x
else d[i][k][s] = max(d[i][k][s], d[i - 1][k][s]);
}
}
}
}
int res = 0;
for (int s = 0; s < maxs; s++) {
for (int k = 0; k < K; k++) {
res = max(res, d[N - 1][k][s]);
}
}
cout << N - res << endl;
}
int main() {
//IN;
while (scan2(N, K) && (N + K)) {
for (int i = 0; i < N; i++) {
scan(a[i]);
--a[i];
}
solve();
}
return 0;
}