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. composer install或者update 出错

    composer install或者update  出错Your requirements could not be resolved to an installable set of package ...

  2. HDU 2048 神、上帝以及老天爷( 错排 )

    链接:传送门 思路:错排模板,典型错排问题,n个人所有人都不会抽到自己的方案数为 Dn = (n-1) * (Dn-1 + Dn-2) /******************************* ...

  3. WIN10 java环境变量问题之 配置的JDK1.8版本却是1.7

    问题前沿,在开发项目中,发布的项目出现了内存溢出问题,我挨个把代码看了一遍,并不能准确定位到那个地方能出现内存溢出问题,后来想到使用压力测试,较可能出现内存溢出的接口进行一番测试. 我就安装了一个ap ...

  4. jboss <context /> 配置虚拟路径,映射本地文件

    这个目录下的server.xml <host><host/>标签中添加: <Context path="/file" docBase="D: ...

  5. tortoiseGit怎么记住密码

    tortoiseGit每次pull和push的时候都要输入git密码很是麻烦,下面是tortoiseGit记住密码的步骤 首先在你的项目界面右键 选择setting,这个步骤知识看一下你的名称和ema ...

  6. 【codeforces 508E】Artur and Brackets

    [题目链接]:http://codeforces.com/problemset/problem/508/E [题意] 让你构造一个括号字符串; 使得每个从左往右数第i个左括号在这个括号序列中与之匹配的 ...

  7. [Angular] Provide Feedback to Progress Events with Angular’s HttpRequest Object

    In some cases your application might need to upload large amounts of data, such as files. Obviously ...

  8. bzoj1570: [JSOI2008]Blue Mary的旅行(二分+网络流)

    1570: [JSOI2008]Blue Mary的旅行 题目:传送门 题解: get到拆点新姿势,还是做题太少了...ORZ 因为每天就只能有一个航班,那就不能直接连了,所以要拆点(然后就被卡住了) ...

  9. CentOS7安装EPEL的两种方式

    转自:http://www.mamicode.com/info-detail-1671603.html epel是社区强烈打造的免费开源发行软件包版本库. EPEL,即Extra Packages f ...

  10. [jzoj 4528] [GDOI2019模拟2019.3.26] 要换换名字 (最大权闭合子图)

    题目链接: https://jzoj.net/senior/#contest/show/2683/0 题目: 题解: 不妨枚举一个点,让两颗树都以这个点为根,求联通块要么点数为$0$,要么包括根(即联 ...