POJ 3671 Dining Cows 简单DP

来源:互联网 发布:apache ant 1.7.0 编辑:程序博客网 时间:2024/06/05 17:52

http://poj.org/problem?id=3671

简单动态规划

opt[i][0]第i个数后1的个数;opt[i][1] 第i个数前2的个数

状态方程

 

Dining Cows
Time Limit: 1000MS Memory Limit: 65536K


Description

The cows are so very silly about their dinner partners. They have organized themselves into two groups (conveniently numbered 1 and 2) that insist upon dining together in order, with group 1 at the beginning of the line and group 2 at the end. The trouble starts when they line up at the barn to enter the feeding area.

Each cow i carries with her a small card upon which is engraved Di (1 ≤ Di ≤ 2) indicating her dining group membership. The entire set of N (1 ≤ N ≤ 30,000) cows has lined up for dinner but it's easy for anyone to see that they are not grouped by their dinner-partner cards.

FJ's job is not so difficult. He just walks down the line of cows changing their dinner partner assignment by marking out the old number and writing in a new one. By doing so, he creates groups of cows like 112222 or 111122 where the cows' dining groups are sorted in ascending order by their dinner cards. Rarely he might change cards so that only one group of cows is left (e.g., 1111 or 222).

FJ is just as lazy as the next fellow. He's curious: what is the absolute minimum number of cards he must change to create a proper grouping of dining partners? He must only change card numbers and must not rearrange the cows standing in line.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 describes cow i's dining preference with a single integer: Di

Output

* Line 1: A single integer that is the minimum number of cards Farmer John must change to assign the cows to eating groups as described.

Sample Input

72111221

Sample Output

2
47ms代码
 
稍微修改优化后代码 16ms
 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 脸上长疙瘩很痒怎么办 一个多月宝宝脸上有湿疹怎么办 脸上发红发痒起疙瘩怎么办 脸过敏了怎么办最简单 胸下垂应该怎么办19岁 身上的肉特别松怎么办 才30岁脸部松弛怎么办 脸上的皮肤很松怎么办 面部皮肤干燥起皮刺痛怎么办 脸上的皮肤很粗糙怎么办 脸上又红又痒怎么办 鲜红斑痣增生了怎么办 激光祛斑后色素沉着怎么办 白球鞋洗后发黄怎么办 夏天出汗妆花了怎么办 买的小产权房怎么办 嘴唇起皮怎么办小妙招 照相嘴巴是歪的怎么办 鼻子笑起来很宽怎么办 财运不好怎么办最近你有偏财 从小缺爱的人怎么办 一到晚上就怕死怎么办 碰到不讲理的人怎么办 遇到不讲理的人怎么办 蚰蜓虫子咬了怎么办 腰肌损伤怎么办恢复快 腰闪了怎么办最有效 墨兰严重烂根怎么办 铁兰花变绿了怎么办 1岁半宝宝吵瞌睡怎么办 28天宝宝吵瞌睡怎么办 2个月宝宝闹瞌睡怎么办 被刺猬的刺扎了怎么办 买电脑被坑了怎么办 买电脑被坑了怎么办啊 在电脑城被坑了怎么办 小狗20天不睁眼怎么办 金星秀停播沈南怎么办 干了活拿不到钱怎么办 干完活要不到钱怎么办 让蚊子咬了很痒怎么办