洛谷—— 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; ...
随机推荐
- unity 获取UGUI中的Text字的坐标
using System.Collections; using UnityEngine; using UnityEngine.UI; public class TextMoveHelper : Mon ...
- C语言实现将一个整形数转换为两个字节16进制
有时候要用到这个转换,这里记录一下,例如把 int a = 164 转换储存在数组里为 uint8_t b[0]=0x00 , b[1]=0xA4 . 很简单,转换如下: b[0] = a > ...
- static和extern的作用域--题目
#include <stdio.h> ; int main(void) { , sum = , count = ; ,count++) // count = 2 { ; count++; ...
- ASP.NET-Session与复杂数据类型
原文链接:http://www.cnblogs.com/fish-li/archive/2013/05/28/3104750.html Session与复杂数据类型 Session有三种工作模式,拿A ...
- cogs 304. [NOI2001] 方程的解数(meet in the middle)
304. [NOI2001] 方程的解数 ★★☆ 输入文件:equation1.in 输出文件:equation1.out 简单对比时间限制:3 s 内存限制:64 MB 问题描述 已 ...
- CURL库的宏定义列表
列表CURL库一共同拥有17个函数 curl_close:关闭CURL会话 curl_copy_handle:复制一个CURL会话句柄,同一时候3复制其全部參数 curl_errno:返回最后一个错误 ...
- 【Android】Android程序自己主动更新
App自己主动更新的步骤可分为三步: 检查更新(假设有更新进行第2步,否则返回) 下载新版的APK安装包 安装APK 以下对这三步进行解释.当中会穿插相应代码.App自己主动更新的这三步所有被封装到了 ...
- 基于Dragon Board410c 的智能机器人预研-语音识别及定位
转自:http://www.csdn.net/article/a/2016-01-06/15833642 一.前言 机器人是一种可编程和多功能的.用来搬运材料.零件.工具的操作机,智能机器人则是一个在 ...
- iOS UI10_带分区的省市区
// // MainViewController.m // UI10_带分区的省市区 // // Created by dllo on 15/8/11. // Copyright (c) 2015年 ...
- windowsclient崩溃分析和调试
本文介绍windows上崩溃分析的一些手段,顺便提多进程调试.死锁等. 1.崩溃分析过程 1.1 确认错误码 不管是用windbg还是用vs.首先应该注意的是错误码,而90%以上的崩溃都是非法訪问. ...