Deep plum tree in winter but not greedy

  Tree greedy title less natural to record some of the greatest fear is that dp not think equation have to be greedy greedy could not think of strategies have to dp.

LINK: Mei gone deep winter  Fusu, the most like him before. Now not cowardly people do not deserve.

Topic mean take some time to understand not repeat them here. How to do it?

For apparently no matter what point do have to do first father son, topology? Such a tree can be convenient but also can solve this problem.

The key dfs first son and then the cost of the statistical problem which is the first son of statistics? Obviously there is a Wx two properties within their own expense the cost of ANSx sub-tree if you want to put a plum in this point requires at least one plum after ANSx done and left ANSx-Wx a plum for each son and then lay down for a the next rest is left of the current number of clubs so if rest <ANSx then still need to bring ANSx-rest and then put a plum plum flower Wx at x. Concrete is such a process we find different ANSx son and son Wx different place which the order is the key to the explosive search order is clearly not AC ...

This question is very specific dp difficult because we are entangled in this order of things and not to select the optimal son transferred or something (although the substance or transfer order but the key is to develop a better order so that the answer can be considered greedy. Consider o observed item exchange method (perturbation method).

O exchange items here to demonstrate. This method is usually right.

Consider a and b two sons have contributed target of the answer makes f [x] as small as possible.

Do first do a b ANSa + max (0, ANSb- (ANSa-Wa)) Note that here we do not Wx itself included wherein as to the order itself and is unrelated to the son.

B do first do a ANSb + max (0.ANSa- (ANSb-Wb)) 

Listed under the formula than the size of it (note that under certain premise as Wa> Wb what in some premise of a better formula than the other formulas, then we get the strategy.

Here we set Wa> Wb found it did not use, do not push it at this time what the size of the relationship, there are two solutions:

1 guess a conclusion to continue to push off 2 directly to the size of the series pushed out conclusion.

2 obviously better to do but I have just been doing a little bit stupid ... I ... yes

Consider a formula <= diethylene do first formula that is a ratio of b do do first do a better b.

Simplification can not be found or what? This is not so bold ... to dismantle max. Max open a formula ANSa + 0 or ANSa + ANSb-ANSa + Wa ...

Found becomes max (ANSa, ANSb + Wa); Similarly two formula = max (ANSb, ANSa + Wb); found still ... max

Then split four cases ANSa <= ANSb ANSb + Wa <= ANSb ANSa <= ANSa + Wb ANSb + Wa <= ANSa + Wb

Among these four cases due to the array W is positive so the third can ignore the first two due to the mandatory us <= number of so this can not happen.

Finishing ANSa <= ANSb ANSb-Wb <= ANSa-Wa observe first formula is bound to want to get ANSa ANSa> = ANSb + Wa available at this time ANS <= ANSb but the array W is positive therefore inequality does not hold.

So finally we get a formula ANSb-Wb <= ANS-Wa this particular thing to do first the case where a ratio b preferably do first the ANSa-Wa> = ANSb-Wb.

Therefore, the use of mathematical induction can be drawn for all sons sequence formed according to this ordering may be such things ANSx optimal. Wx their range is the same for each case it is not considered treatment.

code:

//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<cctype>
#include<utility>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<deque>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<stack>
#include<string>
#include<cstring>
#define INF 2147483647
#define ll long long
#define db double
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define l(p) t[p].l
#define r(p) t[p].r
#define sum(p) t[p].sum
#define mx(p) t[p].mx
#define add(p) t[p].add
#define tag(p) t[p].tag
#define zz p<<1
#define yy p<<1|1
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
    int x=0,f=1;char ch=getc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getc();}
    while(ch>='0 ' && CH <= ' . 9 ' ) {X = X * 10 + CH-, ' 0 ' ; CH = getc ();}
     return X * F; 
} 
const  int MAXN = 100010 ;
 int n-;
 int F [MAXN]; // F [x] x represents the required minimum number of complete coverage plum 
int FA [MAXN], W [MAXN]; 
Vector < int > G [MAXN]; 
inline int CMP ( int x, int Y) { return F [x ] -w [X]> F [Y] - W [Y];} 
inline void dp(int x)
{
    int sz=g[x].size();
    if(!sz){f[x]=w[x];return;}
    for(int i=0;i<sz;++i)
    {
        int tn=g[x][i];
        dp(tn);
    }
    sort(g[x].begin(),g[x].end(),cmp);
    int rest=f[g[x][0]]-w[g[x][0]];
    int an=f[g[x][0]];
    for(int i=1;i<sz;++i)
    {
        int tn=g[x][i];
        if(rest<f[tn])an+=f[tn]-rest,rest=f[tn];
        rest-=w[tn];
    }
    f[x]=an+max(w[x]-rest,0);
}
int main()
{
    //freopen("1.in","r",stdin);
    n=read();
    for(int i=1;i<n;++i)
    {
        fa[i+1]=read();
        g[fa[i+1]].push_back(i+1);
    }
    for(int i=1;i<=n;++i)w[i]=read();
    dp(1);
    for(int i=1;i<=n;++i)printf("%d ",f[i]);
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/chdy/p/11446006.html