Eating Together
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5579   Accepted: 2713

Description

The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. 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 ≤ 3) 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 111222333 or 333222111 where the cows' dining groups are sorted in either ascending or descending order by their dinner cards.

FJ is just as lazy as the next fellow. He's curious: what is the absolute mminimum 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 describes the i-th cow's current dining group with a single integer: Di

Output

* Line 1: A single integer representing the minimum number of changes that must be made so that the final sequence of cows is sorted in either ascending or descending order

Sample Input

5
1
3
2
1
1

Sample Output

1

Source

跟F题差不多,就是变成了三个数,变成了升序或降序。。。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 30005 int d[MAXN][] = {};
int cow[MAXN];
int n;
int get_min()
{
d[][] = d[][] = d[][] = ;
d[][cow[]] = ;
repu(i, , n + )
if(cow[i] == ) {
d[i][] = d[i - ][];
d[i][] = min(d[i - ][], d[i - ][]) + ;
d[i][] = min(d[i - ][], min(d[i - ][], d[i - ][])) + ;
}
else if(cow[i] == ) {
d[i][] = d[i - ][] + ;
d[i][] = min(d[i - ][], d[i - ][]);
d[i][] = min(d[i - ][], min(d[i - ][], d[i - ][])) + ;
}
else {
d[i][] = d[i - ][] + ;
d[i][] = min(d[i - ][], d[i - ][]) + ;
d[i][] = min(d[i - ][], min(d[i - ][], d[i - ][]));
}
return min(d[n][], min(d[n][], d[n][]));
}
int main()
{
int minn;
scanf("%d", &n);
repu(i, , n + ) scanf("%d", &cow[i]);
minn = get_min();
repu(i, , n / + ) swap(cow[i], cow[n - i + ]);
minn = min(minn, get_min());
printf("%d\n", minn);
return ;
}

E-Eating Together(POJ 3670)的更多相关文章

  1. POJ 3670 Eating Together (DP,LIS)

    题意:给定 n 个数,让你修改最少的数,使得它变成一个不下降或者不上升序列. 析:这个就是一个LIS,但是当时并没有看出来...只要求出最长LIS的长度,用总数减去就是答案. 代码如下: #inclu ...

  2. POJ 3670 Eating Together 二分解法O(nlgn)和O(n)算法

    本题就是一题LIS(最长递增子序列)的问题.本题要求求最长递增子序列和最长递减子序列. dp的解法是O(n*n),这个应该大家都知道.只是本题应该超时了. 由于有O(nlgn)的解法. 可是因为本题的 ...

  3. POJ 3670 Eating Together(LIS)

    Description The cows are so very silly about their dinner partners. They have organized themselves i ...

  4. POJ 3670 , 3671 LIS

    题意:两题意思差不多,都是给你一个序列,然后求最少需要改变多少个数字,使得成为一个最长不升,或者最长不降子序列. 当然3671是只能升序,所以更简单一点. 然后就没有什么了,用二分的方法求LIS即可. ...

  5. poj 3670(LIS)

    // File Name: 3670.cpp // Author: Missa_Chen // Created Time: 2013年07月08日 星期一 21时15分34秒 #include < ...

  6. POJ 3670 DP LIS?

    权值为1~3 好了 此题是水题-- i表示到了第i个数,j表示结尾的数是j f[i][j]=min(f[i][j],f[i-1][k]+(a[i]!=j)) 1<=k<=j 最长上升的. ...

  7. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  8. poj 3254 Corn Fields

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  9. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

随机推荐

  1. Maven开发基础总结(Maven自启动,Maven打war包,Maven热部署)

    学习内容: 1.不依赖外部Tomcat,自己启动方式部署 2.Maven打war包,远程部署到centOS 3.Maven热部署(不关闭Tomcat部署应用)   做maven开发前提: 1.编码UT ...

  2. apue和error

    在做进程环境测试的时候,测试demo中出现了apue.h,而标准库中没有这个头文件和其中的函数定义,经查找需要在/usr/include中添加apue.h和error.c.原型可以去这个网站查找.   ...

  3. C library function - tmpfile()

    Description The C library function FILE *tmpfile(void) creates a temporary file in binary update mod ...

  4. 加速chrome之Vimium快捷键

    使用Vimium一段时间,不能完全学习所有的快捷键.但是对这种简约,vim风格的设计还是非常敬佩. 下面是一些总结: Vimium快捷键 WIKI: 是一个开源的google chrome的扩展插件, ...

  5. 将图片转成base64 小工具

    工作需要使用,所以就做了一个小工具,方便使用 推荐使用 chrome,ff . 毕竟是个小工具方便自己使用而已,所以没有做浏览器兼容测试了! 代码如下,直接保存为 .html 打开即可 <!DO ...

  6. 专题实验 PGA

    PGA : 是完全为 server process 服务的, 在 server process 创建时被分配到, 在server process 终止时被释放. 而且是非共享的, 只独立服务于这个se ...

  7. 几种HtmlEncode的区别(转)

    一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...

  8. hdu 4414 Finding crosses

    题目链接:hdu 4414 其实是一道简单的字符型水题,不涉及任何算法,可比赛时却没能做出来,这几天的状态都差到家了... 题目大意是求有多少个满足条件的十字架,十字架的边不能有分叉路口,所以枚举每个 ...

  9. ID和Name的区别

    HTML元素的ID和Name属性的区别一直认为ID和NAME是一样的,两个又可以一起出现,甚是疑惑.今天BAIDU了一下,才发现里面大有文章.发出来研究研究:最classical的答案:ID就像是一个 ...

  10. Spring Boot 以 jar 包方式运行在后台

    spring-boot jar 包方式启动: 首先,为了防止和常用的 Tomcat 8080 端口冲突,将 Spring-boot 项目的端口号设置为 9090. 具体方法:在 application ...