湘潭邀请赛 2018 I Longest Increasing Subsequence
题意:
给出一个长度为n的序列,序列中包含0。定义f(i)为把所有0变成i之后的Lis长度,求∑ni=1i⋅f(i)。
题解:
设不考虑0的Lis长度为L,那么对于每个f(i),值为L或L+1。
预处理f[j],g[j]代表在第j个数结束和从第j个数开始的Lis长度。
对于(1~n)的每个j,找到一个最大的a[k](k>j且a[k]>a[j]),使得g[j]+f[k] = L且j和k之间存在0。那么(a[j],a[k])区间内的数的f()值即为L+1。
从后面往前扫,对于当前点j,vis[L-a[j]]即为最大的a[k]。每到一个0就更新上一个0到这个0的vis信息,保证了j和k之间一定存在着0。若vis[L-a[j]]>a[j],则用差分的形式对(a[j],vis[L-a[j]])区间进行+1。
最后维护下前缀和判断f()的值。还有两种是0 2 和 2 0 这种前缀0和后缀0的情况要特判一下。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+;
const int inf = 0x3f3f3f3f;
int n, pos;
int L;
int a[N];
int f[N], g[N], vis[N];
ll sum[N];
ll ans, cnt;
int main() {
while(~scanf("%d", &n)) {
L = ;
ans = cnt = ;
for(int i = ; i <= n; i++) vis[i] = inf;
for(int i = ; i <= n; i++) sum[i] = ;
for(int i = ; i <= n; i++) scanf("%d", &a[i]), cnt+=a[i];
if(!cnt) {
printf("%lld\n", 1ll*n*(n+)/);
continue;
}
for(int i = ; i <= n; i++) {
if(!a[i]) continue;
int p = lower_bound(vis+, vis+n+, a[i])-vis;
L = max(L, p);
vis[p] = a[i];
f[i] = p;
}
for(int i = ; i <= n; i++) vis[i] = inf;
for(int i = n; i >= ; i--) {
if(!a[i]) continue;
int p = lower_bound(vis+, vis+n+, -a[i])-vis;
vis[p] = -a[i];
g[i] = p;
}
for(int i = ; i <= n; i++) vis[i] = ;
pos = n+;
for(int i = n; i >= ; i--) {
if(a[i]&&pos!=n+) {
if(f[i]==L) sum[a[i]+]++;
else if(vis[L-f[i]]>a[i]+) sum[a[i]+]++, sum[vis[L-f[i]]]--;
}
else if(!a[i]) {
for(int j = pos-; j > i; j--) vis[g[j]] = max(vis[g[j]], a[j]);
pos = i;
}
}
int pos = ;
for(int i = ; i <= n; i++) {
if(pos&&a[i]&&g[i]==L) sum[]++, sum[a[i]]--;
if(!a[i]) pos++;
}
for(int i = ; i <= n; i++) sum[i] += sum[i-];
for(int i = ; i <= n; i++) {
if(sum[i]) ans += 1ll*i*(L+);
else ans += 1ll*i*L;
}
printf("%lld\n", ans);
}
}
湘潭邀请赛 2018 I Longest Increasing Subsequence的更多相关文章
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
随机推荐
- class 类 this指向的问题
ES6 实现了类的概念 class Prosen { } ES5使用函数模拟 function Prosen() { } ES6中的 class定义一个类, 其内部包含 constructor构造函数 ...
- jquery 筛选元素(1)
.eq() 减少匹配元素的集合为指定的索引的那一个元素. .eq(index) index一个整数,指示元素的位置,以0为基数. $("li").eq(2).css('backgr ...
- tree树形
/** * tree * @param menuBeans * @param pid * @return */ public JSON makeTree(List<MenuBean& ...
- WebSeal单点登陆
WebSeal单点登陆 作为学习整理,部分内容来自网络和官方文档. LDAP LDAP可以看作一种数据库,分为客户端和服务端.服务端是用来存放资源,客户端用来操作资源.它是一种树形存储结构,遍历起来会 ...
- springMVC-数据绑定
定义: 将http请求中参数绑定到Handler业务方法 常用数据绑定类型 1. 基本数据类型 不能为其它类型和null值 2. 包装类 可以为其它对象,全部转成null值 3. 数组 多个对象 ...
- 基于Xtrabackup恢复单个innodb表
Preface We all know that Xtrabackup is a backup tool of percona for innodb or Xtradb.It's us ...
- GNU 关闭 MMU 和 Icache 和 Dcache
1. cp15 寄存器 disable Icache 和 Dcache . disable_MMU: MCR p15,0,r0,c7,c7,0 MRC p15,0,r0,c1,c0,0 bic r ...
- 【Effective C++ 读书笔记】条款04:确定对象使用前已先被初始化
永远在使用对象之前先将它初始化.对于无任何成员的内置类型,你必须手工完成此事. 至于内置类型以外的任何其他东西,初始化责任落在构造函数身上.规则很简单:确保每一个构造函数都将对象的每一个成员初始化. ...
- jpeglib的使用
1. 解压jpeglib tar xvzf libjpeg-turbo-1.2.1.tar.gz 2. 阅读里面的说明文件,得到jpeg解压缩的一般步骤: /*Allocate and initial ...
- maven打包成jar
maven pom.xml中添加依赖 <build> <plugins> <plugin> <groupId>org.apache.maven.plug ...