poj 3111 K Best 二分
2017-11-17 22:37
417 查看
K Best
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个宝石里选k个,求
的最大值。
分析:经典题啊,公式少许变化,然后排序、二分。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <list>
#include <sstream>
#include <set>
#include <functional>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 100
#define ESP 1e-6
typedef long long ll;
int n,k;
struct node{
int id,v,w;
double ans;
bool operator < (const node &a) const{
return ans > a.ans;
}
}y[1000001];
bool C(double x)
{
for (int i = 0; i < n; i += 1) y[i].ans = y[i].v-x*y[i].w;
sort(y,y+n);
double sum = 0;
for (int i = 0; i < k; i += 1) sum += y[i].ans;
return sum >= 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
cin >> n >> k;
for (int i = 0; i < n; i += 1) {
cin >> y[i].v >> y[i].w;
y[i].id = i+1;
}
double lb = 0,ub = INF;
while (ub-lb>=ESP){
double mid = (ub+lb)/2;
if(C(mid)) lb = mid;
else ub = mid;
}
for (int i = 0; i < k-1; i += 1) printf("%d ",y[i].id);
printf("%d\n",y[k-1].id);
return 0;
}
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 11677 | Accepted: 3016 | |
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个宝石里选k个,求
的最大值。
分析:经典题啊,公式少许变化,然后排序、二分。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <list>
#include <sstream>
#include <set>
#include <functional>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 100
#define ESP 1e-6
typedef long long ll;
int n,k;
struct node{
int id,v,w;
double ans;
bool operator < (const node &a) const{
return ans > a.ans;
}
}y[1000001];
bool C(double x)
{
for (int i = 0; i < n; i += 1) y[i].ans = y[i].v-x*y[i].w;
sort(y,y+n);
double sum = 0;
for (int i = 0; i < k; i += 1) sum += y[i].ans;
return sum >= 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
cin >> n >> k;
for (int i = 0; i < n; i += 1) {
cin >> y[i].v >> y[i].w;
y[i].id = i+1;
}
double lb = 0,ub = INF;
while (ub-lb>=ESP){
double mid = (ub+lb)/2;
if(C(mid)) lb = mid;
else ub = mid;
}
for (int i = 0; i < k-1; i += 1) printf("%d ",y[i].id);
printf("%d\n",y[k-1].id);
return 0;
}
相关文章推荐
- poj 3111 K Best (二分搜索之最大化平均值之01分数规划)
- POJ 3111:K Best(思维+二分)
- POJ - 3111 K Best(二分)
- POJ 3111 K Best 二分 最大化平均值
- POJ 3111 K Best(二分——最大化平均值)
- K Best poj 3111 (01分数规划---二分搜索)
- poj 3111 K Best(二分-最大化平均值)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- poj3111- POJ - 3111 K Best (二分 + 01分数规划)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- poj 3111 K Best 【0-1分数规划】 【二分 or Dinkelbach迭代】
- poj 3111 K Best(二分)
- POJ 3111 K Best + poj 2976 Dropping tests (二分, 最大化平均值)
- POJ 3111 - K Best(二分搜索)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)
- POJ 3111 K Best (01分数规划+二分)
- POJ 3111 K Best 二分(最小化平均值)
- poj 3111 best(二分)
- K Best POJ - 3111 (二分最大化平均值,注意用G++提交,不然TLE)