63,594
社区成员




#include<iostream>
using namespace std ;
typedef struct node
{
int p , w ;
double info ;
}node;
int cmp ( const void * a ,const void * b )
{
node *c = (node *) a ;
node *d = (node *) b ;
if ( c->info > d->info )
return 1 ;
else
if ( c->info < d->info )
return -1 ;
else
{
if ( c->w > d->w )
return 1 ;
else
return -1;
}
}
int main ()
{
int T , E , F , N , i , x ,tw,ff;
node st[500];
while ( cin >> T )
{
while ( T > 0 )
{
cin >> E >> F ;
tw = F - E ;
x = 0 ;
cin >> N ;
for ( i = 0 ; i < N ; i ++ )
{
cin >> st[i].p >> st[i].w ;
st[i].info = ( double ) st[i].p / (double ) st[i].w ;
}
qsort((void *)st , N , sizeof ( st[0] ) , cmp ) ;
while (1)
{
for ( i = 0 ; i < N && tw > 0 ; i ++ )
{
while ( st[i].w <= tw )
{
tw = tw - st[i].w ;
x = x + st[i].p ;
}
}
if ( tw == 0 )
{
ff = 1 ;
break ;
}
if ( tw < 0 )
{
tw = tw + st[i - 1].w ;
x = x - st[i].p ;
continue ;
}
if ( i == N )
{
ff = 0 ;
break ;
}
}
if ( ff == 1 )
cout << "The minimum amount of money in the piggy-bank is " << x << '.' << endl ;
else
cout << "This is impossible." << endl ;
T -- ;
}
}
return 0 ;
}