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

【题解】
分别求LIS和LDS,用n去减,比较最小值即可

 1 #include <bits/stdc++.h>
 2 const int INF = 0x3f3f3f3f;
 3 const int MAXN = 30000 + 10;
 4 inline void read(int &x){x = 0;char ch = getchar();char c = ch;while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();if(c == ‘-‘)x = -x;}
 5 inline int max(int a, int b){return a > b ? a : b;}
 6 inline int min(int a, int b){return a < b ? a : b;}
 7
 8 int n, num[MAXN],dp[MAXN], f[MAXN];
 9 int ma,ans;
10
11 int main()
12 {
13     read(n);
14     for(register int i = 1;i <= n;++ i)
15     {
16         read(num[i]);
17     }
18
19     //递增走一遍
20     memset(f, 0x3f, sizeof(f));
21     ma = -1;
22     for(register int i = 1;i <= n;++ i)
23     {
24         f[dp[i] = std::upper_bound(f + 1, f + 1 + i, num[i]) - f] = num[i];
25         ma = max(ma, dp[i]);
26     }
27     ans = n - ma;
28
29     //递减走一遍
30     memset(f, 0x3f, sizeof(f));
31     memset(dp, 0, sizeof(dp));
32     ma = -1;
33     for(register int i = n;i >= 1;-- i)
34     {
35         f[dp[i] = std::upper_bound(f + 1, f + n + 1, num[i]) - f] = num[i];
36         ma = max(ma, dp[i]);
37     }
38     ans = min(ans, n - ma);
39     printf("%d", ans);
40     return 0;
41 }

时间: 2024-08-03 19:29:15

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

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 th

洛谷 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时并不