题意:

  给出一个长度为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的更多相关文章

  1. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  2. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  4. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  5. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  6. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  8. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  9. 300. Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...

随机推荐

  1. egg- 配置

    1. model module.exports = app => { const { INTEGER, STRING, TEXT } = app.Sequelize; const User = ...

  2. HTTP协议中request报文请求方法和状态响应码

    一个HTTP请求报文由4部分组成: 请求行(request line) 请求头部(header) 空行 请求数据 下图给出了请求报文的一般格式: 请求行中包括了请求方法,常见的请求方法有: GET:从 ...

  3. python--re(匹配字符串)

    \d 匹配任何十进制数:它相当于类 [0-9]. \D 匹配任何非数字字符:它相当于类 [^0-9]. \s 匹配任何空白字符:它相当于类 [ fv]. \S 匹配任何非空白字符:它相当于类 [^ f ...

  4. c#常用数据结构解析【转载】

    引用:http://blog.csdn.net/suifcd/article/details/42869341 前言:可能去过小匹夫博客的盆油们读过这篇对于数据结构的总结,但是小匹夫当时写那篇文章的时 ...

  5. Codeforces Round #428 (Div. 2) C. dfs

    C. Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  6. python——int()函数

    1. 使用 int() 将小数转换为整数,结果是向上取整还是向下取整呢? 小数取整会采用比较暴力的截断方式,即向下取整.(注:5.5向上取整为6,向下取整为5) 2. 我们人类思维是习惯于“四舍五入” ...

  7. 7,vim

    vim与程序员 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体 ...

  8. 解决Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0

    解决办法是你需要将工程根目录build.gradle中的 classpath 'com.android.tools.build:gradle:2.2.0' 更改成 classpath 'com.and ...

  9. 如何让button保持点击状态

    http://blog.csdn.net/u010957508/article/details/38385207 他的原理就是在代码里面: view.setSelected(true); 而其他的: ...

  10. Column 'sort' specified twice错误

    我使用的是mybatis框架出现的这个问题,如果你们也出现了这个问题的豪华,我想你们的sql代码一定是复制的吧,额哈哈哈