51nod 1218 最长递增子序列 V2(dp + 思维)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218
题解:先要确定这些点是不是属于最长递增序列然后再确定这些数在最长递增序列中出现的次数,如果大于1次显然是可能出现只出现1次肯定是必然出现。那么就是怎么判断是不是属于最长递增序列,这个只要顺着求一下最长递增标一下该点属于长度几然后再逆着求一下最长递减标一下该点属于长度几如果两个下标之和等于最长长度+1那么该点就属于最长递增序列,然后就是求1~len(len表示最长的长度)中各个长度出现的次数就行。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 5e4 + 10;
int a[M] , b[M] , dpa[M] , dpb[M] , vis[M];
bool vs[M];
int binsearch(int l , int r , int num) {
int mid = (l + r) >> 1;
int ans = 0;
while(l <= r) {
mid = (l + r) >> 1;
if(b[mid] > num) r = mid - 1;
else {
ans = mid;
l = mid + 1;
}
}
return ans;
}
int binsearch2(int l , int r , int num) {
int mid = (l + r) >> 1;
int ans = 0;
while(l <= r) {
mid = (l + r) >> 1;
if(b[mid] < num) r = mid - 1;
else {
ans = mid;
l = mid + 1;
}
}
return ans;
}
int main() {
int n;
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i++) scanf("%d" , &a[i]);
int len = 0;
b[0] = -1;
for(int i = 1 ; i <= n ; i++) {
if(a[i] > b[len]) {
len++;
b[len] = a[i];
dpa[i] = len;
continue;
}
else {
int pos = binsearch(1 , len , a[i]);
b[pos + 1] = min(b[pos + 1] , a[i]);
dpa[i] = pos + 1;
}
}
int len2 = 0;
memset(b , inf , sizeof(b));
for(int i = n ; i >= 1 ; i--) {
if(a[i] < b[len2]) {
len2++;
b[len2] = a[i];
dpb[i] = len2;
}
else {
int pos = binsearch2(1 , len2 , a[i]);
b[pos + 1] = max(b[pos + 1] , a[i]);
dpb[i] = pos + 1;
}
}
memset(vs , false , sizeof(vs));
for(int i = 1 ; i <= n ; i++) {
if(dpa[i] + dpb[i] == len + 1) {
vis[dpa[i]]++;
vs[i] = true;
}
}
printf("A:");
for(int i = 1 ; i <= n ; i++) {
if(vis[dpa[i]] > 1 && vs[i]) printf("%d " , i);
}
printf("\n");
printf("B:");
for(int i = 1 ; i <= n ; i++) {
if(vis[dpa[i]] == 1 && vs[i]) printf("%d " , i);
}
printf("\n");
return 0;
}
51nod 1218 最长递增子序列 V2(dp + 思维)的更多相关文章
- [51Nod 1218] 最长递增子序列 V2  (LIS)
		
传送门 Description 数组A包含N个整数.设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可 ...
 - 51nod 1218 最长递增子序列 V2——LIS+思路(套路)
		
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 自己怎么连这种 喜闻乐见的大水题 都做不出来了…… 好像见过 ...
 - [51Nod] 1218 最长递增子序列 V2
		
如何判断一个元素是否一定在LIS中?设f[i]为以ai结尾的LIS长度,g[i]为以ai开头的LIS长度,若f[i]+g[i]-1==总LIS,那么i就一定在LIS中出现 显然只出现一次的元素一定是必 ...
 - 51nod 1218 最长递增子序列 | 思维题
		
51nod 1218 最长递增子序列 题面 给出一个序列,求哪些元素可能在某条最长上升子序列中,哪些元素一定在所有最长上升子序列中. 题解 YJY大嫂教导我们,如果以一个元素结尾的LIS长度 + 以它 ...
 - 51nod 1134 最长递增子序列
		
题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...
 - 51nod 1376 最长递增子序列的数量(线段树)
		
51nod 1376 最长递增子序列的数量 数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递 ...
 - LCS 51Nod 1134 最长递增子序列
		
给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行:1个 ...
 - 51NOD 1376 最长递增子序列的数量 [CDQ分治]
		
1376 最长递增子序列的数量 首先可以用线段树优化$DP$做,转移时取$0...a[i]$的最大$f$值 但我要练习$CDQ$ $LIS$是二维偏序问题,偏序关系是$i<j,\ a_i< ...
 - 51Nod 1376 最长递增子序列的数量 —— LIS、线段树
		
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1376 1376 最长递增子序列的数量 基准时间限制:1 秒 空 ...
 
随机推荐
- unity3d立方体碰撞检测(c#代码实现)
			
由于unity自带的碰撞组件特别耗费性能,网上的unity物体碰撞的c#代码实现比较少,没有适合的,只能自己写一个来用: using System; using System.Collections. ...
 - 洛谷P1510 题解
			
前言: 其实这道题挺水的,但我居然把ta想成了 贪心 啪啪打脸 好了,废话不多说. 思路: step 1:先翻译以下题意,其实就是求出最多消耗多少体力能把东海填满,如果不能填满,就输出"Im ...
 - 【Java例题】1.2计算n的m次方
			
package study; import java.util.*; import java.math.*; public class study { public static void main( ...
 - Spark 系列(三)—— 弹性式数据集RDDs
			
一.RDD简介 RDD 全称为 Resilient Distributed Datasets,是 Spark 最基本的数据抽象,它是只读的.分区记录的集合,支持并行操作,可以由外部数据集或其他 RDD ...
 - AOSP 预置 APP
			
Android 系统预置 APP 是做 Framework 应用开发经常经常会遇到的工作,预置 APP 分为两种,一种是直接预置 APK,一种是预置带有源码的 APP. 预置 apk 示例说明 以 . ...
 - Go类型别名与类型定义区别
			
类型别名和自定义类型区别 自定义类型 //自定义类型是定义了一个全新的类型 //将MyInt定义为int类型 type MyInt int 类型别名 //类型别名规定:TypeAlias只是Type的 ...
 - Python爬虫,爬取腾讯漫画实战
			
先上个爬取的结果图 最后的结果为每部漫画按章节保存 运行环境 IDE VS2019 Python3.7 先上代码,代码非常简短,包含空行也才50行,多亏了python强大的库 import os im ...
 - [luogu4886] 快递员(点分治,树链剖分,lca)
			
dwq推的火题啊. 这题应该不算是点分治,但是用的点分治的思想. 每次找重心,算出每一对询问的答案找到答案最大值,考虑移动答案点,使得最大值减小. 由于这些点一定不能在u的两颗不同的子树里,否则你怎么 ...
 - 简析 Golang net/http 包
			
net/http 包涵盖了与 HTTP 请求发送和处理的相关代码.虽然包中定义了大量类型.函数,但最重要.最基础的概念只有两个:ServeMux 和 Handler. ServeMux 是 HTTP ...
 - 【在 Nervos CKB 上做开发】Nervos CKB脚本编程简介[2]:脚本基础
			
CKB脚本编程简介[2]:脚本基础 原文作者:Xuejie 原文链接:Introduction to CKB Script Programming 2: Script 本文译者:Shooter,Jas ...