POJ 3670 Eating Together(LIS)
|
Description 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 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 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. Input * Line 1: A single integer: N Output * 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 Sample Input 5 Sample Output 1 Source |
题意:将不同编号的牛改成升序123降序321的最小操作步骤。
数据30000。用nlogn的方法。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int maxn=30001;
int num[maxn],s[maxn]; int main()
{
int n,len1,len2;
int l,r,mid;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
memset(s,0,sizeof(s));
s[0]=-1;
len1=0;
for(int i=0;i<n;i++)//升123
{
if(num[i]>=s[len1])
s[++len1]=num[i];
else
{
l=1,r=len1;
while(l<=r)
{
mid=(l+r)>>1;
if(num[i]>=s[mid])
l=mid+1;
else
r=mid-1;
}
s[l]=num[i];
}
}
memset(s,0,sizeof(s));
len2=0;
s[0]=INT_MAX;
for(int i=0;i<n;i++)//降321
{
if(s[len2]>=num[i])
s[++len2]=num[i];
else
{
l=1,r=len2;
while(l<=r)
{
mid=(l+r)>>1;
if(num[i]<=s[mid])
l=mid+1;
else
r=mid-1;
}
s[l]=num[i];
}
}
int ans=n-max(len1,len2);
printf("%d\n",ans);
}
return 0;
}
POJ 3670 Eating Together(LIS)的更多相关文章
- Poj 3903 Stock Exchange(LIS)
一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...
- POJ 3670 Eating Together (DP,LIS)
题意:给定 n 个数,让你修改最少的数,使得它变成一个不下降或者不上升序列. 析:这个就是一个LIS,但是当时并没有看出来...只要求出最长LIS的长度,用总数减去就是答案. 代码如下: #inclu ...
- POJ 1631 Bridging signals(LIS O(nlogn)算法)
Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...
- POJ 3903 Stock Exchange(LIS || 线段树)题解
题意:求最大上升子序列 思路:才发现自己不会LIS,用线段树写的,也没说数据范围就写了个离散化,每次查找以1~a[i]-1结尾的最大序列答案,然后更新,这样遍历一遍就行了.最近代码总是写残啊... 刚 ...
- POJ 3670 Eating Together 二分解法O(nlgn)和O(n)算法
本题就是一题LIS(最长递增子序列)的问题.本题要求求最长递增子序列和最长递减子序列. dp的解法是O(n*n),这个应该大家都知道.只是本题应该超时了. 由于有O(nlgn)的解法. 可是因为本题的 ...
- POJ 1631 Bridging signals(LIS的等价表述)
把左边固定,看右边,要求线不相交,编号满足单调性,其实是LIS的等价表述. (如果编号是乱的也可以把它有序化就像Uva 10635 Prince and Princess那样 O(nlogn) #in ...
- POJ 1065 Wooden Sticks(LIS,最少链划分)
题意:求二维偏序的最少链划分. 用到Dilworth定理:最少链划分=最长反链.(对偶也成立,个人认为区别只是一个维度上的两个方向,写了个简单的证明 相关概念:偏序集,链,反链等等概念可以参考这里:h ...
- POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- POJ 1631 Bridging signals(LIS 二分法 高速方法)
Language: Default Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1076 ...
随机推荐
- git远程仓库变更
查看自己的远程仓库 git remote -v 远程仓库变更 git remote remove origin //移出现有的远程仓库的地址 git remote add origin http:// ...
- json对象获取长度以及字符串和json对象的转换
var arr = Object.keys(typeARR); var str = ''; var len = arr.length; for(var i = 0;i<len;i++){ str ...
- 洛谷 P1417 烹调方案 (01背包拓展)
一看到这道题就是01背包 但是我注意到价值和当前的时间有关. 没有想太多,直接写,0分 然后发现输入方式不对-- 改了之后只有25分 我知道wa是因为时间会影响价值,但不知道怎么做. 后来看了题解,发 ...
- 嵌入式表单字段中的内容可能被server更改以删除不安全的内容。是否要又一次载入您的页面以查看保存结果?
嵌入式表单字段中的内容可能被server更改以删除不安全的内容.是否要又一次载入您的页面以查看保存结果? 近期有朋友问到,当他在SharePoint首页上进行编辑时.插入一段代码. 完 ...
- 死锁的Dump文件
死锁的Dump文件 package com.stono.thread; public class DeadLockDemo { private static String A = "A&qu ...
- 使用Qt.labs.settings来存储应用的设置
我在曾经的文章中,讲述了怎样使用U1db及SQLite offline storage API来存储应用的一些状态.在这篇文章中,我将介绍怎样使用Qt.labs.settings来存储应用的状态.更加 ...
- 公布Qt Widgets桌面应用程序的方法
公布Qt Widgets桌面应用程序的方法 Qt是一款优秀的跨平台开发框架,它能够在桌面.移动平台以及嵌入式平台上执行.眼下Qt 5介绍程序公布的文章帖子比較少.大家又很想要知道怎样公布Qt应用程序, ...
- android开发之Animation(五)
android开发之Animation的使用(五) 本博文主要讲述的是Animation中的AnimationLisenter的用法,以及此类的一些生命周期函数的调用,代码实比例如以下: MainAc ...
- thinkphp 中模型究竟是什么用?
thinkphp 中模型究竟是什么用? 问题 似乎所有的操作都能在控制器中就能完成,模型除了几种验证之外,究竟是干什么用的,这个问题一直没理解透 解答 解答一 要明白这个问题,必须了解 MVC 历史. ...
- 福建省赛--Problem E The Longest Straight(标记+二分)
Problem E The Longest Straight Accept: 71 Submit: 293 Time Limit: 1000 mSec Memory Limit : 327 ...