题目链接:http://poj.org/problem?id=3111

K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 11380   Accepted: 2935
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 = {i1i2, …, 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

Source

Northeastern Europe 2005, Northern Subregion
 
 
 
 
题解:
 
 
代码一:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; struct node
{
double d;
int a, b, id;
bool operator<(const node x)const{
return d>x.d;
}
}q[MAXN];
int n, k; bool test(double L)
{
for(int i = ; i<=n; i++)
q[i].d = 1.0*q[i].a - L*q[i].b; sort(q+, q++n);
double sum = ;
for(int i = ; i<=k; i++) //取前k大的数
sum += q[i].d;
return sum>=;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
//一次性把所有信息都录入结构体中,当排序时,即使打乱了顺序,仍然还记得初始下标。
for(int i = ; i<=n; i++)
{
scanf("%d%d", &q[i].a, &q[i].b);
q[i].id = i;
} double l = , r = 1e7;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(mid))
l = mid + EPS;
else
r = mid - EPS;
} for(int i = ; i<=k; i++)
printf("%d ", q[i].id);
printf("\n");
}
}
 
代码二:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; struct node
{
double d;
int id;
bool operator<(const node x)const{
return d>x.d;
}
}q[MAXN]; int n, k;
int a[MAXN], b[MAXN]; bool test(double L)
{
for(int i = ; i<=n; i++) //每一次q[i]都重新更新,与a[i],b[i]独立开来
{
q[i].id = i;
q[i].d = 1.0*a[i] - L*b[i];
}
sort(q+, q++n);
double sum = ;
for(int i = ; i<=k; i++)
sum += q[i].d;
return sum>=;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d%d", &a[i], &b[i]); double l = , r = 1e7;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(mid))
l = mid + EPS;
else
r = mid - EPS;
} for(int i = ; i<=k; i++)
printf("%d ", q[i].id);
printf("\n");
}
}

错误代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; struct node
{
double d;
int id;
bool operator<(const node x)const{
return d>x.d;
}
}q[MAXN]; int n, k;
int a[MAXN], b[MAXN], ans[MAXN]; bool test(double L)
{
//经过一次排序后,q[i].id不再等于i,所以出错。应该同时更新q[i].id
for(int i = ; i<=n; i++)
q[i].d = 1.0*a[i] - L*b[i]; sort(q+, q++n);
double sum = ;
for(int i = ; i<=k; i++)
sum += q[i].d;
return sum>;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
for(int i = ; i<=n; i++)
{
scanf("%d%d", &a[i], &b[i]);
q[i].id = i;
} double l = , r = 1e7;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(mid))
l = mid + EPS;
else
r = mid - EPS;
} for(int i = ; i<=k; i++)
printf("%d ", q[i].id);
printf("\n");
}
}
 
 

POJ3111 K Best —— 01分数规划 二分法的更多相关文章

  1. POJ - 3111 K Best 0-1分数规划 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 12812   Accepted: 3290 Case Time ...

  2. [POJ3111]K Best(分数规划, 二分)

    题目链接:http://poj.org/problem?id=3111 求选k对数,使得上述式子值最大.容易想到设左边为一个值,对式子变形以下,得到sigma(v-r*w))==0的时候就是最大的,& ...

  3. POJ2976 Dropping tests —— 01分数规划 二分法

    题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  4. 01分数规划问题(二分法与Dinkelbach算法)

    链接 前置技能 二分思想 最短路算法 一些数学脑细胞? 问题模型1基本01分数规划问题给定n个二元组(valuei,costi),valuei是选择此二元组获得的价值(非负),costi是选择此二元组 ...

  5. 【转】[Algorithm]01分数规划

    因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...

  6. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

  7. Desert King(01分数规划问题)(最优斜率生成树)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:33847   Accepted: 9208 Descr ...

  8. 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}

    P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...

  9. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9703   Accepted: 3299 ...

随机推荐

  1. websql使用实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Chrome中输入框默认样式移除

    Chrome中输入框默认样式移除 在chrome浏览器中会默认给页面上的输入框如input.textarea等渲染浏览器自带的边框效果 IE8中效果如下: Chrome中效果如下:   这在我们未给输 ...

  3. java多线程编程核心技术学习-1

    实现多线程的两种方式 继承Thread类,重写Thread类中的run方法 public class MyThread extends Thread{ @Override public void ru ...

  4. Bayan 2015 Contest Warm Up D. CGCDSSQ (math,pair,map,暴力)

    哎,只能转题解了,,, 8165031                 2014-10-10 15:53:42     njczy2010     D - CGCDSSQ             GN ...

  5. cmd指令

    d:  进入D盘: cd job 进入d盘名为job的文件夹:cd显示当前路径: md test创建名为test的文件夹: rd test删除名为test的文件夹: cd.>test.json创 ...

  6. (43)C#网络1 http

    一.HttpClient类 用于发送http请求,并接受请求的相应 (从4.5起开始可用) using System.Net.Http; 异步调用 HttpClient httpClient = ne ...

  7. ELK之收集Nginx、Tomcat的json格式日志

    1.安装Nginx yum -y install nginx vim /etc/nginx/nginx.conf # 修改日志格式为json格式,并创建一个nginxweb的网站目录 log_form ...

  8. sqlalchemy的merge使用

    1.先看下文档 merge(instance, load=True) Copy the state of a given instance into a corresponding instance ...

  9. A002-开发工具介绍

    关于Android的开发工具有非常多,基本上都能够在SDK中找到.下面我们逐个来看一下: 首先我们使用的是Java语言进行Android应用的开发,那么Java的执行环境是少不了的了,我们须要在我们的 ...

  10. win10 UWP 申请微软开发人员

    申请微软开发人员能够到https://dev.windows.com/zh-cn/programs/join 假设是学生,先去http://www.dreamspark.com/ 假设是英文,点stu ...