zoukankan      html  css  js  c++  java
  • 洛谷—— 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-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
    ",ans);
    34     return 0;
    35 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    2018 dnc .NET Core、.NET开发的大型网站列表、各大公司.NET职位精选,C#王者归来
    下载docker镜像报错,dial tcp x.x.x.x:443: connect: connection refused
    SpringMVC
    MyBatis操作数据库——增删改查
    MyBatis——Mapper配置并查询数据
    MyBatis工程搭建
    测试IOC&DI
    SpringAOP测试
    Spring连接Mysql
    Spring 工程搭建
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7434082.html
Copyright © 2011-2022 走看看