码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-04 21:47:46      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:ica   set   class   mem   rsh   describe   数字   clu   --   

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 }
View Code

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

标签:ica   set   class   mem   rsh   describe   数字   clu   --   

原文地址:http://www.cnblogs.com/huibixiaoxing/p/7118112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!