poj3671Dining Cows(DP)
主题链接:
题意:
给一个仅仅含有1。2的序列,如何变换n次使序列成为一个非递减的序列,而且使n最小。
思路:
这道题的数据范围是50000,则肯定承受不了n方的复杂度。所以 仅仅能写O(n)的算法,甚至更小,所以当时想二分,可是不知道怎么写,忽然想到能够枚举每个位置,把每个位置都当做一个分界点。然后求前半部有多少个2。后半段有多少个1,最后和所有是1和2进行比較,这个问题便得到了解决。
题目:
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
field=source&key=USACO+2008+February+Bronze" style="text-decoration:none">USACO 2008 February Bronze
代码
#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;
}
#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)的更多相关文章
- 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 ) ) 边 ...
- 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 ...
- POJ3186 Treats for the Cows —— DP
题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K To ...
- poj 3671 Dining Cows (Dp)
/* 一开始并没有想出On的正解 后来发现题解的思路也是十分的巧妙的 还是没能把握住题目的 只有1 2这两个数的条件 dp还带练练啊 ... */ #include<iostream> # ...
- POJ 3671 Dining Cows (DP,LIS, 暴力)
题意:给定 n 个数,让你修改最少的数,使得这是一个不下降序列. 析:和3670一思路,就是一个LIS,也可以直接暴力,因为只有两个数,所以可以枚举在哪分界,左边是1,右边是2,更新答案. 代码如下: ...
- BZOJ USACO 银组 水题集锦
最近刷银组刷得好欢快,好像都是水题,在这里吧他们都记录一下吧(都是水题大家一定是道道都虐的把= =)几道比较神奇的题到时再列出来单独讲一下吧= =(其实我会说是BZOJ蹦了无聊再来写的么 = =) [ ...
- HDU 3045 Picnic Cows(斜率优化DP)
Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ3186:Treats for the Cows(区间DP)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- 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 ...
随机推荐
- 异常Exception in thread "AWT-EventQueue-XX" java.lang.StackOverflowError
今天太背了,bug不断,检查到最后都会发现自己脑残了,粗心写错,更悲剧的是写错的时候还不提示错. 刚才有遇到一个问题,抛了这个异常Exception in thread "AWT-Event ...
- 【Android先进】查看手机记忆库状态和应用方法
一世 我们知道.android程序存储器通常被限制16M.当然,24M的,和android程序存储器分为2部分:native和dalvik.dalvik 就是我们寻常说的java堆.我们创建的对象是在 ...
- Silverlight之 xaml布局
目标:在两周内完成一个界面的功能 第1阶段:完成xaml的布局 准备:视频4-14节 第2阶段: 完成环状图 柱状图 TreeView样式 准备: 矢量绘图 telerik 自定义控件 自定义控件 ...
- twrp 2.7.0 ui.xml简单分析,布局讲解,第一章
twrp 的ui.xml文件在bootable/recovery/gui/devices/$(DEVICE_RESOLUTION)/res目录里面 下面我主要分析的是720x1280分辨率的界面布局及 ...
- 文章3说话 微信商城云server创建后台
一个. 应用server资源 想要进行微信开发.少不了后台server端程序的开发,那么我们首先就要申请server资源.眼下有非常多云server可选,比方新浪的sae ...
- 截图工具 Snagit
相对于其他截图工具方面,Snagit 一个主要特点是: 滚动截图. 另:同样基于手工绘制的形状截图, 有可能截取文本(测试只 windows在窗口内的目录 要么 文件名 实用). 不管是 web页,是 ...
- Java中Integer类的方法
java.lang 类 Integer java.lang.Object java.lang.Number java.lang.Integer 全部已实现的接口: Serializable, Comp ...
- 百度云BAE3.0 的ssh构造(本机ssh项目迁移到BAE3.0)
依据百度云的java部署文档进行部署 http://developer.baidu.com/wiki/index.php?title=docs/cplat/bae/java 做例如以下改动,然后把项目 ...
- mysql数据文件迁移到新的硬盘分区的方法
该系统增加了一个硬盘.要创建新的分区/data文件夹,mysql对于数据文件夹/var/lib/mysql 1. 停止mysql维修 [root@localhost~]# service mysql ...
- Bean Validation 技术规范特性概述
概述 Bean Validation 规范 Bean 是 Java Bean 的缩写.在 Java 分层架构的实际应用中,从表示层到持久化层.每一层都须要对 Java Bean 进行业务符合性验证,如 ...