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

题意:将不同编号的牛改成升序123降序321的最小操作步骤。

数据30000。用nlogn的方法。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int maxn=30001;
int num[maxn],s[maxn]; int main()
{
int n,len1,len2;
int l,r,mid;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
memset(s,0,sizeof(s));
s[0]=-1;
len1=0;
for(int i=0;i<n;i++)//升123
{
if(num[i]>=s[len1])
s[++len1]=num[i];
else
{
l=1,r=len1;
while(l<=r)
{
mid=(l+r)>>1;
if(num[i]>=s[mid])
l=mid+1;
else
r=mid-1;
}
s[l]=num[i];
}
}
memset(s,0,sizeof(s));
len2=0;
s[0]=INT_MAX;
for(int i=0;i<n;i++)//降321
{
if(s[len2]>=num[i])
s[++len2]=num[i];
else
{
l=1,r=len2;
while(l<=r)
{
mid=(l+r)>>1;
if(num[i]<=s[mid])
l=mid+1;
else
r=mid-1;
}
s[l]=num[i];
}
}
int ans=n-max(len1,len2);
printf("%d\n",ans);
}
return 0;
}

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

  1. Poj 3903 Stock Exchange(LIS)

    一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...

  2. POJ 3670 Eating Together (DP,LIS)

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

  3. POJ 1631 Bridging signals(LIS O(nlogn)算法)

    Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...

  4. POJ 3903 Stock Exchange(LIS || 线段树)题解

    题意:求最大上升子序列 思路:才发现自己不会LIS,用线段树写的,也没说数据范围就写了个离散化,每次查找以1~a[i]-1结尾的最大序列答案,然后更新,这样遍历一遍就行了.最近代码总是写残啊... 刚 ...

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

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

  6. POJ 1631 Bridging signals(LIS的等价表述)

    把左边固定,看右边,要求线不相交,编号满足单调性,其实是LIS的等价表述. (如果编号是乱的也可以把它有序化就像Uva 10635 Prince and Princess那样 O(nlogn) #in ...

  7. POJ 1065 Wooden Sticks(LIS,最少链划分)

    题意:求二维偏序的最少链划分. 用到Dilworth定理:最少链划分=最长反链.(对偶也成立,个人认为区别只是一个维度上的两个方向,写了个简单的证明 相关概念:偏序集,链,反链等等概念可以参考这里:h ...

  8. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descripti ...

  9. POJ 1631 Bridging signals(LIS 二分法 高速方法)

    Language: Default Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1076 ...

随机推荐

  1. ActiveMQ学习笔记(6)----ActiveMQ整合Spring开发

    1. 添加依赖 spring 提供了对JMS的支持,需要添加Spring支持jms的包和Spring的核心包,如下: <dependency> <groupId>org.apa ...

  2. servlet中Cookie的编码问题

    a.什么是Cookie的编码问题?      Cookie只能存放合法的ascii字符,如果是非asicc字符(比如中文),     需要转换成合法的ascii字符的形式.  b.如何处理?     ...

  3. NodeJS学习笔记 (3)域名解析-dns(ok)

    域名解析:dns.lookup() 比如我们要查询域名 www.qq.com 对应的ip,可以通过 dns.lookup() . var dns = require('dns'); dns.looku ...

  4. CodeForces-1007A Reorder the Array 贪心 田忌赛马

    题目链接:https://cn.vjudge.net/problem/CodeForces-1007A 题意 给个数组,元素的位置可以任意调换 问调换后的元素比此位置上的原元素大的元素个数最大多少 思 ...

  5. iptables 配置端口及转发

    iptables端口转发指令:iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443 iptables配 ...

  6. salt 安装kubernetes集群3节点

    [root@linux-node1 k8s]# tree .├── etcd.sls├── files│   ├── cfssl-1.2│   │   ├── cfssl-certinfo_linux ...

  7. 平凡主丛上的Yang-Mills理论

    本文是复旦大学由丁青教授的暑期课程“Yang-Mills理论的几何及其应用”所作笔记,会有少许修正. 所需基础: 多元微积分学 微分方程(常微分方程,数学物理方程) 曲线曲面论(初等微分几何) 以下是 ...

  8. 制作PC端的安装程序

    一个多月不写博客了,不造大家有没有想我,(别自恋了,寥寥无几的粉丝,谁会想你),呜呜~~~ 好了,废话少叙,借用郭德纲老板的话,天儿不早了,干点正事儿吧! 一.序 Unity开发者都知道,打包出来的e ...

  9. Java IO(二) 之 InputStream

    源代码均以JDK1.8作为參考 前言: InputStream实现了两个接口Closeable和AutoCloseable: Closeable:JDK1.5中引入,Closeable接口中仅仅有一个 ...

  10. iOS_第3方类库_側滑选项卡SlideSwitchView

    终于效果: 用法: 1.在主控制器中创建一个[SlideSwitchView]的对象实例,并用成员变量记住,如_slideSwitchView,并加入到self.view 2.设置[_slideSwi ...