"bzoj1109" [POI2007] stacked wood klo dynamic planning + Tree Array

Source: Internet
Author: User

Title Description

Mary has some bricks in her birthday present. The bricks are all cubes of the same size. Each block has a number above it. Mary used all his bricks to base a tall tower. Mom told Mary that the purpose of the game was to build a tower so that the most bricks were in the right place. The correct position of a building block with the number I above is the tower from the bottom to the first number I position. Mary decided to remove some of the existing towers so that the most bricks were in the right place. Please tell Mary which bricks she should remove.

Input

The first behavior is a number n, which indicates the initial height of the tower. The second line contains n number a1,a2,..., An, which represents the number of blocks above each block.

Output

Note: Please output up to how many points can be in the correct position

Sample input

5
1 1 2) 5 4

Sample output

3

Exercises

Dynamic planning + tree-like arrays

Set F[i] Indicates the number of points in the first I block selected I are in the correct position.

Then if J can update I, the condition needs to be met: $j <i\ \&\&\ a_j<a_i\ \&\&\ a_i-a_j\le i-j (J-a_j\le i-a_i) $.

This appears to be a three-dimensional partial order problem with three conditions, but in fact the first condition can be introduced by the latter two conditions, which can be ignored and become a two-dimensional partial order problem.

Then you can sort by a value from small to large, and it becomes the longest non-descending sub-sequence problem. Can be maintained using a tree-like array.

Note: Because $a_j$ are required to be strictly less than $a_i$, they should not be updated when they are equal. Here in order to avoid this problem, I will $a_i$ equal I according to $i-a_i$ from large to small sort, prevent redundant updates.

#include <cstdio> #include <cstring> #include <algorithm> #define N 100010using namespace Std;struct Data{int x, y;} A[n];int V[n], tot, f[n], dp[n], N;bool cmp1 (data A, data b) {return a.y < b.y;} BOOL CMP2 (data A, data b) {return a.x = = b.x? a.y > B.y:a.x < b.x;} void update (int x, int a) {int i;for (i = x; i <= tot; i + = i & i) f[i] = max (F[i], a);} int query (int x) {int I, ans = 0x80000000;for (i = x; i; i-= i & i) ans = max (ans, f[i]); return ans;} int main () {int I, ans = 0;scanf ("%d", &n), n + +, a[1].y = 1;for (i = 2; I <= n; i + +) scanf ("%d", &a[i]. x), a[i].y = I-a[i].x;sort (A + 1, a + n + 1, CMP1), v[0] = 0x80000000;for (i = 1; I <= n; i + +) {if (A[i].y! = v[ TOT]) V[++tot] = A[i].y;a[i].y = tot;} Sort (A + 1, a + n + 1, CMP2), memset (f, 0x80, sizeof (f)), update (A[1].Y, 0), for (i = 2; I <= n; i + +) dp[i] = qu Ery (A[I].Y) + 1, update (A[I].Y, dp[i]), ans = max (ans, dp[i]);p rintf ("%d\n", ans); return0;} 

"bzoj1109" [POI2007] stacked wood klo dynamic planning + Tree Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.