zoukankan      html  css  js  c++  java
  • POJ 3111 K Best(01分数规划)

    K Best
    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 = {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

    题目链接: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 ? "
    " : " ");
        }
        return 0;
    }
  • 相关阅读:
    20201009 SpringBoot WebFlux 笔记
    20200930 尚硅谷 Netty【归档】
    20200930 11. 用Netty 自己 实现 dubbo RPC
    如何要注销按钮更加优雅,点击过程 会有友情提示
    浏览器执行代码 是jsp 服务端执行的是<%%>
    细谈getRequestDispatcher()与sendRedirect()的区别
    session.invalidate()
    python中的列表的嵌套与转换
    基于ajax 验证表单是否被占用----------------附:10.25日总结
    如何避免提交页面,信息未填写完善 就出现注册成功提示 基于js
  • 原文地址:https://www.cnblogs.com/Blackops/p/6550039.html
Copyright © 2011-2022 走看看