poj3111 选取物品(二分+贪心)
题意就是,n个物品,从中选取k个,要按照那个公式所取的值最大。
思路:最大化平均值的时候首先想到的就是二分, 我们设G(x) 为单位的重量不小于X, 我们的目标就是要找到满足条件的最大的X, 也就是说我们的OK函数就要判断是否能够找到一个大小为K的集合S, 使得单位重量的价值大于等于X, 我们将当时变形转化为vi - x*wi >= 0 对于 所有的i 属于集合S, 这时我们对于一个整体值vi-x*wi的值的 排序, 贪心的选取前K个, 这样OK函数就写好了。
但是这道题最恶心的地方是,卡常数,如果要用c++交,你的排序函数就必须 引用,g++交好像就没关系。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<cmath>
#include<time.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<numeric>
using namespace std;
typedef long long ll;
const int maxn=2000010;
const int INF=0x3f3f3f3f;
struct dian{
double v,w;
double y;
int id;
}a[maxn];
double mps=1e10,eps=1e-9;
double l,r,mid;
int n,k;
bool cmp(const dian& a,const dian& b){
return a.y>b.y;
}
bool ok(double d){
double e=0;
for(int i=1;i<=n;i++){
a[i].y=a[i].v-a[i].w*d;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=k;i++){
e+=a[i].y;
}
return e>=0;
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%lf%lf",&a[i].v,&a[i].w);
a[i].id=i;
}
l=0,r=1e13;
while(r-l>eps){
mid=(l+r)/2;
if(ok(mid)){
l=mid;
}else{
r=mid;
}
}
for(int i=1;i<=k;i++){
printf("%d ",a[i].id);
}
printf("\n");
}
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 13263 | Accepted: 3388 | |
Case Time Limit: 2000MS | Special Judge |
Description
Demy has n jewels. Each of her jewels has some value vi and weight wi.
Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. She decided to keep such jewels that their specific value is as large as possible. That is, denote the specific value of some set of jewels S = {i1, i2, …, ik} as
.
Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so.
Input
The first line of the input file contains n — the number of jewels Demy got, and k — the number of jewels she would like to keep (1 ≤ k ≤ n ≤ 100 000).
The following n lines contain two integer numbers each — vi and wi (0 ≤ vi ≤ 106, 1 ≤ wi ≤ 106, both the sum of all vi and the sum of all wi do not exceed 107).
Output
Output k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.
Sample Input
3 2 1 1 1 2 1 3
Sample Output
1 2