hdu2570 (greedy)

http://acm.hdu.edu.cn/showproblem.php?pid=2570

Also considered the question carefully ah.

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
    
#include " iostream "
#include
" algorithm "
using namespace std;
int main()
{
int i,t;
int n,v,w;
double a[ 110 ];
cin
>> t;
while (t -- )
{
cin
>> n >> v >> w;
for (i = 0 ;i < n;i ++ ) cin >> a[i];
sort(a,a
+ n);
double p = 0.0 ;
int k = 0 ;

for (i = 0 ;i < n;i ++ )
{
if ((p * i + a[i]) / (i + 1 ) <= w) // (sum*i*v+a[i]*v)/((i+1)*v)
{
p
= (p * i + a[i]) / ((i + 1 ));
}
else break ;
}
printf(
" %d %.2lf\n " ,i * v,p / 100 );
}
return 0 ;
}

The following method is a beginning, because few take up a v, w n times, the tragedy!

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
    
#include " iostream "
#include
" algorithm "
using namespace std;
int main()
{
int t,i,j;
int n,v,w;
int a[ 110 ];
cin
>> t;
while (t -- )
{
cin
>> n >> v >> w;
for (i = 0 ;i < n;i ++ ) cin >> a[i];
sort(a,a
+ n);
double SUM = 0 , S = 0 ;

the __int64 K
= 0 ;
int T = . 1 ;
for (I = 0 ; I < n-; I ++ )
{
S
+ = A [I] * V; // here because of beginning take a few V, n times the result was wrong, but just does not affect the test case, tragedy ah! IF (S / (V * (I + . 1 )) <= W) { SUM = S / (V * (I + . 1 ));



k
++ ;
}
else break ;
}

printf(
" %I64d %.2lf\n " ,k * v,sum / 100 );

}
return 0 ;
}

Reproduced in: https: //www.cnblogs.com/FCWORLD/archive/2011/05/04/2036083.html

Guess you like

Origin blog.csdn.net/weixin_34061555/article/details/94156122