湘潭邀请赛 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 ...
随机推荐
- v-for的简单实现
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【复杂度分析】loj#6043. 「雅礼集训 2017 Day7」蛐蛐国的修墙方案
感觉有点假 题目大意 数据范围:$n<=100$ 题目分析 由于题目给出的是 置换,所以相当于只需枚举每个环的两个状态. 主要是复杂度分析这里: 一元环:不存在 二元环:特判保平安 三元环:不存 ...
- Docker学习之镜像操作
使用Docker镜像 以下都是Docker镜像的一系列重要名操作,包括获取.查看.搜索.删除.创建.存出或载入.上传等.可使用docker image help命令查看帮助. 1.获取镜像(pull) ...
- Zookeeper+Kafka的单节点配置
Zookeeper+Kafka的单节点配置 环境描述:Ubuntu16.04 server系统,在系统上搭建Java环境,jdk的版本为1.8或更高,我的服务器IP地址为192.168.0.106. ...
- poj 1611 dsu
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35918 Accepted: 17458 De ...
- Android面试收集录14 Android进程间通信方式
一.使用 Intent Activity,Service,Receiver 都支持在 Intent 中传递 Bundle 数据,而 Bundle 实现了 Parcelable 接口,可以在不同的进程间 ...
- Unity Occlusion Culling 遮挡剔除研究
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52684127 作者:car ...
- SpringMVC---四大注解
SpringMVC四大注解 Component 通用标注,在不清楚使用哪个注解的时候,可以使用Component通用注解 Controller 标注web请求控制器 Service 标注Service ...
- 单例解决存储微信Token信息保留两小时
采用单例模式可以存储初始化数据,比如第一次对/api/test接口进行了访问,传入的信息为“123”,则在两个小时之内返回的信息依然是“123”,无论传入的参数是什么,如果有效时间过了两个小时,比如传 ...
- runtime如何通过selector找到对应的IMP地址?(分别考虑类方法和实例方法)
每一个类对象中都一个对象方法列表(对象方法缓存) 类方法列表是存放在类对象中isa指针指向的元类对象中(类方法缓存) 方法列表中每个方法结构体中记录着方法的名称,方法实现,以及参数类型,其实selec ...