湘潭邀请赛 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 ...
随机推荐
- vue2.0之监听属性的使用心得及搭配计算属性的使用
我们在工作中常常需要监听某一个属性值的变化,这个时候我们就需要用到了监听属性watch,在这里我总结watch属性的三种场景使用希望对你有所帮助: 1.基础版监听: 场景如下:输入框输入你的年龄,如果 ...
- js jquery 权限单选 bug修改以及正确代码 购物车数量加减
效果图废话不多直接上代码 用的avalon渲染,其实都是一样的 <div class="shop-arithmetic"> <a href="javas ...
- centos6 下查看SELinux状态 关闭SELinux
转载自:https://blog.csdn.net/boomjane_testingblog/article/details/52859977 SELinux(Security-Enhanced Li ...
- Spring Cloud 入门 Consul-Client服务提供
前面介绍了 Rureka Client服务提供, 只需要改pom.xml部分内容 1.pom.xml <?xml version="1.0" encoding="U ...
- Python入门必知的几个点
Python是Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言.全世界差不多有600多种编程语言,但流行的编程语言也就那么20来种.如果你听说过TIOB ...
- caioj:1093: 并查集2(scy的删边问题) C++
题目描述 [题目描述] 读入一个无向图(可能含有多个连通分支),输出最多能删掉多少条边,而不改变这个图任意两点的连通性(原来连通的两个点依然连通,不连通的依然不连通). [输入格式] 第一行为图的顶点 ...
- 笔记-爬虫-XPATH
笔记-爬虫-XPATH 1. xpath XPath是W3C的一个标准.它最主要的目的是为了在XML1.0或XML1.1文档节点树中定位节点所设计.目前有XPath1.0和XPath2.0两 ...
- 5-1 练习css 总结
1. 边框 border:3px dotted; border: 2px solid yellow; 背景颜色 background-color: red; 外攘 margin:20px 0 20px ...
- Java的接口和抽象类深入理解
对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者确实有很多相似的地方,看了一整天别人怎么说,大致总结如下: 一.抽象类 在了解抽象类 ...
- USACO Section1.2 Dual Palindromes 解题报告
dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...