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. 屏幕序列Screen Sequences

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. [Effective Java]第十一章 序列化

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. C语言中strdup函数使用方法

    头文件:#include <string.h> 定义函数:char * strdup(const char *s); 函数说明:strdup()会先用malloc()配置与参数s 字符串相 ...

  4. Java——Image 图片切割

    package com.tb.image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io ...

  5. Linux基础※※※※访问Windows共享文件夹

    参考Linux公社链接:http://www.linuxidc.com/Linux/2014-06/103749.htm 实际上,可以直接用sambaclient程序访问共享资源. 列出共享主机的列表 ...

  6. Java Abstract class and Interface

    Abstract Class 在定义class的时候必须有abstract 关键字 抽象方法必须有abstract关键字. 可以有已经实现的方法. 可以定义static final 的常量. 可以实现 ...

  7. iOS添加Prefix Header

    1. 添加Prefix Header 注: Xcode 6苹果默认去掉prefix Header, 用以提高原文件的复用性, 便于迁移. 并且可以一定程度上减少Build Time. 解决办法: (1 ...

  8. java里有没有专门判断List里有重复的数据

    public static void main(String[] args)     {         List<String> list = new ArrayList<Stri ...

  9. Centos升级Python及pip

    因为CentOS系统中旧版本的Python已被深度依赖,所以不能卸载原有的Python,只能全新安装. 1.从官网下载: wget https://www.python.org/ftp/python/ ...

  10. UIButton的常见设置

    - (void)setTitle:(NSString *)title forState:(UIControlState)state;设置按钮的文字 - (void)setTitleColor:(UIC ...