https://www.luogu.org/problem/show?pid=2896

题目描述

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

傻乎乎的n^2做法:求出最长上升和下降序列,用总数-其中较大的、、T两个点
 #include <algorithm>
#include <cstdio> using namespace std; const int N(3e5+);
int n,cnt1,cnt2,x[N];
int f1[N],f2[N]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",x+i);
for(int i=;i<=n;i++)
for(int j=;j<i;j++)
{
if(x[j]<=x[i]) f1[i]=max(f1[i],f1[j]+);
cnt1=max(cnt1,f1[i]);
if(x[j]>=x[i]) f2[i]=max(f2[i],f2[j]+);
cnt2=max(cnt2,f2[i]);
}
printf("%d\n",n-max(cnt1,cnt2)-);
return ;
}

n^2 AAAAAAAATTA

应为题目说d€[1,3]    考虑用f[i][d]表示第i个数变成d的最小步数,正反跑一边解决递增或是递减

 #include <cstring>
#include <cstdio> #define min(a,b) (a<b?a:b)
const int N(3e5+);
int ans=<<,n,f[N][],x[N]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",x+i);
memset(f,/,sizeof(f));
f[][]=f[][]=f[][]=;
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
{
for(int k=;k<=j;k++)
f[i][j]=min(f[i][j],f[i-][k]);
if(x[i]!=j) f[i][j]++;
}
for(int i=;i<=;i++) ans=min(ans,f[n][i]);
memset(f,/,sizeof(f));
f[n+][]=f[n+][]=f[n+][]=;
for(int i=n;i>=;i--)
for(int j=;j<=;j++)
{
for(int k=;k<=j;k++)
f[i][j]=min(f[i][j],f[i+][k]);
if(x[i]!=j) f[i][j]++;
}
for(int i=;i<=;i++) ans=min(ans,f[][i]);
printf("%d\n",ans);
return ;
}

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

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

    P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 The cows are so very silly about their dinner partners. T ...

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

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

  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. Numpy的使用规则

    之前安装的python版本是3.7 各种库都是自己一个一个下载安装的 很操心 各种缺功能 后来发现了anaconda 啊 真是一个好东西 简单来说 它就是一个涵盖大部分常用库的python包 一次安装 ...

  2. 2015 Multi-University Training Contest 5 hdu 5352 MZL's City

    MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. .C#-NET开源方向基本

    我的理解,nancyFx是一个.net的微型框架,可以在Linux环境下运行,ServiceStack也是全平台框架,更大一些 owin的概念:Open Web Server Interface Fo ...

  4. 洛谷 P1490 买蛋糕

    P1490 买蛋糕 题目描述 野猫过生日,大家当然会送礼物了(咳咳,没送礼物的同志注意了哈!!),由于不知道送什么好,又考虑到实用性等其他问题,大家决定合伙给野猫买一个生日蛋糕.大家不知道最后要买的蛋 ...

  5. ssm框架下上传图片及其他信息

    先引入这两个包: <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...

  6. Qt之命令行参数

    简述 在Qt之进程间通信(QProcess)一节,我们讲解了如何通过QProcess来进行进程间的通信.主要通过启动外部程序,然后通过命令行的方式传递参数. 这里,我们可以通过Qt Creator来设 ...

  7. [Ubuntu]安装中文输入法

    安装了英文版的Ubuntu12.04之后.使用ctrt+space无法切换到中文,才反应过来. 于是查询了下怎么在Ubuntu下安装和配置中文输入法. 第一步安装语言包: 在左側的菜单条中点击 sys ...

  8. HDU 4332 Contest 4

    顶好的一道题.其实,是POJ 2411的升级版.但POJ 2411我用的插头DP来做,一时没想到那道题怎么用状态DP,于是回头看POJ 2411那一道的状态DP,其实也很简单,就是每一行都设一个状态, ...

  9. cocos2d-x-lua基础系列教程一(hello lua)

    myscene.lua function ERROR_TRACBACK(msg) print (==========) print ("lua error is "..tostri ...

  10. 子文件夹的遍历(python、matlab)

    1. python 使用 os.listdir:Python Tricks(九)-- 递归遍历目录下所有文件 使用 os.walk: os.walk返回的是生成器(Generator),需迭代访问: ...