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. Java的日期类和日期格式化类

    日期类: Date date = new Date(); // 获取当前的系统时间 2 System.out.println("年份:"+ date.getYear()); Cal ...

  2. Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法

    通过配置文件XML方法的配置 可以使用非常简练的Service类 UserService类代码如下: package com.swift; public class UserService { pri ...

  3. 我的offer之路(一)

    目录 1.职业规划. 2.刷题. 3.看书. <剑指offer> <数据结构算法与应用:C++语言描述 > <Effective C++> <C与指针> ...

  4. dev gridview columns代码管理

    进入run designer界面.我们将在代码中设置columns的属性. 类: ViewTriAtt : DevExpress.XtraEditors.XtraUserControl 在类里面设置g ...

  5. 【离线 线段树分治】bzoj4025: 二分图

    昨天mac的gdb挂了,今天怎么笔记本的gdb也挂了…… Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这 ...

  6. [BZOJ] 1907: 树的路径覆盖

    一个点必然被路径覆盖,根据是否为路径的端点分类 \(f[x][0]\)表示以\(x\)为根的子树,\(x\)不为端点的最小路径覆盖数 \(f[x][1]\)表示以\(x\)为根的子树,\(x\)为一条 ...

  7. ipmitool的使用

    https://www.ibm.com/developerworks/cn/linux/l-ipmi/index.html

  8. Vue 父子组件间的通信

    前言 在 Vue 项目中父子组件的通信是非常常见的,最近做项目的时候发现对这方面的知识还不怎么熟练,在这边做一下笔记,系统学习一下吧. 1 父组件传值给子组件 1.1 传值写法 父组件传值给子组件,这 ...

  9. Python学习笔记:xlrd和xlwt(Excel读写)

    xlrd模块 Python的三方库xlrd用于对excel文件进行读取,可以是“.xls”或“.xlsx”格式(旧版本可能不支持“.xlsx”). 下载安装:https://pypi.org/proj ...

  10. 探讨2018年最受欢迎的15顶级Python库!

    近日,数据科学网站 KDnuggets 评选出了顶级 Python 库 Top15,领域横跨数据科学.数据可视化.深度学习和机器学习.如果本文有哪些遗漏,你可以在评论区补充. 图 1:根据 GitHu ...