题目描述

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.

每次可以改变一个数字,要求使给定的数列变成单调递增或递减,求最小操作数

输入输出格式

输入格式:

  • 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

输出格式:

  • 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

输入输出样例

输入样例#1:

5
1
3
2
1
1
输出样例#1:

1
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,a[],f[][][];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++){
f[i][][]=f[i-][][]+(a[i]!=);
f[i][][]=min(f[i-][][],f[i-][][])+(a[i]!=);
f[i][][]=min(f[i-][][],min(f[i-][][],f[i-][][]))+(a[i]!=);
f[i][][]=min(f[i-][][],min(f[i-][][],f[i-][][]))+(a[i]!=);
f[i][][]=min(f[i-][][],f[i-][][])+(a[i]!=);
f[i][][]=f[i-][][]+(a[i]!=);
}
cout<<min(f[n][][],min(f[n][][],min(f[n][][],min(f[n][][],min(f[n][][],f[n][][])))));
}
 

洛谷 P2896 [USACO08FEB]一起吃饭Eating Together的更多相关文章

  1. 洛谷P2896 [USACO08FEB]一起吃饭Eating Together

    题目描述 The cows are so very silly about their dinner partners. They have organized themselves into thr ...

  2. 洛谷—— P2896 [USACO08FEB]一起吃饭Eating Together

    https://www.luogu.org/problem/show?pid=2896 题目描述 The cows are so very silly about their dinner partn ...

  3. bzoj1609 / P2896 [USACO08FEB]一起吃饭Eating Together(最长不降子序列)

    P2896 [USACO08FEB]一起吃饭Eating Together 显然的最长不升/降子序列,求出最长值,则答案为$n-$最长值(改掉剩下的). 复杂度$O(nlogn)$ (然鹅有神仙写了$ ...

  4. P2896 [USACO08FEB]一起吃饭Eating Together

    传送门 可以考虑DP 设 f [ i ] [ 1/2/3 ] [ 0/1 ] 表示当前考虑到第 i 头牛,打算让当前位置的编号变成 1/2/3,并且打算让整段序列上升/下降 0/1 然后就对每种情况慢 ...

  5. 洛谷P2894 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...

  6. 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  7. 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  8. 洛谷 P2893 [USACO08FEB]修路Making the Grade 解题报告

    P2893 [USACO08FEB]修路Making the Grade 题目描述 A straight dirt road connects two fields on FJ's farm, but ...

  9. 洛谷—— P2895 [USACO08FEB]流星雨Meteor Shower

    P2895 [USACO08FEB]流星雨Meteor Shower 题目描述 Bessie hears that an extraordinary meteor shower is coming; ...

随机推荐

  1. 单向链表的归并排序——java实现

    在做Coursera上的Algorithms第三周测验练习的时候有一道链表随机排序问题,刚开始没有什么思路,就想着先把单向链表归并排序实现了,再此基础上进行随机排序的改造.于是就结合归并排序算法,实现 ...

  2. 1046: [HAOI2007]上升序列(dp)

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4999  Solved: 1738[Submit][Stat ...

  3. linux随便贴贴

    在bin目录下进入mysql: ./mysql -uroot -p123456 update mysql.user set password=password('root') where user=' ...

  4. this引用逃逸问题

    //this引用逃逸 // 1.构造器还未完成前,将自身this引用向外抛,使其他线程访问这个引用,进而访问到其未初始化的变量,造成问题 // 2.内部类访问外部类未初始化的成员变量 //3.多态继承 ...

  5. 基于CGAL的Delaunay三角网应用

    目录 1. 背景 1.1 CGAL 1.2 cgal-bindings(Python包) 1.3 vtk-python 1.4 PyQt5 2. 功能设计 2.1 基本目标 2.2 待实现目标 3. ...

  6. String和八种基本数据类型互相转换

    //String转换为对应的八种基本数据类型 String str="100"; //Value out of range. Value:"200" Radix ...

  7. Bootstrap3.0的栅格布局系统实现原理

    这个标题取的有点奇怪,怪我翻译的有问题吧.英文学平有限,有道词典和google翻译齐上阵是必须的.还好翻译的不是小说,对于技术文章,还是能勉强翻过来的. 本文主要讲解了Bootstrap3.0的栅格布 ...

  8. JavaScript中比较运算符的使用

    比较运算符的基本操作过程是:首先对操作数进行比较,这个操作数可以是数字也可以是字符串,然后返回一个布尔值true或false. 在JavaScript中常用的比较运算符如下表所示. 例如,某商场店庆搞 ...

  9. Android6.0以上版本获取本机蓝牙地址

    Android6.0以上版本使用BluetoothAdapter.getDefaultAdapter().getAddress()是获取不到正确的蓝牙地址的,返回的值都是02:00:00:00:00: ...

  10. JAVA可能问的几个面试题问题及问题的标准答案

    问题一:你希望工作环境是怎样的? 标准答案:我对环境没有苛求,我会努力适应环境的. 注:此问题是在测试你的求职心态,是以自己为中心还是以工作为中心. 问题二:你觉得自己有哪些缺点? 标准答案:说一些对 ...