计蒜客 The Heaviest Non-decreasing Subsequence Problem dp LIS变形 || 线段树+dp
来源:互联网 发布:手机实用软件大全 编辑:程序博客网 时间:2024/06/03 07:22
题意:每个数有一个val和weight,如果val < 0 weight 为 0 , val >= 1e4 weight 为5 ,其余为1.让你求一哥序列使得为weight最大并且val是不下降的.
心得:悲惨背锅。。。不知道怎么该LIS的nlogn做法。。
结果看了挺久。。
最后队友想的方法大概是
其实这个题仔细想一下就是把价值为0的去掉,然后把价值为5的分成5个1的放进去,然后求一次LIS,得到的LIS长度就是最大值了.
PS:如果价值不是5而是很大的数这个方法就不可以了.
看了别人的 ,这才知道怎么用LIS的线段树写法,这样的话可以保证在val为很大的数 的时候也可以。。。
参考http://blog.csdn.net/howardemily/article/details/78078682
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 3e5+5; int a[maxn], v[maxn], val[maxn], Hash[maxn], id[maxn]; int dp[maxn], treeMax[maxn*4], Max; void update(int root, int l, int r, int pos, int val) { if(l == r) { treeMax[root] = max(treeMax[root], val); return ; } int mid = (l+r)/2; if(pos <= mid) update(root*2, l, mid, pos, val); else update(root*2+1, mid+1, r, pos, val); treeMax[root] = max(treeMax[root*2], treeMax[root*2+1]); } void query(int root, int l, int r, int i, int j) { if(i <= l && j >= r) { Max = max(treeMax[root], Max); return ; } int mid = (l+r)/2; if(mid >= i) query(root*2, l, mid, i, j); if(mid < j) query(root*2+1, mid+1, r, i, j); } int main(void) { int x, n = 1; while(~scanf("%d", &a[n])) { v[n] = a[n]; if(a[n] >= 10000) v[n] -= 10000; if(a[n] >= 10000) val[n] = 5; else if(a[n] >= 0) val[n] = 1; else val[n] = 0; Hash[n] = v[n]; n++; } n--; sort(Hash+1, Hash+1+n); int d = unique(Hash+1, Hash+1+n)-Hash-1; for(int i = 1; i <= n; i++) id[i] = lower_bound(Hash+1, Hash+1+d, v[i])-Hash; int ans = 0; for(int i = 1; i <= n; i++) { Max = 0; query(1, 1, n, 1, id[i]); dp[i] = Max+val[i]; ans = max(ans, dp[i]); update(1, 1, n, id[i], dp[i]); } printf("%d\n", ans); return 0; }
阅读全文
0 0
- 计蒜客 The Heaviest Non-decreasing Subsequence Problem dp LIS变形 || 线段树+dp
- 计蒜客 The Heaviest Non-decreasing Subsequence Problem dp LIS变形 || 线段树+dp
- The Heaviest Non-decreasing Subsequence Problem LIS 思维题
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛The Heaviest Non-decreasing Subsequence Problem(线段树优化DP)
- The Heaviest Non-decreasing Subsequence Problem
- The Heaviest Non-decreasing Subsequence Problem
- ???The Heaviest Non-decreasing Subsequence Problem
- The Heaviest Non-decreasing Subsequence Problem
- 计蒜客 17319 The Heaviest Non-decreasing Subsequence Problem
- 2017 icpc 南宁赛区 L.The Heaviest Non-decreasing Subsequence Problem(LIS)
- 最长不减子序列变形 The Heaviest Non-decreasing Subsequence Problem 南宁网络赛
- The Heaviest Non-decreasing Subsequence Problem ACM-ICPC南宁wa
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem (LIS)
- 计蒜客 The Heaviest Non-decreasing Subsequence Problem(最大权值和非递减子序列)
- 计蒜客 2017icpc南宁赛区 The Heaviest Non-decreasing Subsequence Problem 最长不递减子序列
- 计蒜客 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem
- HashMap原理
- markdown编辑器
- DbParameter 类 DBType无RefCursor数据类型
- matlab截取图像一部分
- vue学习总结(1)
- 计蒜客 The Heaviest Non-decreasing Subsequence Problem dp LIS变形 || 线段树+dp
- PHP大小写敏感规则
- java.lang.RuntomeException: lib/arm64-v8a/路径下没有libinet.2.0.1.so
- openstack 通过service 查看每个service的存放log的位置
- QT5文件
- SQL 注入防御方法总结
- Linux的直接I/O机制
- struts2的文件上传和下载
- 一名交互设计师必备的知识架构