洛谷 P2896 [USACO08FEB]一起吃饭Eating Together
题目描述
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
输入输出样例
5
1
3
2
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的更多相关文章
- 洛谷P2896 [USACO08FEB]一起吃饭Eating Together
题目描述 The cows are so very silly about their dinner partners. They have organized themselves into thr ...
- 洛谷—— P2896 [USACO08FEB]一起吃饭Eating Together
https://www.luogu.org/problem/show?pid=2896 题目描述 The cows are so very silly about their dinner partn ...
- bzoj1609 / P2896 [USACO08FEB]一起吃饭Eating Together(最长不降子序列)
P2896 [USACO08FEB]一起吃饭Eating Together 显然的最长不升/降子序列,求出最长值,则答案为$n-$最长值(改掉剩下的). 复杂度$O(nlogn)$ (然鹅有神仙写了$ ...
- P2896 [USACO08FEB]一起吃饭Eating Together
传送门 可以考虑DP 设 f [ i ] [ 1/2/3 ] [ 0/1 ] 表示当前考虑到第 i 头牛,打算让当前位置的编号变成 1/2/3,并且打算让整段序列上升/下降 0/1 然后就对每种情况慢 ...
- 洛谷P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 洛谷 P2893 [USACO08FEB]修路Making the Grade 解题报告
P2893 [USACO08FEB]修路Making the Grade 题目描述 A straight dirt road connects two fields on FJ's farm, but ...
- 洛谷—— P2895 [USACO08FEB]流星雨Meteor Shower
P2895 [USACO08FEB]流星雨Meteor Shower 题目描述 Bessie hears that an extraordinary meteor shower is coming; ...
随机推荐
- bzoj3224: Tyvj 1728 普通平衡树(平衡树)
bzoj3224: Tyvj 1728 普通平衡树(平衡树) 总结 a. cout<<(x=3)<<endl;这句话输出的值是3,那么对应的,在splay操作中,当父亲不为0的 ...
- hibernate初步2
Hibernate级联设计 数据库表之间的关系(主要关系有一对多.一对一.多对多)主要是从如下三个方面体现出来: 1.表体设计 2.实体类的设计 3.配置文件 以下是一些重要级联设计参数属性介绍: c ...
- JFreeChart之堆叠柱形图(StackedBar)
JFreeChart之堆叠柱形图(StackedBar) JAVA JFreeChart 最近的项目使用有个功能需要使用到堆叠柱形图,看了项目以前的代码实现没有想要的结果.所以自己就先到官网下载了 D ...
- Hamming Distance(随机算法)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:计算任意两个十六进制的数异或后1的最少个数. 思路:用随机数随机产生两个数作为下标,记录这两个数异或 ...
- selenium3 + python - select定位
一.Select模块(index) 1.导入Select模块.直接根据属性或索引定位 2.先要导入select方法:from selenium.webdriver.support.se ...
- B - Magnets
Problem description Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't ...
- 移动端弹性滑动以及vue记录滑动位置
-webkit-overflow-scrolling介绍 -webkit-overflow-scrolling: auto | touch; auto: 普通滚动,当手指从触摸屏上移开,滚动立即停止 ...
- Python+selenium第一个自动化脚本
第一个自动化脚本(用Python写的) from selenium import webdriver #从selenium导入webdriber driver=webdriber.Firefox() ...
- 解决JavaOpenCV的内存问题
在使用OpenCV时,程序总是在某个时间墨明棋妙地终止,打开CygWin ,输入 adb logcat ,查看打印的信息,发现是内存问题.经过反复的查找,发现使用OpenCV的java类库时,一定要慎 ...
- 新书《计算机图形学基础(OpenGL版)》PPT已发布
为方便有些老师提前备课,1-10章所有章节已发布到本博客中. 欢迎大家下载使用,也欢迎大家给我们的新书反馈与意见,谢谢!