Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8126   Accepted: 3441

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

7
2
1
1
1
2
2
1

Sample Output

2

Source

 
问最少变换几次可以使原数列变成只有一串连续的1和一串连续的2的形式。
 
DP,枚举断点i,将i位置之前全变成1,之后全变成2,记录最优解即可。
 
 /**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
int a[mxn],b[mxn];
int n;
int main(){
scanf("%d",&n);
int i,j;
int d;
for(i=;i<=n;i++){
scanf("%d",&d);
if(d==){
a[i]=a[i-]+;//前缀和
b[i]=b[i-];
}
else{
a[i]=a[i-];
b[i]=b[i-]+;
}
}
int ans=mxn;
for(i=;i<=n;i++){
ans=min(ans,a[n]-a[i]+b[i]);//枚举断点,将i及之前所有的2替换成1,将i后面所有的1替换成2
}
ans=min(ans,min(a[n],b[n]));//全部替换成1或2
printf("%d\n",ans);
return ;
}

POJ3671 Dining Cows的更多相关文章

  1. POJ 3671 Dining Cows (DP,LIS, 暴力)

    题意:给定 n 个数,让你修改最少的数,使得这是一个不下降序列. 析:和3670一思路,就是一个LIS,也可以直接暴力,因为只有两个数,所以可以枚举在哪分界,左边是1,右边是2,更新答案. 代码如下: ...

  2. poj 3671 Dining Cows (Dp)

    /* 一开始并没有想出On的正解 后来发现题解的思路也是十分的巧妙的 还是没能把握住题目的 只有1 2这两个数的条件 dp还带练练啊 ... */ #include<iostream> # ...

  3. F-Dining Cows(POJ 3671)

    Dining Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7584   Accepted: 3201 Descr ...

  4. poj3671Dining Cows(DP)

    主题链接: 啊哈哈,点我点我 题意: 给一个仅仅含有1.2的序列,如何变换n次使序列成为一个非递减的序列,而且使n最小. 思路: 这道题的数据范围是50000,则肯定承受不了n方的复杂度.所以 仅仅能 ...

  5. OJ题解记录计划

    容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001  A+B Problem First AC: 2 ...

  6. poj3281 Dining

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14316   Accepted: 6491 Descripti ...

  7. POJ 3281 Dining

    Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and dri ...

  8. POJ 3281 Dining 网络流最大流

    B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...

  9. poj 3281 Dining【拆点网络流】

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11828   Accepted: 5437 Descripti ...

随机推荐

  1. django logging日志优先级

    原创博文 转载请注明出处! 参考官方文档:https://docs.djangoproject.com/en/2.1/topics/logging/ Loggers¶ A logger is the ...

  2. 概括的描述一下Spring注册流程

    Spring经过大神们的构思.编码,日积月累而来,所以,对其代码的理解也不是一朝一夕就能快速完成的.源码学习是枯燥的,需要坚持!坚持!坚持!当然也需要技巧,第一遍学习的时候,不用关注全部细节,不重要的 ...

  3. intellij idea 下载安装破解教程

    官网下载:http://www.jetbrains.com/idea/download/#section=windows 选择  Ultimate 版本下载 下载完成后,打开安装 在安装路径位置,可以 ...

  4. Ubuntu下kaldi安装

    该文章为博主原创,如若转载请注明出处:https://www.cnblogs.com/fengleixue/p/9482202.html 因公司业务需要需使用kaldi语音识别工具,现将kaldi环境 ...

  5. python 监控日志

    #需求: #1.每分钟监控服务器日志,ip请求超过200次的,加入黑名单 #1.读文件,获取到每行的内容 open readlines # 178.210.90.90 - - [04/Jun/2017 ...

  6. mysql数据库使用mybatis 插入数据时返回主键

    为了体现题目,特指的是mysql,先贴上代码: <insert id="saveBizProdOrderDetail" useGeneratedKeys="true ...

  7. paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之onehot coding styles(encoded-parameter style with registered outputs不推荐但是经常有人写这样的代码)

    这样写法,不利与综合,case语句中比较也是full-vector比较.

  8. Divisibility by 25 CodeForces - 988E

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  9. Solution: 最近公共祖先·一 [hiho一下 第十三周]

    题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...

  10. [转] 查看 SELinux状态及关闭SELinux

    本文转载自: http://bguncle.blog.51cto.com/3184079/957315 查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SE ...