开一个dp[30010][3]的数组

其中dp[i][j]表示把第i个数改成j最少要花多少次

那么状态转移方程就列出来了:

令a=1 j!=a[i]

0 j==a[i]

那么dp[i][1]=dp[i-1][1]+a;

dp[i][2]=min(dp[i-1][1],dp[i-1][2])+a;

那么根据空间优化原理,我们的第一维的i不过是为了递推,那么可以优化掉。

不过要注意的是,dp【2】的状态要先求,否则测试数据会是3,因为沿用了本次的dp【1】,而非上次的dp【1】。

#include <iostream>
#include <cstdio>
#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn=;
//int dp[maxn][3];
int dp[maxn];
int cow[maxn];
int main()
{
// freopen("in.txt","r",stdin);
int n;
cin >> n;
for(int i=;i<=n;i++)
{
cin >> cow[i];
int flag;
if(cow[i]==)
flag=;
else flag=;
//dp[i][2]=min(dp[i-1][1],dp[i-1][2])+flag;
dp[]=min(dp[],dp[])+flag;
if(flag==)flag=;
else flag=;
// dp[i][1]=dp[i-1][1]+flag;
dp[]=dp[]+flag;
}
//cout << min(dp[n][1],dp[n][2])<< endl;
cout << min(dp[],dp[])<< endl;
return ;
}
左右遍历,计算left【i】+right【i】的max:
#include <iostream>
#include <cstdio>
#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
#define maxn 30002
int s[maxn],l[maxn],r[maxn];
int main()
{
//freopen("in.txt","r",stdin);
memset(s,,sizeof(s));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
int n;
cin >> n;
for(int i=;i<=n;++i)
{
cin >> s[i];
}
if(s[]==)
{
l[]=;
}
for(int i=;i<=n;i++)
{
if(s[i]==)
{
l[i]=l[i-]+;
}
else l[i]=l[i-];
}
if(s[n]==)
{
r[n]=;
}
for(int i=n-;i>=;i--)
{
if(s[i]==)
{
r[i]=r[i+]+;
}
else r[i]=r[i+];
}
int max=;
for(int i=;i<=n;i++)
{
if(l[i]+r[i]>max)
{
max=l[i]+r[i];
}
}
cout << n-max << endl;
return ;
}
还有一个不错的方法,我们先计算1的个数,然后从前遍历,每次判断:是2就自加1,不是就自减1,其中保存min,则为结果。实际上也为dp,每次的i值代表,编号为i的数组前面不可更改的数字1个数。 
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX=+;
int a[MAX]; //存储输入的串
int main()
{
//freopen("in.txt","r",stdin);
int cnt; //数组元素的个数
int i; int min; //最少要改变的次数
scanf("%d",&cnt);
int num1=;
for (i=;i<cnt;i++)
{
scanf("%d",&a[i]);
if (a[i] == )
num1++;
} min=num1;
for (i=;i<cnt;i++)
{
(a[i] == ) ? num1-- : num1++;
if (min > num1) min = num1;
}
printf("%d\n",min);
return ;
}
												

【经典dp】 poj 3671的更多相关文章

  1. 【dp】 比较经典的dp poj 1160

    转自http://blog.sina.com.cn/s/blog_5dd8fece0100rq7d.html [题目大意]:用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上,需要选择 ...

  2. poj 1050 To the Max 最大子矩阵和 经典dp

    To the Max   Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  3. POJ 1160:Post Office 邮局经典DP

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17168   Accepted: 9270 Desc ...

  4. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  5. poj1458 求最长公共子序列 经典DP

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45763   Accepted: 18 ...

  6. NYOJ - 矩形嵌套(经典dp)

    矩形嵌套时间限制:3000 ms | 内存限制:65535 KB 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b< ...

  7. 51nod 1412 AVL树的种类(经典dp)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 题意: 思路: 经典dp!!!可惜我想不到!! $dp[i][k] ...

  8. NYOJ 16 矩形嵌套(经典DP)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=16 矩形嵌套 时间限制:3000 ms  |           内存限制:65535 KB 难度: ...

  9. CS Academy Distinct Neighbours(经典dp)

    CS Academy Distinct Neighbours(经典dp) 题意: 求相邻无相同数字的合法的排列数 题解: 题解 先将相同的数字分为一类,假设共有n组 定义\(dp[i][j]\)表示前 ...

  10. 【经典dp 技巧】8.13序列

    经典的拆绝对值 题目大意 给定$n$个具有顺序的序列,允许对每个序列循环移动.记第$i$个序列尾元素为$x$,$i+1$个序列首元素为$y$,定义其连接收益为$|x-y|*i$,求$n$个序列连接最大 ...

随机推荐

  1. javascript中的事件处理

    事件处理:http://www.cnblogs.com/polk6/archive/2016/02/19/5154470.html#Menu2-DOMEventSpecification addEve ...

  2. ZOJ 2476 Total Amount

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476 Time Limit: 2 Seconds         ...

  3. thinkphp 的两种建构模式 第一种一个单入口里面定义两个模块,前台和后台,函数控制模块必须function.php前台加载前台模块的汉书配置文件,后台加载后台模块的汉书配置文件,公共文件共用。第二种架构模式两个单入口文件,分别生成两个应用定义define。。。函数可以定义配置文件。。。。

    thinkphp 的两种建构模式  第一种一个单入口里面定义两个模块,前台和后台,函数控制模块必须function.php前台加载前台模块的汉书配置文件,后台加载后台模块的汉书配置文件,公共文件共用. ...

  4. jQuery进行简单验证的正则表达式

    下面都是一些比较常用简单的验证,像那些特殊的复杂的情况这里不进行考虑 1.验证电话号码或者手机号码 ? 1 2 3 4 5 6 7 8 9 10 /**  * 验证电话号码(手机号码+电话号码)  * ...

  5. 【Python】Python&&MySQL

    按照廖雪峰的官方网站http://www.liaoxuefeng.com/中给的步骤做的,但还是出现了一些问题: 1.安装MySQL时候,提示我没有安装Python3.4,我电脑安装的3.3,所以直接 ...

  6. linux centOS 安装oracle

    安装环境 Linux服务器:CentOS6.4-64位 oracle服务器:oracle11g-64位 基本要求 内存大小:至少2G 硬盘大小:至少6G 交换空间:一般为内存的2倍,例如:2G的内存可 ...

  7. Android中GridView、ListView 的 getChildAt() 方法返回null 问题

    开发的Android app用到了GridView或者ListView,通常使用getChildAt(int position)方法获取当前点击或者选中的View(即position对应的View). ...

  8. MySQL(3)-索引

    一.索引类型 在MySQL中,存储引擎使用索引,首先在索引中找到对应值,然后根据匹配的索引记录中找到对应的行. 无论是多么复杂的ORM工具,在精妙和复杂的索引面前都是"浮云".这里 ...

  9. LeetCode OJ 84. Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  10. 转载–移动互联网终端的touch事件,touchstart, touchend, touchmove

    转载请注明: 转载自WEB前端开发(www.css119.com)-关注常见的WEB前端开发问题.最新的WEB前端开发技术(webApp开发.移动网站开发).最好的WEB前端开发工具和最全的WEB前端 ...