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. 基于bootstrap的分页组件-Bootstrap Paginator

    效果

  2. input输入框获取焦点是背景黄色去除

    input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset !important;}

  3. RabbitMQ学习总结(6)——消息的路由分发机制详解

    一.Routing(路由) (using the Java client) 在前面的学习中,构建了一个简单的日志记录系统,能够广播所有的日志给多个接收者,在该部分学习中,将添加一个新的特点,就是可以只 ...

  4. [terry笔记]GoldenGate_迁移同步_主库零停机

    ogg根据scn同步数据,源库零停机时间 本次实验与上次的区别:更加注重细节,几乎包含所有步骤,把我越到的坑都作出了说明.并且同步是由10g向11g进行同步,更加符合升级迁移需求. 如下是主要步骤: ...

  5. SQL SERVER-union

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每个 SELECT 语句中 ...

  6. POJ——T2352 Stars

    http://poj.org/problem?id=2352 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46592   ...

  7. 洛谷 P1301 魔鬼之城

    P1301 魔鬼之城 题目描述 在一个被分割为N*M个正方形房间的矩形魔鬼之城中,一个探险者必须遵循下列规则才能跳跃行动.他必须从(1, 1)进入,从(N, M)走出:在每一房间的墙壁上都写了一个魔法 ...

  8. Codeforces Round #252 (Div. 2)-C,D

    C题就是一个简单的模拟.首先给每一个人两个.然后把剩下的都给一个人就好了. 给的时候蛇形给. #include<stdio.h> #include<string.h> #inc ...

  9. MySQL超级简明基本操作攻略,给自己看(一)

    系统:Ubuntu 14.04 LTS 安装: apt-get install mysql //安装数据库 apt-get install mysql-workbench //安装图形界面 使用: 启 ...

  10. Codeforce 163 A. Substring and Subsequence DP

    A. Substring and Subsequence   One day Polycarpus got hold of two non-empty strings s and t, consist ...