牛客假日团队赛7:
链接:https://ac.nowcoder.com/acm/contest/997/G
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
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
    输入
    复制
    513211

输出
复制

1

说明
We would need at least two changes to turn this into an increasing sequence (changing both non-1’s to a 1).
However, changing the first “1” to a “3” yields a decreasing sequence with just one change, which is optimal.
题意:
一个只由1,2,3组成的序列,改变最少的数使序列成为上升或下降序列。
很明显就是求一个最长上升子序列和最长下降子序列,需要修改的数量ans = N -两个子序列中长度的较大值。
N达到3e4,所以用基于贪心的O(NlogN)解法
AC_code:

#include <bits/stdc++.h>
using namespace std;
const int N = 3e4+4;
int a[N];
int st[N];
int main()
{int n;cin>>n;int t = 0;st[t] = 0;for(int i = 1; i <= n; i++){cin>>a[i];if(a[i] >= st[t]){st[++t] = a[i];}else{int l = 1,r = t;int res = 0;while(l <= r){int mid = (l+r)>>1;if(st[mid] > a[i]){res = mid;r = mid - 1;}else l = mid+1;}st[res] = a[i];}}int maxx = t;t = 0;st[t] = 10;for(int i = 1; i <= n; i++){if(a[i] <= st[t]){st[++t] = a[i];}else{int l = 1,r = t;int res = 0;while(l <= r){int mid = (l+r)>>1;if(st[mid] < a[i]){res = mid;r = mid - 1;}else l = mid+1;}st[res] = a[i];}}maxx = max(maxx,t);cout<<(n-maxx)<<endl;return 0;
}

G.Eating Together(LIS,LDS)相关推荐

  1. BZOJ 1609 [Usaco2008 Feb]Eating Together麻烦的聚餐:LIS LDS (nlogn)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1609 题意: 给你一个只由数字"1,2,3"组成的序列a[i],共n个 ...

  2. G - Eating Plan

    G - Eating Plan 题意: 一个1到n组成的排列,每个数的价值为其阶乘,有m个询问ki,要求你在排列中选取连续的一块,使得价值和mod 998857459 后,大于ki,问最短区间长度,如 ...

  3. bzoj 4660 Crazy Rabbit——LIS解决“相交”限制的思想

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4660 想到一个点可以用它与圆的两个切点表示.并想到可以把切点极角排序,那么就变成环上的一些区 ...

  4. linux启停was命令,linux下的启停脚本

    linux下的根据项目名称,进行进程的启停脚本 #!/bin/bash JAVA=/usr/bin/java APP_HOME=/opt/program/qa/wechat APP_NAME=prog ...

  5. ICPC2019南昌区域赛

    ICPC2019南昌区域赛 题号 题目 知识点 难度 A 9102 B A Funny Bipartite Graph 状压dp+思维 稳银快金 C And and Pair 二项式定理 快铜 D B ...

  6. 数组排序最小复杂度_进行排序的最小缺失数

    数组排序最小复杂度 Problem statement: 问题陈述: Given an array of n integers. Find the minimum number of elements ...

  7. 推荐系统遇上深度学习(一)--FM模型理论和实践

    全文共2503字,15张图,预计阅读时间15分钟. FM背景 在计算广告和推荐系统中,CTR预估(click-through rate)是非常重要的一个环节,判断一个商品的是否进行推荐需要根据CTR预 ...

  8. python重复输入字符串a三次_8道Python基础面试练习题

    1.26个字母大小写成对打印,例如:Aa,Bb...... for i in range(26): print(chr(65+i)+chr(97+i)) 2.一个list包含10个数字,然后生成一个新 ...

  9. python元素相邻_在python中,如何根据键(相邻组)将元素分组在...

    您可以使用itertools.groupby: >>> from itertools import groupby >>> from operator import ...

最新文章

  1. jQuery实现父窗口的问题
  2. 如何成为一名入门级 iOS 开发者
  3. Win11控制面板里面怎么找到系统安全?
  4. oracle约束 关闭,Oracle约束管理脚本
  5. DIV实现CSS 的placeholder效果
  6. layUI数据表格可编辑表格单元格值修改之后获取修改前的值
  7. Markdown - 语法简介
  8. 基于 Spark推荐 系统应用 现 状
  9. excel导入sqlserver(asp版)
  10. matlab调和均值滤波_matlab-均值滤波
  11. 软件工程实验报告三--需求分析及文档编写(模板)
  12. CDA I级学习 - CDA I级报名
  13. 翰文付费打印后还有水印吗_翰文进度计划编制系统去除水印中文增强版
  14. 关于工信部191号文《App违法违规收集使用个人信息行为认定方法》的评估
  15. 2017年2月28日-----------乱码新手自学.net 之特性与验证
  16. android x86 驱动精灵,手机驱动精灵软件下载(5个步骤即可解决)
  17. MATLAB 将图像随机裁剪为N*N的图像块
  18. 云服务器到底是什么?云服务器的优势有哪些
  19. Qt 一个简单的word文档编辑器
  20. 广东移动至2020年数据中心机柜规模将近10万架

热门文章

  1. c语言左移15位,关于c语言中左移右移运算符的问题
  2. python django框架分析_Django框架模型简单介绍与使用分析
  3. jar 工程我怎么在网页上url访问某一个方法_搜狗用这个骚技术,把百度逼上了绝路。。。...
  4. win10弹不出密码输入框_电脑互访提示输入网络密码,怎么办?
  5. 解决Linux下启动Tomcat遇到Neither the JAVA_HOME nor the JRE_HOME environment variable is defined...
  6. 一个已经存在 10 年,却被严重低估的库
  7. 小程序分享到朋友圈功能_来啦!小程序支持分享朋友圈
  8. 电大计算机应用模块四实2010,国家开放大学《计算机应用基础》考试与答案形考任务模块2Word2010文字处理系统—客观题测验答案.docx...
  9. go ssh 执行多个命令_每天一个 Linux 命令(92): dirname 命令
  10. python3输入框_Python3 tkinter基础 Entry get 点击按钮 将输入框中文字输出到控制台...