POJ 3670 Eating Together(LIS+nlogn算法)
来源:互联网 发布:淘宝买家服务热线 编辑:程序博客网 时间:2024/06/03 22:06
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
513211
1
【题解】 题意是给你一个序列,要你把这个序列变成一个单调非递减或者单调非递增序列,最少需要改变几个数的数值,(如1,3,2,1,1,要把它变成单调非递增序列,只要把第一个数变成>=3即可,此操作次数为1,而如果要变成单调非递减序列,则要把后三个数都变成>=3,操作次数为3,所以总的最小操作数就为1)。
很明显要正向逆向分别求一遍最长非递减子序列,取最大值,最后结果就是总个数m减去最大值。
因为数据量比较大,所以要用到nlongn算法,详见http://http://blog.csdn.net/qq_38538733/article/details/75258177
【AC代码】
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int N=3e4+5;int dp[N];int a[N];int m,maxn;int main(){ scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%d",&a[i]); dp[0]=0; int pos; int cnt=0; for(int i=1;i<=m;i++) { if(a[i]>=dp[cnt]) dp[++cnt]=a[i]; else { pos=upper_bound(dp,dp+cnt+1,a[i])-dp;//此函数返回的是下标,所以减去数组首元素下标,即为整型 dp[pos]=a[i]; } } maxn=cnt; dp[0]=0; cnt=0; for(int i=m;i>0;i--) { if(a[i]>=dp[cnt]) dp[++cnt]=a[i]; else { pos=upper_bound(dp,dp+cnt+1,a[i])-dp; dp[pos]=a[i]; } } maxn=maxn>cnt?maxn:cnt; printf("%d\n",m-maxn); return 0;}
- POJ 3670 Eating Together(LIS+nlogn算法)
- POJ 3670 Eating Together LIS最长递增子序列+二分查找 nlogn算法
- poj 3670 Eating Together(最长上升子序列 lis nlogn复杂度)
- POJ 3670 Eating Together(LIS)
- poj 3670 Eating Together LIS
- poj 3670 Eating Together LIS+二分
- poj 3670 Eating Together
- POJ 3670 (Eating Together)
- poj 3670 Eating Together(最长不下降子序列、LIS)
- POJ 3670 Eating Together (DP)
- POJ 3670 Eating Together(dp)
- poj 3670 Eating Together dp
- POJ3670 Eating Together LIS DP
- poj 3670 Eating Together
- POJ:3670 Eating Together(动态规划)
- poj 1631 LIS nlogn算法
- POJ 3670 Eating Together 二分解法O(nlgn)和O(n)算法
- POJ3670 Eating Together 【动态规划】【LIS模板】
- hdu2222(AC自动机)
- 查找:基本概念
- tomcat使用localhost可以访问,使用本机ip不能访问
- VMware Authorization Service不能启动 VMware虚拟机状态已挂起无法恢复解决方案
- C++ typename的起源与用法
- POJ 3670 Eating Together(LIS+nlogn算法)
- javascript的==和===的区别
- Unity 3D循环滚动效果
- 虚拟机下CentOS 配置IP地址的三种方法
- iOS App上架流程(细版)
- 教你彻底学会动态规划——入门篇
- java的基本数据类型
- c++left right 和 setw() 函数的用法Alignment of Code
- http请求的常见响应码