洛谷P2896 [USACO08FEB]一起吃饭Eating Together
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷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
输入输出样例
5 1 3 2 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 }
以上是关于洛谷P2896 [USACO08FEB]一起吃饭Eating Together的主要内容,如果未能解决你的问题,请参考以下文章
洛谷P2896 [USACO08FEB]一起吃饭Eating Together
洛谷—— P2896 [USACO08FEB]一起吃饭Eating Together
[luoguP2896] [USACO08FEB]一起吃饭Eating Together(DP)
洛谷 P2665 [USACO08FEB]连线游戏Game of Lines