http://acm.hdu.edu.cn/showproblem.php?pid=1009
Analysis:
Simply find the unit price per Java bean in each room.
This article mainly talk about pit point (seemingly also say not on).
Paste the WA code first:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int a, b;
Double C;
} X[1005];
BOOL CMP (Node A, Node B)
{
return a.c > B.C;
}
int main ()
{
int n, m;
while (CIN >> n >> m&&n! =-1 | | m! =-1)
{
double ans = 0;
for (int i = 0;i<m;i++)
{
cin >> x[i].a >> x[i].b;
X[I].C = x[i].a*1.0/x[i].b;
cout << x[i].c << endl;
}
Sort (x, x + M, CMP);
for (int j = 0;j < m;j++)
{
if (n >= x[j].b)
{
ans + = (x[j].b*x[j].c); The reason for WA, because the X[J].C is double, has been approximate calculation, the calculation of the error after several times is large.
N-= x[j].b;
}
else
{
ans + = (n*x[j].c);
break;
}
}
printf ("%.3lf\n", ans);
}
}
AC code (only one change):
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int a, b;
Double C;
} X[1005];
BOOL CMP (Node A, Node B)
{
return a.c > B.C;
}
int main ()
{
int n, m;
while (CIN >> n >> m&& (n! =-1 && m! =-1))
{
double ans = 0;
for (int i = 0;i<m;i++)
{
cin >> x[i].a >> x[i].b;
X[I].C = x[i].a*1.0/x[i].b;
}
Sort (x, x + M, CMP);
for (int i = 0;i < m;i++)
// cout << x[i].a << " " << x[i].b << Endl;
for (int j = 0;j < m;j++)
{
if (n >= x[j].b)
{
ans + = (x[j].a);
N-= x[j].b;
}
else
{
ans + = (n*x[j].c);
break;
}
}
printf ("%.3lf\n", ans);
}
}