hdu 1009 FatMouse' Trade

来源:互联网 发布:mac截图保存位置修改 编辑:程序博客网 时间:2024/06/05 23:26
贪心。。。。。。
#include<iostream>
#include<string.h>
using namespace std;
int main()    //0 1不行了
{
 int M,N;//M 老鼠有的猫粮 N 仓库的数目
 double ans=0;
 double c;
 int t1,t2,t=N;
 int i=1,z=1;
 while(scanf("%d%d",&M,&N)==2)
 {
 if(M==-1&&N==-1) break;
 int *p=new int[N+1];
 int *q=new int[N+1];
 memset(p,0,N*sizeof(int));
 memset(q,0,N*sizeof(int));
 t=N;
 i=1,z=1;
 ans=0.0;
 while(t--&&scanf("%d%d",&p[i++],&q[z++])==2);//i不能同时做两个变量的下标
 for(i=1;i<N;i++)
 for(int j=1;j<N+1-i;j++)
 {
  if(((double)p[j]/q[j])>((double)p[j+1]/q[j+1]))
  {
   t1=p[j];
   p[j]=p[j+1];
   p[j+1]=t1;
   t2=q[j];
   q[j]=q[j+1];
   q[j+1]=t2;
   
  }
 }
 if(M==0)
 for(int j=1;j<N+1;j++)
  if(q[j]==0)ans+=p[j];
 i=N;
 while(M&&i)//i也要保证不超过-1
 {
  if(M>q[i])
  {
   ans+=p[i];
   M-=q[i];
   //cout<<"a y";
   //cout<<ans<<endl;
  }
  else
  {
   c=(double)p[i]/q[i];
   //cout<<p[i]<<" "<<q[i]<<" "<<c<<" ";
   ans+=c*M;
   //cout<<ans<<endl;
   M=0;
  }
  i--;
 }
 printf("%.3f\n",ans);
  delete []p;
 delete []q;
   }
/* for(int j=1;j<N+1;j++)
 cout<<p[j]<<endl; */
 return 0;
 
}
原创粉丝点击