【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5773
题目大意:
T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何自然数)
题目思路:
【动态规划】【二分】【最长上升子序列】
按最长上升子序列做,遇到0的时候更新所有长度的最优解。(这种暴力解法都能过?而且还比标解快?)
//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int a[N],q[N];
void work()
{
int i,j,l,r,mid;
lll=;
memset(q,-,sizeof(q));
for(i=;i<=n;i++)
{
if(a[i]==)
{
q[lll+]=q[lll]+;
for(j=lll;j>;j--)q[j]=min(q[j-]+,q[j]);
q[]=;lll++;
continue;
}
l=,r=lll;
while(l<r)
{
mid=(l+r+)>>;
if(a[i]>q[mid])l=mid;
else r=mid-;
}
q[r+]=a[i];
lll=max(lll,r+);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j;
// for(scanf("%d",&cas);cas;cas--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
printf("Case #%d: ",cass);
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
work();
printf("%d\n",lll);
}
return ;
}
/*
// //
*/
正解是把0先都拿出来,非0的数都减去它前面0的个数(0可以变成任何自然数),求最长上升子序列,再把0的数加上即为答案。
//
//by coolxxx
////<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int a[N],q[N];
void work()
{
int i,j,l,r,mid;
lll=;
memset(q,-,sizeof(q));
for(i=;i<=n;i++)
{
l=,r=lll;
while(l<r)
{
mid=(l+r+)>>;
if(a[i]>q[mid])l=mid;
else r=mid-;
}
q[r+]=a[i];
lll=max(lll,r+);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j;
// for(scanf("%d",&cas);cas;cas--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
printf("Case #%d: ",cass);
scanf("%d",&n);
m=;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==)n--,i--,m++;
else a[i]-=m;
}
work();
printf("%d\n",lll+m);
}
return ;
}
/*
// //
*/
【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero的更多相关文章
- HDU 4604 Deque 二分最长上升子序列
题目大意就是给一个deque 然后有n个数,依次进行操作,每种操作,你可以把这个数放在deque首部,也可以放在尾部,也可以扔掉不管,但是要保证deque中的数是非递减的.最要求deque中最长能是多 ...
- 动态规划:最长上升子序列(二分算法 nlogn)
解题心得: 1.在数据量比较大的时候n^2会明显超时,所以可以使用nlogn 的算法,此算法少了双重循环,用的lower_bound(二分法). 2.lis中的数字并没有意义,仅仅是找到最小点lis[ ...
- HDU 1159 Common Subsequence (动态规划、最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1243 反恐训练营 (动态规划求最长公共子序列)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 动态规划:最长上升子序列(LIS)
转载请注明原文地址:http://www.cnblogs.com/GodA/p/5180560.html 学习动态规划问题(DP问题)中,其中有一个知识点叫最长上升子序列(longest incre ...
- 动态规划初步--最长上升子序列(LIS)
一.问题 有一个长为n的数列 a0,a1,a2...,an-1a.请求出这个序列中最长的上升子序列的长度和对应的子序列.上升子序列指的是对任意的i < j都满足ai < aj的子序列. 二 ...
- 【动态规划】最长上升子序列(LIS)
今天看了<挑战程序设计竞赛>的动态规划部分,感觉对以前一些知其然却不知其所以然的问题有了更好的理解,先整理一部分. 题意: 有一个长为n的数列a0,a1,a2,...,an .请求出这个序 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
随机推荐
- GridView 无数据时,绑定提示
private void BindData() { DataTable dt = DAO.RunSQLReturnDt(this.getsql()); int dtcount = dt.Rows.Co ...
- Android OpenGL ES 3.0 纹理应用
本文主要演示OpenGL ES 3.0 纹理演示.接口大部分和2.0没什么区别,脚本稍微有了点变化而已. 扩展GLSurfaceView package com.example.gles300; im ...
- SlidingMenu侧换菜单的导入
对于Adt-22.3有一种使用SlidingMenu(侧滑菜单的方式),直接加你放到lib文件夹下
- cmd运行java程序,无黑框闪烁
程序目录中创建 “启动.bat” @echo off set mypath="%~dp0myjar.jar" echo %mypath% start javaw -jar %myp ...
- Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证
1.新建Controller public ActionResult Index() { return View(); } public ActionResult Person(int? id) { ...
- linux 命令学习(4)
Linux中常用的关机和重新启动命令有shutdown.halt.reboot以及init,它们都可以达到关机和重新启动的目的,但是每个命令的内部工作过程是不同的,下面将逐一进行介绍. 1. shut ...
- ReactiveCocoa入门教程——第一部分
ReactiveCocoa iOS 翻译 2015-01-22 02:33:37 11471 6 15 本文翻译自RayWenderlich ReactiveCocoa ...
- Idea中运行Testng时,报SAXParseException:parallel为none的问题原因及解决
今天更新了testng的版本为6.9.10, 在idea中运行测试案例时,报错如下: org.testng.TestNGException: org.xml.sax.SAXParseException ...
- php之购物车类思路及代码
<?php /* 购物车类 1.整站范围内,购物车--全局有效 解决:把购物车的信息,放在session里 2.既然全局有效,购物车的实例只有一个 解决:单例模式 技术选型:session+单例 ...
- python str + int
TypeError: cannot concatenate 'str' and 'int' objects 1. print 'Is your secret number " + str(p ...