湘潭邀请赛 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 ...
随机推荐
- eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>
eclipse环境Dynamic web module version 3.1版本之前,Dynamic web object 中Servlet类的配置,要在web.xml 配置<Servlet& ...
- TSMessages,非HUD风格的iOS提示框(附官方demo BUG修复方案)
优势 先看效果 个人觉得这种提示效果用在UITableView上要比HUD优雅美观,而其他情况下的提示,用HUD比较好 源码简介易懂,用起来也很方便 导入 pod导入相对很简单,主要讲怎么手动导入这个 ...
- ant Design表单验证笔记
1.pattern正则验证 <Col md={12} sm={24}> <FormItem {...formItemLayout} label="班数"> ...
- css3 媒体查询的学习。
1.什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向)为其设定CSS样式,媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于 ...
- Oracle 11g R2在 win7 64位的安装流程图解【含常见问题解决方案】
ORACLE数据库系统是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器(CLIENT/SERVER)或B/S体系结构的数据库之一.Oracle 11 ...
- win10鼠标右键菜单在左边,怎么改回右边
键盘上按WIN+R打开运行窗口,输入shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}按回车键
- python基础之正则表达式和re模块
正则表达式 就其本质而言,正则表达式(或 re)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...
- get请求中url传参中文乱码问题
在项目中经常会遇到中文传参数,在后台接收到乱码问题.那么在遇到这种情况下我们应该怎么进行处理让我们传到后台接收到的参数不是乱码是我们想要接收的到的,下面就是我的一些认识和理解. 一:get请求url中 ...
- web项目中获取spring的bean对象
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...
- css一些事儿
1. margin和padding 如果边界画一条线,则margin的属于边界外,padding属于边界内 当我们给元素背景色时,margin区域不会被着色,而padding区域会被着色. 当上下两个 ...