洛谷—— 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

输入输出样例

输入样例#1:

5
1
3
2
1
1

输出样例#1:

1

傻乎乎的n^2做法:求出最长上升和下降序列,用总数-其中较大的、、T两个点

 1 #include <algorithm>
 2 #include <cstdio>
 3
 4 using namespace std;
 5
 6 const int N(3e5+5);
 7 int n,cnt1,cnt2,x[N];
 8 int f1[N],f2[N];
 9
10 int main()
11 {
12     scanf("%d",&n);
13     for(int i=1;i<=n;i++) scanf("%d",x+i);
14     for(int i=2;i<=n;i++)
15         for(int j=1;j<i;j++)
16         {
17             if(x[j]<=x[i]) f1[i]=max(f1[i],f1[j]+1);
18             cnt1=max(cnt1,f1[i]);
19             if(x[j]>=x[i]) f2[i]=max(f2[i],f2[j]+1);
20             cnt2=max(cnt2,f2[i]);
21         }
22     printf("%d\n",n-max(cnt1,cnt2)-1);
23     return 0;
24 }

n^2 AAAAAAAATTA

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

 1 #include <cstring>
 2 #include <cstdio>
 3
 4 #define min(a,b) (a<b?a:b)
 5 const int N(3e5+5);
 6 int ans=1<<30,n,f[N][4],x[N];
 7
 8 int main()
 9 {
10     scanf("%d",&n);
11     for(int i=1;i<=n;i++)
12         scanf("%d",x+i);
13     memset(f,127/3,sizeof(f));
14     f[0][1]=f[0][2]=f[0][3]=0;
15     for(int i=1;i<=n;i++)
16       for(int j=1;j<=3;j++)
17       {
18           for(int k=1;k<=j;k++)
19               f[i][j]=min(f[i][j],f[i-1][k]);
20         if(x[i]!=j) f[i][j]++;
21       }
22     for(int i=1;i<=3;i++) ans=min(ans,f[n][i]);
23     memset(f,127/3,sizeof(f));
24     f[n+1][1]=f[n+1][2]=f[n+1][3]=0;
25     for(int i=n;i>=1;i--)
26       for(int j=1;j<=3;j++)
27       {
28           for(int k=1;k<=j;k++)
29               f[i][j]=min(f[i][j],f[i+1][k]);
30           if(x[i]!=j) f[i][j]++;
31       }
32     for(int i=1;i<=3;i++) ans=min(ans,f[1][i]);
33     printf("%d\n",ans);
34     return 0;
35 }
时间: 2024-11-10 11:37:03

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

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

P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 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 lin

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

P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 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 lin

洛谷 P2895 [USACO08FEB]流星雨Meteor Shower

P2895 [USACO08FEB]流星雨Meteor Shower 题目描述 Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe locatio

[luoguP2896] [USACO08FEB]一起吃饭Eating Together(DP)

传送门 由于 Di 只有 3 种情况,那么就很简单了 f[i][j][0] 表示前 i 个,且第 i 个变成 j 的 递增序列最小修改次数 f[i][j][1] 表示前 i 个,且第 i 个变成 j 的 递减序列最小修改次数 状态转移看代码. ——代码 1 #include <cstdio> 2 #include <iostream> 3 4 const int MAXN = 30001; 5 int n, ans = ~(1 << 31); 6 int a[MAXN]

洛谷P2982 [USACO10FEB]慢下来Slowing down(线段树 DFS序 区间增减 单点查询)

To 洛谷.2982 慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N move from the barn to her private pasture. The pastures are organized as a tree, with the barn being on pasture 1. Exactly N-1 cow

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3

洛谷1231 教辅的组成

洛谷1231 教辅的组成 https://www.luogu.org/problem/show?pid=1231 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书多得数不胜数,其中有书,有答案,有练习册.已知一个完整的书册均应该包含且仅包含一本书.一本练习册和一份答案,然而现在全都乱做了一团.许多书上面的字迹都已经模糊了,然而HansBug还是可

洛谷教主花园dp

洛谷-教主的花园-动态规划 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树的高度分别为10,20,30.教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高. 输入输出格式 输入格式: 输入文件garden.in的第1行为一个正整数n,表示需要种的

洛谷 P2801 教主的魔法 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2801 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是不超过1000的正整数.教主的魔法每次可以把闭区间[L, R](1≤L≤R≤N)内的英雄的身高全部加上一个整数W.(虽然L=R时并不