BZOJ4374 : Little Elephant and Boxes

设$f[i][j][k]$表示前$i$个物品买了$j$个,消耗$k$个钻石,最少花多少钱,可以通过简单的DP求出。

枚举拥有的钻石数以及最多能购买的物品数的下界,那么钱数的下界是定值。

将$n$个箱子折半搜索,按钻石数分组并排序,枚举左半边每种方案,在右半边双指针求出总概率即可。

时间复杂度$O(nm2^{\frac{n}{2}}+nm^2)$。

#include<cstdio>
#include<algorithm>
const int N=33,inf=~0U>>2;
int T,n,m,lim,i,j,k,f[N][N][N],ca[N],cb[N];double p[N],ans;
struct P{int x;double p;P(){}P(int _x,double _p){x=_x,p=_p;}}a[N],A[N][33000],B[N][33000];
struct E{int c,d;}b[N];
inline bool cmp(const P&a,const P&b){return a.x<b.x;}
inline void up(int&a,int b){if(a>b)a=b;}
void dfsl(int x,int y,int z,double p){
  if(x==lim){
    A[y][++ca[y]]=P(z,p);
    return;
  }
  dfsl(x+1,y,z+a[x].x,p*a[x].p);
  dfsl(x+1,y+1,z,p*(1.0-a[x].p));
}
void dfsr(int x,int y,int z,double p){
  if(x==n){
    B[y][++cb[y]]=P(z,p);
    return;
  }
  dfsr(x+1,y,z+a[x].x,p*a[x].p);
  dfsr(x+1,y+1,z,p*(1.0-a[x].p));
}
inline double cal(int x,int y,int z){
  if(z>=inf)return 0;
  int n=ca[x],m=cb[y],i;double p=0,ret=0;
  if(!n||!m)return 0;
  for(i=1;i<=n;i++){
    while(m&&A[x][i].x+B[y][m].x>=z)p+=B[y][m--].p;
    ret+=A[x][i].p*p;
  }
  return ret;
}
int main(){
  for(scanf("%d",&T);T--;printf("%.4f\n",ans)){
    scanf("%d%d",&n,&m);lim=n/2;
    for(i=0;i<n;i++)scanf("%d%lf",&a[i].x,&a[i].p),a[i].p*=0.01;
    for(i=1;i<=m;i++)scanf("%d%d",&b[i].c,&b[i].d);
    for(i=0;i<=m;i++)for(j=0;j<=m;j++)for(k=0;k<=n;k++)f[i][j][k]=inf;
    f[0][0][0]=0;
    for(i=1;i<=m;i++)for(j=0;j<=m;j++)for(k=0;k<=n;k++)if(f[i-1][j][k]<inf){
      up(f[i][j][k],f[i-1][j][k]);
      if(k+b[i].d<=n)up(f[i][j+1][k+b[i].d],f[i-1][j][k]+b[i].c);
    }
    for(j=0;j<=m;j++)for(k=1;k<=n;k++)up(f[m][j][k],f[m][j][k-1]);
    for(i=0;i<=n;i++)ca[i]=cb[i]=0;
    dfsl(0,0,0,1);
    dfsr(lim,0,0,1);
    for(i=0;i<=n;i++){
      if(ca[i]>1)std::sort(A[i]+1,A[i]+ca[i]+1,cmp);
      if(cb[i]>1)std::sort(B[i]+1,B[i]+cb[i]+1,cmp);
    }
    ans=p[m+1]=0;
    for(i=0;i<=n;i++)for(j=m;j;j--){
      p[j]=0;
      for(k=0;k<=lim&&k<=i;k++)p[j]+=cal(k,i-k,f[m][j][i]);
      ans+=(p[j]-p[j+1])*j;
    }
  }
  return 0;
}

  

时间: 2025-01-10 03:46:21

BZOJ4374 : Little Elephant and Boxes的相关文章

&lt;Dr.Elephant&gt;

Why Dr.Elephant? Most of Hadoop optimization tools out there, but they are focused on simplifying the deploy and managment of Hadoop clusters. Very few tools are designed to help Hadoop users optimize their flows. Dr.Elephant supports Hadoop with a v

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

[LeetCode] Remove Boxes 移除盒子

Given several boxes with different colors represented by different positive numbers. You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes

Hadoop监控分析工具Dr.Elephant

公司基础架构这边想提取慢作业和获悉资源浪费的情况,所以装个dr elephant看看.LinkIn开源的系统,可以对基于yarn的mr和spark作业进行性能分析和调优建议. DRE大部分基于java开发,spark监控部分使用scala开发,使用play堆栈式框架.这是一个类似Python里面Django的框架,基于java?scala?没太细了解,直接下来就能用,需要java1.8以上. prerequest list: Java 1.8 PlayFramework+activator No

练习题目 4 Little Elephant and Cards

Little Elephant and Cards Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the c

UVa 12657 Boxes in a Line(应用双链表)

Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th

UVa - 103 - Stacking Boxes

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

poj 1475 Pushing Boxes

http://poj.org/problem?id=1475 Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4662   Accepted: 1608   Special Judge Description Imagine you are standing inside a two-dimensional maze composed of square cells which may or

HDU 1475 Pushing Boxes

Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ID: 147564-bit integer IO format: %lld      Java class name: Main Special Judge Imagine you are standing inside a two-dimensional maze composed of sq