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

本文介绍了一种算法问题,即如何通过最少的操作次数将一群牛按其晚餐伙伴卡的编号分成递增或递减的组别。提供了两种解决方案,一种是n²复杂度的方法,另一种则是更高效的利用动态规划思想来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

 

转载于:https://www.cnblogs.com/Shy-key/p/7434082.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值