Colored stones

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 1759 Accepted: 829

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.

Source

这道题目,应该先去求给定的序列最长的子序列并满足,相同元素都是相邻的这个条件。于是我很容易想到了和LIS问题联系起来,就是开始去想。怎么也想不出来,看了题解,是要把状态表示成这样DP[i][j][k] 到序列第i个数的最长满足要求的子序列的状态j和子序列的最后一个元素是k。状态其实二进制压缩,表示k个元素是否在子序列中都出现过。我本来以为是简单的一维DP,到头来居然是三维的,有了这个状态,状态转移方程也好写了。我想在看过题解之后应该去思考,为什么这个状态是正确的,为什么要用这个状态表示,

还有一点要注意,第二维的状态要是逆序的,因为代码中

ss=s+(1<<(a[i]-1)); 如果正序会将已经求出ss又覆盖掉。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h> using namespace std;
int n,k;
int a[105];
int dp[105][1<<5][6];
int ans=0;
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==0&&k==0)
break;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int s=(1<<k)-1;s>=0;s--)
{
for(int p=1;p<=k;p++)
{
dp[i][s][p]=dp[i-1][s][p];
}
if((1<<(a[i]-1))&s)
dp[i][s][a[i]]=dp[i-1][s][a[i]]+1;
else
{
int ss=s+(1<<(a[i]-1)); for(int p=1;p<=k;p++)
{ if(dp[i][ss][a[i]]<dp[i-1][s][p]+1)
dp[i][ss][a[i]]=dp[i-1][s][p]+1;
}
} }
} ans=0; for(int s=0;s<=(1<<k)-1;s++)
{
for(int p=1;p<=k;p++)
{
ans=max(ans,dp[n][s][p]);
} }
printf("%d\n",n-ans); }
return 0;
}

HOJ 2156 &POJ 2978 Colored stones(线性动规)的更多相关文章

  1. [poj 2978]Colored Stones[状态压缩DP]

    题意: 给出n个石子,一共m种颜色.问最少去掉几个石子使得同种颜色全连续. 思路见注释. #include <algorithm> #include <cstdio> #inc ...

  2. POJ-1958 Strange Towers of Hanoi(线性动规)

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...

  3. POJ-1953 World Cup Noise(线性动规)

    World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16374 Accepted: 8097 Desc ...

  4. POJ-1458 Common Subsequence(线性动规,最长公共子序列问题)

    Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44464 Accepted: 18186 ...

  5. POJ--1050--To the Max(线性动规,最大子矩阵和)

    To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44723 Accepted: 23679 Descript ...

  6. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  7. (动规 或 最短路)Help Jimmy(poj 1661)

    http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同的 ...

  8. 关于DP动规

    今天学了动规,简单记录一下自己理解了的:(要不俺就忘了) 首先,啥是DP??? 动态规划,其实就是组合子问题的解来解决整个问题的解,由于每个子问题他只判断一次,所以不会重复计算,那就很牛啊!!! 专业 ...

  9. vijos1431[noip2007]守望者的逃离(背包动规)

    描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者 在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这 个荒岛施咒,这座岛很快就会 ...

随机推荐

  1. [原]IOS 后台发送邮件

    skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git -(void)statrUpLoad:(id) ...

  2. iOS UITextField控件总结

    先声明下面总结不是自己写的. 参考网址:http://blog.csdn.net/tskyfree/article/details/8121915 //初始化textfield并设置位置及大小   U ...

  3. Linux+Redis实战教程_day03_Redis-set【重点】_有序set(了解)

    2.redis-set[重点] Java HashSet  无序,不重复. Redis操作中,涉及到两个大数据集合的并集,交集,差集运算. 赋值: l sadd key values[value1.v ...

  4. MongoDB中常用的find

    接着前一篇文章,下面主要介绍一下MongoDB中常用的find操作. 先打开MongoDB shell,通过下面一组命令插入一些数据. post1 = {} post2 = {} post3 = {} ...

  5. 一句话木马:JSP篇

    JSP一句话收集: <%if(request.getParameter("f")!=null)(new java.io.FileOutputStream(applicatio ...

  6. ASP代码审计学习笔记 -4.命令执行漏洞

    命令执行漏洞: 保存为cmd.asp,提交链接: http://localhost/cmd.asp?ip=127.0.0.1 即可执行命令 <%ip=request("ip" ...

  7. [C] scanf - 格式输入函数

    scanf 函数称为格式输入函数,即把用户键盘输入的内容保存到指定的变量中. scanf 函数签名 scanf("格式控制字符串", 变量地址列表) scanf 函数是一个标准库函 ...

  8. [Linux] 如何修改 Linux 主机名

    该方法适用于安装了 Linux 系统的Raspberry Pi & Cubieboard. 在终端执行: sudo vi /etc/hosts 你看到的 hosts 文件应该是这样的: 127 ...

  9. apk反编译看包名什么的

    首先默认你是装了java环境的 到google code里面下载apktool1.5.2.tar.bz2和apktool-install-windows-r05-ibot.tar.bz2(地址:htt ...

  10. PHP的ISAPI和FastCGI比较

     1.CGI(通用网关接口/Common Gateway Interface)一般是可执行程序,例如EXE文件,和WEB服务器各自占据着不同的进程,而且一般一个CGI程序只能处理一个用户请求.这样,当 ...