K Best(二分搜索)

K Best
Time Limit: 8000MS  Memory Limit: 65536K
Total Submissions: 5288  Accepted: 1431
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

 

       题意:

       给出 N , K,代表有 N 颗宝石,要求从中选择 K 颗,使等式   的 S 值达到最大,输出应该选择哪几颗宝石。

 

       思路:

       二分搜索。求 v - s * w >= 0 情况下前 K 个的最大值。二分 S 后,对 v - s * w 由大到小排序即可。double 输入是 %lf。

      

       AC:

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef struct {
    double v, w, h;
    int num;
}node;

const int INF = 10000005;

node jew[100005];
int n, k;

int cmp(node a, node b) { return a.h > b.h; }

bool C(double s) {
        for (int i = 0 ; i < n; ++i)
                jew[i].h = jew[i].v - s * jew[i].w;

        sort(jew, jew + n, cmp);

        double sum = 0;
        for (int i = 0; i < k; ++i)
                sum += jew[i].h;

        return sum >= 0;
}

void solve() {
        double l = 0, r = INF;

        for (int i = 0; i < 50; ++i) {
                double mid = (l + r) / 2;
                if (C(mid)) l = mid;
                else r = mid;
        }

        for (int i = 0 ; i < k; ++i) {
                printf("%d", jew[i].num);
                i == k - 1 ? printf("\n") : printf(" ");
        }
}

int main () {
        scanf("%d%d", &n, &k);

        for (int i = 0; i < n; ++i) {
                scanf("%lf%lf", &jew[i].v, &jew[i].w);
                jew[i].num = i + 1;
        }

        solve();

        return 0;
}

 

 

 

### Java 实现分巧克力问题的二分法算法 #### 1. 问题描述 给定若干块不同尺寸的巧克力,每块巧克力由两个整数表示其长度和宽度。目标是找到一个最大的正方形边长 \( d \),使得可以从这些巧克力中切割出尽可能多的小朋友数量相同的正方形巧克力。 #### 2. 解决方案概述 为了高效解决这个问题,采用二分查找的方法来确定最大可能的正方形边长 \( d \)。通过不断调整上下限并验证当前中间值是否满足条件,最终得到最优解。 #### 3. 算法步骤说明 - 定义变量 `low` 和 `high` 分别初始化为最小可能值(通常是1)以及所有矩形中最短的一条边作为初始上限。 - 计算中间值 `mid = low + (high - low >> 1)` 并检查能否按照此大小分配足够的份数。 - 如果可以,则尝试更大的值;反之缩小范围直到收敛于最佳答案。 #### 4. 关键函数解析 定义辅助方法用于判断特定尺寸下能否成功分割足够数量: ```java private static boolean canDivide(int[][] chocolates, int mid, int k){ int count = 0; for (int[] chocolate : chocolates){ count += ((chocolate[0] / mid) * (chocolate[1] / mid)); if(count >= k) return true; // 提前终止循环提高效率 } return false; } ``` 该逻辑实现了对于每一个候选边长 `mid` 的可行性评估,并利用提前退出机制减少不必要的计算开销[^3]。 #### 5. 主体代码实现 以下是完整的Java程序清单,展示了如何应用上述思路解决问题: ```java import java.util.*; public class ChocolateDivision { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { List<int[]> inputChocolates = readInput(); System.out.println(findMaxSquareSize(inputChocolates.toArray(new int[inputChocolates.size()][]), getK())); } private static List<int[]> readInput(){ int n = Integer.parseInt(scanner.nextLine()); List<int[]> result = new ArrayList<>(); while(n-- > 0){ String[] line = scanner.nextLine().split(" "); result.add(new int[]{Integer.parseInt(line[0]), Integer.parseInt(line[1])}); } return result; } private static int findMaxSquareSize(int[][] chocolates, int k){ Arrays.sort(chocolates, Comparator.comparingInt(o -> Math.min(o[0], o[1]))); int minEdgeLength = 1; int maxPossibleEdgeLength = chocolates[chocolates.length - 1][0]; int bestFit = binarySearchForBestFit(minEdgeLength, maxPossibleEdgeLength, chocolates, k); return bestFit; } private static int binarySearchForBestFit(int low, int high, int[][] chocolates, int k){ while(low <= high){ int mid = low + ((high - low) >>> 1); if(canDivide(chocolates, mid, k)){ low = mid + 1; }else{ high = mid - 1; } } return high; } private static boolean canDivide(int[][] chocolates, int mid, int k){ long piecesCount = 0L; for(var item : chocolates){ piecesCount += (((item[0]/mid)*(item[1]/mid))); if(piecesCount>=k)return true; } return false; } private static int getK() { return Integer.parseInt(scanner.nextLine()); } } ``` 这段代码首先读入输入数据,接着调用核心处理流程 `findMaxSquareSize()` 来执行主要运算过程。其中包含了对原始数据按最短边排序的操作以便更合理地设定搜索区间边界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值