博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HOJ 2156 &POJ 2978 Colored stones(线性动规)
阅读量:4676 次
发布时间:2019-06-09

本文共 2246 字,大约阅读时间需要 7 分钟。

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 
#include
#include
#include
#include
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<
=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]]

转载于:https://www.cnblogs.com/dacc123/p/8228794.html

你可能感兴趣的文章
单元测试之初识
查看>>
golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法
查看>>
内存分配 保存数据
查看>>
磁盘分区、格式化
查看>>
嵌入式实时操作系统的可裁剪性及其实现
查看>>
VC++2012编程演练数据结构《31》狄杰斯特拉算法
查看>>
盘点:移动服务 #AzureChat
查看>>
基于visual Studio2013解决C语言竞赛题之0608水仙花函数
查看>>
Sass学习笔记
查看>>
C语言练习
查看>>
Eclipse:An internal error occurred during: "Building workspace". GC overhead limit exceeded
查看>>
纯Css实现Div高度根据自适应宽度(百分比)调整
查看>>
jboss eap6.1(4)(部署应用)
查看>>
配置jboss EAP 6.4 数据库连接超时时间
查看>>
【BZOJ5005】乒乓游戏 [线段树][并查集]
查看>>
前端页面数据埋点、分析和参考
查看>>
NBear简介与使用图解
查看>>
ng-app一些使用
查看>>
ubuntu16.04安装 java JDK8
查看>>
中兴F412光猫超级密码破解、破解用户限制、关闭远程控制、恢复路由器拨号
查看>>