洛谷$P4322\ [JSOI2016]$最佳团体 二分+$dp$

正解:二分+$dp$

解题报告:

传送门$QwQ$

这题长得好套路嗷,,,就一看就看出来是个$01$分数规划+树形$dp$嘛$QwQ$.

考虑现在二分的值为$mid$,若$mid\leq as$,则有$\frac{\sum p_i}{\sum s_i}\geq mid,\sum p_i-mid\cdot \sum s_i\geq 0$.

于是就把每个点的点权改为$mid\cdot s-p$.现在变成要选$K$个节点使得点权之和取$max$.

于是就树形$dp$呗?设$f_{i,j}$表示点$i$的子树中选了$j$个点的$max$,转移的时候强制点$i$必须要选就成.

$over$.

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define lf double
#define gc getchar()
#define t(i) edge[i].to
#define ri register int
#define rc register char
#define rb register bool
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)
#define e(i,x) for(ri i=head[x];i;i=edge[i].nxt) const int N=+,inf=1e9;
const lf eps=1e-;
int n,K,sz[N];
lf f[N][N],q[N],tmp[N];
vector<int>V[N];
struct node{int s,p;}nod[N]; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
il void merg(ri x,ri y)
{
rp(i,,K)tmp[i]=-inf;
rp(i,,sz[x])rp(j,,min(K-i,sz[y]))tmp[i+j]=max(f[x][i]+f[y][j],tmp[i+j]);
rp(i,,K)f[x][i]=max(f[x][i],tmp[i]);
}
void dfs(ri nw){ri siz=V[nw].size();sz[nw]=;rp(i,,siz-)dfs(V[nw][i]),merg(nw,V[nw][i]),sz[nw]+=sz[V[nw][i]];}
il bool check(lf dat){memset(f,-,sizeof(f));rp(i,,n)f[i][]=nod[i].p-dat*nod[i].s;dfs();return f[][K]>=;} int main()
{
freopen("4322.in","r",stdin);freopen("4322.out","w",stdout);
K=read()+;n=read();rp(i,,n){nod[i]=(node){read(),read()};V[read()].push_back(i);}
lf l=,r=;while(r-l>=eps){lf mid=(l+r)/;if(check(mid))l=mid;else r=mid;}printf("%.3lf\n",r);
return ;
}
上一篇:[JSOI2016]最佳团体 DFS序/树形DP


下一篇:Codeforces Round #378 (Div. 2) A B C D 施工中