4753: [Jsoi2016] Best Group
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2003 Solved: 790
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1000 1 0
1 1000 1
Sample Output
01 planning score, simple terms, is that some tuples $ (s_i, p_i) $, select from a number of tuples, so $ \ Sigma {s_i} / \ Sigma {p_i} $ maximum (minimum).
This problem is a general class of solution, we assume $ x = \ Sigma {s_i} / \ Sigma {p_i} $ maximum (minimum) value, then there is $ x * \ Sigma {p_i} = \ Sigma {s_i } $, i.e. $ \ Sigma {s_i} -x * \ Sigma {p_i} = 0 $. That is, when a certain value of x satisfies the above equation, it is the desired value. We can think of enumeration ...... but then again, think about it, this can be half the answer.
So we direct answer half, when the above formula> 0, indicating little answer, <0 then the answer is big, so you can calculate.
Well, the pre-cheese resolved, that in fact this question is the Word 01 scores planning and $ (n ^ 2) $ backpack bare tree title of O, Also to be noted is the initialization problem, particular attention is that each second-half to give the answer must attach an array initial value dp, dp fact, each check is an array that is an array of values dp x. Finally, note that the title because he says he must choose, and not included in the total number of people, so k ++.
The total time complexity $ O (n ^ 2logn) $, often slightly card, to open the luogu O2.
Sahua end.
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #include<vector> 7 #include<queue> 8 #include<cstdlib> 9 using namespace std; 10 const int N=2505; 11 const double eps=1e-5; 12 int first[N],nex[N<<1],to[N<<1],tot; 13 int vis[N],size[N]; 14 int n,k; 15 int att[N],co[N],ri[N]; 16 double TerStegen[N]; 17 double f[N][N]; 18 void add(int a,int b){ to[++tot]=b,nex[tot]=first[a],first[a]=tot;} 19 void dfs(int x){ 20 vis[x]=1;size[x]=1; 21 f[x][1]=TerStegen[x];// 22 for(int i=first[x];i;i=nex[i]){ 23 int y=to[i]; 24 //if(vis[y]) continue;//danxiangbian 25 dfs(y); 26 for(int j=min(size[x],k);j>=1;j--) for(int l=min(size[y],k);l>=1;l--) f[x][l+j]=max(f[x][l+j],f[y][l]+f[x][j]);//dayudengyu1? 27 size[x]+=size[y]; 28 } 29 } 30 int check(double mid){ 31 int ju; 32 for(int i=0;i<=2501;i++) for(int j=0;j<=2501;j++) f[i][j]=-88484848; 33 //memset(f,0xcf,sizeof(f)); 34 for(int i=0;i<=n;i++) f[i][0]=0.0; 35 for(int i=0;i<=n;i++) TerStegen[i]=1.0*att[i]-1.0*mid*1.0*co[i]; 36 dfs(0); 37 //cout<<f[0][k]<<endl; 38 if(f[0][k]>=0) ju=1; 39 else ju=0; 40 return ju; 41 } 42 int main(){ 43 scanf("%d%d",&k,&n); 44 k++;//duliu 45 for(int i=1;i<=n;++i){ 46 scanf("%d%d%d",&co[i],&att[i],&ri[i]); 47 add(ri[i],i); 48 } 49 double l=0.0,r=2e7*1.0; 50 double ans; 51 while(l+eps<r){ 52 double mid=(l+r)/2; 53 //cout<<mid<<" "<<l<<" "<<r<<endl; 54 if(check(mid)) l=mid; 55 else r=mid; 56 } 57 printf("%.3lf",(l+r)/2); 58 }