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;
    }
  • 相关阅读:
    你真的理解clear:both吗?
    动态修改DataSet的列名,GridView动态绑定列
    word2007 添加批注后怎样让文档内容不向左移动 保持不变
    【转载】哪个OA比较好,18家常见OA系统全方位大阅兵
    短线选股的四大核心要素
    ASPX页面的缓存OutputCache
    OA产品的边际竞争者
    老股民经验之谈 这些股票买入必死无疑
    Word 2007批注及批注者姓名修改技巧
    ASP.NET中httpmodules与httphandlers全解析
  • 原文地址:https://www.cnblogs.com/Blackops/p/6550039.html
Copyright © 2011-2022 走看看