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. Java编程思想读书笔记

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

  2. How to build a GUI in ROS with Qt / C++

    p { margin-bottom: 0.1in; direction: ltr; line-height: 120%; text-align: left; widows: 2; orphans: 2 ...

  3. T-SQL JOIN

    a表name sex张三 男李四 女 b表name age李四 30王五 23 1 全外连接select a.name,a.sex,b.name,b.agefrom a full outer join ...

  4. Linux vi 中移动光标 命令

    移动光标 上:k nk:向上移动n行 9999k或gg可以移到第一行 G移到最后一行下:j nj:向下移动n行左:h nh:向左移动n列右:l nl:向右移动n列 w:光标以单词向前移动 nw:光标向 ...

  5. tracert命令详解

    一.windows.Linux系统下 tracert ip/网站域名 二.mac traceroute IP/域名 ---------2016-10-10 15:29:07-- source:[1]t ...

  6. sp工具类编写

    package com.itheima.mobilesafe74.utils; import android.content.Context; import android.content.Share ...

  7. 转:桥接模式(Bridge)

    转自:http://www.cnblogs.com/rush/archive/2011/06/29/2093743.html  分析十分透彻明了  可以再结合另外一篇文章中的示例理解(http://b ...

  8. Hbase之批量删除数据

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  9. sql中charindex

    假如你写过很多程序,你可能偶尔会碰到要确定字符或字符窜串否包含在一段文字中,在这篇文章中,我将讨论使用CHARINDEX和PATINDEX函数来搜索文字列和字符串.我将告诉你这两个函数是如何运转的,解 ...

  10. semantic-ui dropdown is not a function

    按照semantic-ui官网示例,编写了如下示例,却不见效果. <div class="ui secondary menu"> <a class="i ...