还是二分查找,这几题都差不多,很快就水完了。。这题开始因为精度设的太小WA了一次,设到1e-6就可以A了
#include<cstdio>
#include<algorithm>
using namespace std;
namespace
{
struct Jew
{
int v, w, index;
double value;
} dat[100000];
int n, k;
const double eps = 1e-6;
bool cmp(struct Jew j1, struct Jew j2)
{
return j1.value > j2.value;
}
bool check(double s)
{
for (int i = 0; i < n; i++)
dat[i].value = dat[i].v - s * dat[i].w;
sort(dat, dat + n, cmp);
double sum = 0;
for (int i = 0; i < k; i++)
sum += dat[i].value;
return sum >= 0;
}
void solve(double ub)
{
double lb = 0, mid;
while (lb + eps < ub)
{
mid = (lb + ub) / 2;
if (check(mid))
lb = mid;
else
ub = mid;
}
for (int i = 0; i < k; i++)
printf(i ? " %d" : "%d", dat[i].index);
puts("");
}
}
int main()
{
scanf("%d %d", &n, &k);
double maxx = 0;
for (int i = 0; i < n; i++)
{
scanf("%d %d", &dat[i].v, &dat[i].w);
dat[i].index = i + 1;
maxx = max(maxx, dat[i].v * 1.0 / dat[i].w);
}
solve(maxx);
return 0;
}