POJ3671 Dining Cows

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

USACO 2008 February Bronze

问最少变换几次可以使原数列变成只有一串连续的1和一串连续的2的形式。

DP,枚举断点i,将i位置之前全变成1,之后全变成2,记录最优解即可。

 1 /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 const int mxn=50000;
 9 int a[mxn],b[mxn];
10 int n;
11 int main(){
12     scanf("%d",&n);
13     int i,j;
14     int d;
15     for(i=1;i<=n;i++){
16         scanf("%d",&d);
17         if(d==1){
18             a[i]=a[i-1]+1;//前缀和
19             b[i]=b[i-1];
20         }
21         else{
22             a[i]=a[i-1];
23             b[i]=b[i-1]+1;
24         }
25     }
26     int ans=mxn;
27     for(i=1;i<=n;i++){
28         ans=min(ans,a[n]-a[i]+b[i]);//枚举断点,将i及之前所有的2替换成1,将i后面所有的1替换成2
29     }
30     ans=min(ans,min(a[n],b[n]));//全部替换成1或2
31     printf("%d\n",ans);
32     return 0;
33 }
时间: 2024-11-13 19:23:18

POJ3671 Dining Cows的相关文章

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

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

POJ 3671 Dining Cows (DP)

题目大意:给你一串只有1,2的数字,让你改变最少的次数,让这个序列变成非递减的. 思路:动态规划,判断分界点,开一个dp[30010][2]的数组,其中dp[i][j]表示把第i个数改成j最少要花多少次 那么状态转移方程就列出来了: 令a=1 j!=a[i] 0 j==a[i] 那么dp[i][1]=dp[i-1][1]+a; dp[i][2]=min(dp[i-1][1],dp[i-1][2])+a; #include <iostream> using namespace std; int

F-Dining Cows(POJ 3671)

Dining Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7584   Accepted: 3201 Description The cows are so very silly about their dinner partners. They have organized themselves into two groups (conveniently numbered 1 and 2) that ins

poj3671Dining Cows(DP)

题目链接: 啊哈哈,点我点我 题意: 给一个只含有1,2的序列,怎样变换n次使序列成为一个非递减的序列,并且使n最小. 思路: 这道题的数据范围是50000,则肯定承受不了n方的复杂度,所以 只能写O(n)的算法,甚至更小,所以当时想二分,但是不知道怎么写,忽然想到可以枚举每个位置,把每一个位置都当做一个分界点,然后求前半部有多少个2,后半段有多少个1,最后和全部是1和2进行比较,这个问题便得到了解决. 题目: Dining Cows Time Limit: 1000MS   Memory Li

POJ 3281 Dining (最大流)

Dining Time Limit: 2000MS   Memory Limit: 65536K       Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot

poj 3281 Dining(最大流)

poj 3281 Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their prefer

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

POJ 3281 Dining 最大流 Dinic算法

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10768   Accepted: 4938 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulo

POJ 3281 Dining

ISAP最大流...果粉专用的最大流 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9573   Accepted: 4417 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John