POJ 3111 K Best(01分数规划)
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 9876 | Accepted: 2535 | |
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
Source
题目链接:POJ 3111
这题多了一个得输出方案序号,用结构体记录一下id就行,代码的话迭代或者二分都可以,但是迭代速度快很多很多
迭代代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
const double eps = 1e-9;
struct info
{
double w, v, d;
int id;
bool operator<(const info &rhs)const
{
return d > rhs.d;
}
};
info A[N]; double getnewk(int n, int k, double r)
{
double V = 0.0, W = 0.0;
for (int i = 0; i < n; ++i)
A[i].d = A[i].v - r * A[i].w;
sort(A, A + n);
for (int i = 0; i < k; ++i)
{
V += A[i].v;
W += A[i].w;
}
return V / W;
}
int main(void)
{
int n, k, i;
while (~scanf("%d%d", &n, &k))
{
for (i = 0; i < n; ++i)
{
scanf("%lf%lf", &A[i].v, &A[i].w);
A[i].id = i + 1;
}
double ans = 1, temp = 1;
while (1)
{
temp = getnewk(n, k, ans);
if (fabs(temp - ans) < eps)
break;
ans = temp;
}
for (i = 0; i < k; ++i)
printf("%d%s", A[i].id, i == k - 1 ? "\n" : " ");
}
return 0;
}
POJ 3111 K Best(01分数规划)的更多相关文章
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
- POJ 2976 Dropping tests 01分数规划
给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- $POJ$2976 $Dropping\ tests$ 01分数规划+贪心
正解:01分数规划 解题报告: 传送门! 板子题鸭,,, 显然考虑变成$a[i]-mid\cdot b[i]$,显然无脑贪心下得选出最大的$k$个然后判断是否大于0就好(,,,这么弱智真的算贪心嘛$T ...
- POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))
题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ3111 K Best —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissio ...
- POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题
http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
随机推荐
- 2018.5.28 Oracle数据库补充
select * from (select rownum rn,e2.* from (select e1.* from emp e1)e2 where rownum<=10)e3 where e ...
- pandas 常用统计方法
统计方法 pandas 对象有一些统计方法.它们大部分都属于约简和汇总统计,用于从 Series 中提取单个值,或从 DataFrame 的行或列中提取一个 Series. 比如 DataFrame. ...
- C# StreamReader对象
1.读取文件 输入流用于从外部源读取数据,在很多情况下,数据源可以是磁盘上的文件或网络的某些位置,任何可能发送数据的位置都可以是数据源,比如网络应用程序,web服务,甚至是控制台.StreamRead ...
- 织梦dedecms出现系统基本参数空白或显示Call to undefined function make_hash()
织梦dedecms出现系统基本参数空白或显示Call to undefined function make_hash() 最新的织梦版本(2018-01-09)修改了include文件夹中的commo ...
- common-fileupload组件实现java文件上传和下载
简介:文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...
- ES6学习(一):数值的扩展
chapter06 数值的扩展 6.1 二进制和八进制 二进制 前缀 0b 或者 0B 八进制 前缀 0o 或者 0O 6.2 Number.isFinite() Number.isNaN() 原先这 ...
- 文档处理jQuery,实现添加删除复制
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- atm-interface-shopping
from db import db_handlerfrom interface import bank def shopping_interface(name, cost, shoppingcart) ...
- tp3.2读取time()格式遇到的的问题(尚未解决)
在用tp3.2框架做一个讲座模块.最近又遇到了一个问题 如上图所示,我把日期和讲座开始时间结束时间分来放了.(这里的Jdate2和jdate3本来存放为time(7)类型的,后发现在原来这个7是可以改 ...
- JZOJ 5842
Description 给定一个n*m 的 01 矩阵,求包含[l,r]个 1 的子矩形个数. Input 第一行,两个正整数n,m.接下来n 行,每行一个长度为 m 的 01 串,表示给定的矩阵.接 ...