SWUN OJ 1749(DP + 线段树)
来源:互联网 发布:dota2简单英雄知乎 编辑:程序博客网 时间:2024/06/14 10:27
SWUN 1749
题目链接
思路:lis一样的状态转移方程,不过要利用线段树去维护,每次更新到i,相应的维护i - d之后的区间的最大值,不断转移即可
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define lson(x) ((x<<1)+1)#define rson(x) ((x<<1)+2)const int N = 100005;int n, d, A[N];struct Node { int l, r, v;} node[N * 4];void pushup(int x) { node[x].v = max(node[lson(x)].v, node[rson(x)].v);}void build(int l, int r, int x = 0) { int mid = (l + r) / 2; node[x].l = l; node[x].r = r; node[x].v = 0; if (l == r) {return; } build(l, mid, lson(x)); build(mid + 1, r, rson(x)); pushup(x);}void add(int v, int val, int x = 0) { if (node[x].l == node[x].r) {node[x].v = max(node[x].v, val);return; } int mid = (node[x].l + node[x].r) / 2; if (v <= mid) add(v, val, lson(x)); else add(v, val, rson(x)); pushup(x);}int query(int l, int r, int x = 0) { if (r < l) return 0; if (node[x].l >= l && node[x].r <= r)return node[x].v; int mid = (node[x].l + node[x].r) / 2; int ans = 0; if (l <= mid) ans = max(ans, query(l, r, lson(x))); if (r > mid) ans = max(ans, query(l, r, rson(x))); return ans;}int dp[N];int main() { while (~scanf("%d%d", &n, &d)) {build(0, 100000);int ans = 0;for (int i = 1; i <= d; i++) { scanf("%d", &A[i]); dp[i] = 1;}for (int i = d + 1; i <= n; i++) { scanf("%d", &A[i]); add(A[i - d - 1], dp[i - d - 1]); dp[i] = query(1, A[i] - 1) + 1; ans = max(dp[i], ans);}printf("%d\n", ans); } return 0;}
0 0
- SWUN OJ 1749(DP + 线段树)
- Light OJ 1084 Winter (线段树+DP)
- Light OJ 1360 Skyscraper (线段树+DP)
- 九度OJ 1407(线段树) 1408(DP) 1409(DP) 1410(DP) 1411(最短路)
- Light OJ 1084 线段树+dp or(单调队列+dp) 水题
- 玲珑学院OJ 1097 萌萌哒的第二题【dp+线段树】
- 玲珑学院OJ 1103 萌萌哒的第八题【dp+线段树】
- ZOJ3381 线段树+DP
- hdu3450(线段树+DP)
- poj2750 线段树+dp
- ZOJ3632 线段树+DP
- 线段树+DP+hdu3016
- hdu4521 线段树+dp
- HDU3564 线段树+dp
- xtu oj 1238 线段树
- poj 3171 线段树 + dp
- HDU 3607 线段树+DP
- hdu3016 线段树+简单DP
- CI 系统搭建:五. GitLab、Gerrit、Jenkins 三者整合
- 【LeetCode】Substring with Concatenation of All Words 解题报告
- SQL FIRST() 函数
- OpenStack RBD 上传镜像非常慢
- 关于ISO-8859-1 的相关知识
- SWUN OJ 1749(DP + 线段树)
- MySQL查询优化之profile
- C函数调用与堆栈的变化
- Android--通知之Notification
- 阿里云观察2014
- C语言那点事——如何从零学好C语言?
- Android内核详解之Low memory killer
- hdu 5120 Intersection
- 数据结构 第四章 思维导图