hdu 2111 Saving HDU (DP)

Claire_ljy發表於2020-04-04

點選開啟連結

ps:動態規劃 -裝箱
#include<stdio.h>
#include<stdlib.h>
	   struct fun
   {
	   int p,m;
   }a[105];
   int cmp(const void*a,const void *b)
   {
	   struct fun *c=(struct fun*)a,*d=(struct fun*)b;
	   return d->p-c->p;
   }
   int main()
   {
	   int v,n,i,tol;
	   while(scanf("%d%d",&v,&n)!=EOF&&v!=0)
	   {
		   tol=0;
		   for(i=0;i<n;i++)
			   scanf("%d%d",&a[i].p,&a[i].m);
		   qsort(a,n,sizeof(a[0]),cmp);
		   for(i=0;i<n;i++)
		   {
			   if(a[i].m<=v)
			   {
				   tol+=a[i].p*a[i].m;
				   v-=a[i].m;
			   }
			   else
			   {
				   tol+=a[i].p*v;
				   v=0;
			   }
		   }
		   printf("%d\n",tol);
	   }
	   return 0;
   }


轉載於:https://www.cnblogs.com/yyf573462811/archive/2012/09/02/6365222.html

相關文章