Bzoj 1564 [NOI2009] binary search tree (tree dp)

Source: Internet
Author: User

"Topic link"

http://www.lydsy.com/JudgeOnline/problem.php?id=1564

Test instructions

Given a treap, the total cost is the sum of the depth * distance. You can modify the weights at the cost of K, asking the minimum price.

Ideas

The data values are constant, so the treap traversal is unique. First, the data is sorted by data value, and the sequential traversal is obtained.

The weights are then discretized into the [1,n] range.

Set F[L][R][W] for the interval [l,r] The weight of the value is greater than w when the minimum cost, there is a transfer type:

f[l][r][w]=min {f[l][k-1][w]+f[k+1][r][w]+k+c (l,r)},a[k].w>=w

f[l][r][w]=min {f[l][k-1][a[k].w]+f[k+1][r][a[k].w]+c (l,r)}

Memory Search Better Write Lalala

Code

1#include <Set>2#include <cmath>3#include <queue>4#include <vector>5#include <cstdio>6#include <cstring>7#include <iostream>8#include <algorithm>9 #defineTrav (u,i) for (int i=front[u];i;i=e[i].nxt)Ten #definefor (A,B,C) for (int a= (b); a<= (c); a++) One using namespacestd; A  -typedefLong Longll; - Const intN = the; the Const intINF =1e9; -  - structNode - { +     intdat,w,c; -     BOOL operator< (Constnode& RHS)Const  +     { A         returndat<Rhs.dat; at     } - } A[n]; -  - intN,k,sumc[n],f[n][n][n]; -pair<int,int>B[n]; -  in intdpintLintRintW) - { to     int& ans=F[l][r][w]; +     if(L>r)returnans=0; -     if(ans!=-1)returnans; theans=inf; * For (k,l,r) { $         if(a[k].w>=W)Panax NotoginsengAns=min (ANS,DP (l,k-1, A[K].W) +DP (k +1, R,A[K].W) +sumc[r]-sumc[l-1]); -Ans=min (ANS,DP (l,k-1, W) +DP (k +1, r,w) +sumc[r]-sumc[l-1]+K); the     } +     returnans; A } the  + intMain () - { $ //freopen ("in.in", "R", stdin); $ //freopen ("Out.out", "w", stdout); -memset (f,-1,sizeof(f)); -scanf"%d%d",&n,&K); thefor (I,1, N) scanf ("%d",&a[i].dat); -for (I,1, N) scanf ("%d",&A[I].W);Wuyifor (I,1, N) scanf ("%d",&a[i].c); theSort (A +1, a+n+1); -for (I,1, N) Wub[i]=Make_pair (a[i].w,i); -Sort (b +1, b+n+1); Aboutfor (I,1, N) $a[b[i].second].w=I, -sumc[i]=sumc[i-1]+a[i].c; -      -     intans=inf; Afor (I,1, N) +Ans=min (ANS,DP (1, N,i)); theprintf"%d", ans); -     return 0; $}

Bzoj 1564 [NOI2009] binary search tree (tree dp)

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.