POJ 3111 K Best(二分搜索,最大化平均值)
2015-12-04 21:45
417 查看
[align=center]K Best[/align]
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
Sample Output
题意:有N颗珠宝,每颗珠宝的价值为vi,重量为wi。 女主不得已要卖掉部分珠宝,她想留下k颗珠宝,并要求(v1+v2+....vk)/(w1+w2+...wk)的值最大,输出女主留下的珠宝的编号。(可不按输入的顺序输出)。
题解:二分搜索,找出最大化的平均值,然后记录下每次被选中的珠宝编号,输出即可。
代码如下:
#include<cstdio>
#include<algorithm>
#define INF 1000100
using namespace std;
int n,k;
double v[100010],w[100010];
struct node
{
int id;
double y;
}num[100010];
int cmp(node a,node b)
{
return a.y>b.y;
}
bool dis(double x)
{
int i=0;
for(i=0;i<n;++i)
{
num[i].y=v[i]-x*w[i];
num[i].id=i+1;
}
sort(num,num+n,cmp);
double sum=0;//不小心把sum定义成了int,一直WA
for(i=0;i<k;++i)
sum+=num[i].y;
return sum>=0;
}
int main()
{
int i;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=0;i<n;++i)
scanf("%lf%lf",&v[i],&w[i]);
double left=0,right=INF,mid;
while(right-left>1e-8)//100次超时,50次能过,30次不能过
{
mid=(left+right)/2;
if(dis(mid))
left=mid;
else
right=mid;
}
for(i=0;i<k-1;++i)
printf("%d ",num[i].id);
printf("%d\n",num[i].id);
}
return 0;
}
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 7491 | Accepted: 1936 | |
Case Time Limit: 2000MS | Special Judge |
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
题意:有N颗珠宝,每颗珠宝的价值为vi,重量为wi。 女主不得已要卖掉部分珠宝,她想留下k颗珠宝,并要求(v1+v2+....vk)/(w1+w2+...wk)的值最大,输出女主留下的珠宝的编号。(可不按输入的顺序输出)。
题解:二分搜索,找出最大化的平均值,然后记录下每次被选中的珠宝编号,输出即可。
代码如下:
#include<cstdio>
#include<algorithm>
#define INF 1000100
using namespace std;
int n,k;
double v[100010],w[100010];
struct node
{
int id;
double y;
}num[100010];
int cmp(node a,node b)
{
return a.y>b.y;
}
bool dis(double x)
{
int i=0;
for(i=0;i<n;++i)
{
num[i].y=v[i]-x*w[i];
num[i].id=i+1;
}
sort(num,num+n,cmp);
double sum=0;//不小心把sum定义成了int,一直WA
for(i=0;i<k;++i)
sum+=num[i].y;
return sum>=0;
}
int main()
{
int i;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=0;i<n;++i)
scanf("%lf%lf",&v[i],&w[i]);
double left=0,right=INF,mid;
while(right-left>1e-8)//100次超时,50次能过,30次不能过
{
mid=(left+right)/2;
if(dis(mid))
left=mid;
else
right=mid;
}
for(i=0;i<k-1;++i)
printf("%d ",num[i].id);
printf("%d\n",num[i].id);
}
return 0;
}
相关文章推荐
- [转]排查线程池使用情况,可定时任务统计.打印到指定日志. 获取系统扩容,线程池改造信息.
- 错误: java.io.NotSerializableException: net.sf.json.JSONObject 的解决方法
- 数据结构实验之查找一:二叉排序树 SDUT 二叉排序树的建立与判断
- hpuoj 1720: 感恩节KK专场——面试难题【贪心】
- project euler 58
- 39.XML数据解析的DOM解析
- Docker跨主机通信:桥接方式和路由方式
- 阶乘
- leetcode Ugly Number
- HTML学习笔记之--HTML主体的常用设置
- GoogleMap 循环绑定图标点击事件的坑
- project euler 57
- 数组
- 将博客搬至CSDN
- poj 2240 Arbitrage 【最短路&&floyd】
- project euler 56
- poj1163 The Triangle
- Java编写Telnet客户端,连接到Windows的Telnet服务器,执行命令和批处理脚本
- seajs加载jquery时提示$ is not a function该怎么解决
- setValuesForKeysWithDictionary:方法的改进