洛谷—— P2896 [USACO08FEB]一起吃饭Eating Together
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
输入输出样例
5
1
3
2
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的更多相关文章
- 洛谷 P2896 [USACO08FEB]一起吃饭Eating Together
P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 The cows are so very silly about their dinner partners. T ...
- 洛谷P2896 [USACO08FEB]一起吃饭Eating Together
题目描述 The cows are so very silly about their dinner partners. They have organized themselves into thr ...
- 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; ...
随机推荐
- .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
最近在项目中与别的公司对接业务,对方是Java语言,需要调用对方的WebServices,结果常规的添加web引用的方法可以传过去值,但是返回值为null 查了很多资料,没有解决方法 思考应该是.Ne ...
- 代理上网环境配置docker私有库
最后更新时间:2018年12月27日 Docker使用代理上网去 pull 各类 images,需要做如下配置: 创建目录: /etc/systemd/system/docker.service.d ...
- 利用CORS解决前后端分离的跨域资源问题
CORS 即CrossOrigin Resources Sharing-跨域资源共享,它定义了一种浏览器和服务器交互的方式来确定是否允许跨域请求.它是一个妥协,有更大的灵活性,但比起简单地允许所有这些 ...
- VUE:模板语法(小白自学)
VUE:模板语法 一:何为声明式 安装规定的语法,去实现一些效果(不需要管流程). 二:模板语法 <!DOCTYPE html> <html> <head> < ...
- oracle 数据类型及函数
第一节:字符串类型及函数 字符类型分 3 种,char(n) .varchar(n).varchar2(n) : char(n)固定长度字符串,假如长度不足 n,右边空格补齐: varchar(n)可 ...
- DQL查询语句使用(select)
9)DQL查询语句使用 SELECT语句在PL/SQL中使用,必须 采用下面用法: select id INTO 变量 from t001 where id=5; 将记录字段 ...
- java源码之TreeSet
1,TreeSet介绍 1)TreeSet 是一个有序的集合,它的作用是提供有序的Set集合.2)TreeSet 继承于AbstractSet,所以它是一个Set集合,具有Set的属性和方法.3)Tr ...
- Nginx Zabbix
https://www.cnblogs.com/wangxiaoqiangs/archive/2016/04/20/5412111.html
- java properties类读取配置文件
1.JAVA Properties类,在java.util包里,具体类是java.util.properties.Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值 ...
- Java生成验证码_转
为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下;这是一个web工程, ...