主题链接:

题意:

给一个仅仅含有1。2的序列,如何变换n次使序列成为一个非递减的序列,而且使n最小。

思路:

这道题的数据范围是50000,则肯定承受不了n方的复杂度。所以 仅仅能写O(n)的算法,甚至更小,所以当时想二分,可是不知道怎么写,忽然想到能够枚举每个位置,把每个位置都当做一个分界点。然后求前半部有多少个2。后半段有多少个1,最后和所有是1和2进行比較,这个问题便得到了解决。

题目:

Dining Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7237   Accepted: 3078

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



代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=30000+10; int sum1[maxn],sum2[maxn]; int main()
{
int n,cal1,cal2,tmp,ans;
while(~scanf("%d",&n))
{
cal1=cal2=0;
ans=INF;
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
for(int i=1;i<=n;i++)
{
scanf("%d",&tmp);
if(tmp==1)
sum1[i]=++cal1;
else
sum1[i]=cal1;
if(tmp==2)
sum2[i]=++cal2;
else
sum2[i]=cal2;
}
for(int i=1;i<n;i++)
ans=min(ans,sum2[i]+(sum1[n]-sum1[i]));
ans=min(ans,sum1[n]);
ans=min(ans,sum2[n]);
printf("%d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj3671Dining Cows(DP)的更多相关文章

  1. BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

    dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边 ...

  2. poj 3186 Treats for the Cows(dp)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  3. POJ3186 Treats for the Cows —— DP

    题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K To ...

  4. poj 3671 Dining Cows (Dp)

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

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

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

  6. BZOJ USACO 银组 水题集锦

    最近刷银组刷得好欢快,好像都是水题,在这里吧他们都记录一下吧(都是水题大家一定是道道都虐的把= =)几道比较神奇的题到时再列出来单独讲一下吧= =(其实我会说是BZOJ蹦了无聊再来写的么 = =) [ ...

  7. HDU 3045 Picnic Cows(斜率优化DP)

    Picnic Cows Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. POJ3186:Treats for the Cows(区间DP)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  9. poj 3186 Treats for the Cows(区间dp)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

随机推荐

  1. Oracle SQL Lesson (4) - 使用转换函数和条件表达式

    隐式转换select * from emp where empno='7788'字符(char,varchar2)转换为数字(number)或日期(date)数字或日期转换为字符 显式转换字符转换为数 ...

  2. Wind River Linux 6 Security Profile

    2692407267@qq.com,很多其它内容请关注http://user.qzone.qq.com/2692407267 Wind River Linux 6 Security Profile

  3. 编程算法 - 不用加减乘除做加法 代码(C)

    不用加减乘除做加法 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 写一个函数, 求两个整数之和, 要求在函数体内不得使用+, -, *, /四 ...

  4. Java 执行四则运算

    四种基本的操作原理是将被转换成后缀缀表达式表达.然后计算. 转换思路和原则.可以参考将中缀表达式转化为后缀表达式 import java.math.BigDecimal; import java.ut ...

  5. Duanxx的C++学习: const指针具体解释

    Const指的是一个编译时的常量. keywordconst使得代码能够确定一个变量能否够被改动. 使用了const后,能够防止对变量或者指针的改动:更重要的是,const的引用能够防止对所引用的对象 ...

  6. HDU 1856 More is better(并查集+离散化)

    题目地址:HDU 1856 水题.因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化.然后就是裸的并查集了. 代码例如以下: #include <iostream> #includ ...

  7. 1、Cocos2dx 3.0游戏开发三找一小块前言

    尊重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 前言 Cocos2d-x 是一个通用 ...

  8. lua 远程调试 【zeroBrane 使用mobdebug】(good转)

    最近基于业务需求,学习了如何使用zeroBrane这个IDE实现C/S 模式下的 lua远程调试,废话不多,上效果图: ---------------------------------------- ...

  9. 怎么样putty打开图形化管理工具,在终端上

    有时需要在putty这种图形终端开放的图形化管理工具将出现以下错误: [root@node2 ~]# Traceback (most recent call last): File "/us ...

  10. windbg更改cmd的token提升其特权

    采用windbg 调试xp. 执行cmd.whoami检查权限如下面: 以下要做的就是把cmd.exe 的token值用system的token替换. 1.  Ctrl + break ,windbg ...