zoukankan      html  css  js  c++  java
  • POJ 3111

    K Best
    Time Limit: 8000MS   Memory Limit: 65536K
    Total Submissions: 5177   Accepted: 1411
    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 ≤ kn ≤ 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

    Northeastern Europe 2005, Northern Subregion
     
    二分平均值
    x为平均值,判断的时候 计算每个数的 (v[i] - w[i] * x) ,排序,找出最大的k个数,加起来,如果大于等于0 则往上找,否则往下
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <utility>
     6 
     7 using namespace std;
     8 
     9 #define maxn 100005
    10 #define eps 1e-8
    11 typedef pair<double,int> pii;
    12 
    13 int w[maxn],v[maxn];
    14 double f[maxn];
    15 pii ans[maxn];
    16 int n,k;
    17 
    18 
    19 bool judge(double x) {
    20         for(int i = 1; i <= n; ++i) {
    21                 f[i] = (v[i] - x * w[i]);
    22         }
    23         sort(f + 1,f + n + 1);
    24 
    25         double sum = 0;
    26         for(int i = n; i >= n - k + 1; --i) {
    27                 sum += f[i];
    28         }
    29 
    30         return sum >= 0;
    31 
    32 }
    33 
    34 void output(double x) {
    35 
    36         for(int i = 1; i <= n; ++i) {
    37                 ans[i] = make_pair(v[i] - x * w[i],i);
    38         }
    39         sort(ans + 1,ans + n + 1);
    40 
    41         printf("%d",ans[n].second);
    42         for(int i = n - 1; i >= n - k + 1; --i) {
    43                 printf(" %d",ans[i].second);
    44         }
    45 
    46         printf("
    ");
    47 
    48 
    49 }
    50 
    51 void solve() {
    52         double l = 0,r = 0;
    53         for(int i = 1; i <= n; ++i) {
    54                 r += v[i];
    55         }
    56 
    57         int num = 0;
    58         while(num++ < 50 ) {
    59                 //printf(" l = %f r = %f
    ",l,r);
    60                 //num++;
    61                 double mid = (l + r) / 2;
    62                 if(judge(mid)) {
    63                         l = mid;
    64                 } else {
    65                         r = mid;
    66                 }
    67 
    68         }
    69 
    70         //printf("%f
    ",l);
    71         output(l);
    72 }
    73 int main()
    74 {
    75 
    76     //freopen("sw.in","r",stdin);
    77 
    78     while(~scanf("%d%d",&n,&k))
    79     {
    80         for(int i = 1; i <= n; ++i) {
    81             scanf("%d%d",&v[i],&w[i]);
    82         }
    83 
    84         solve();
    85     }
    86 
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    mysql相关笔记
    qt杂项
    rpm离线安装整理
    linux fopen个数限制的问题(文件描述符限制)
    解决free():invalid pointer:0x00000000000000155455 ****的问题。
    linux c获取系统时间戳
    ubuntu QT Creater 安装
    LinkedHashMap如何保证顺序性
    HashMap原理(二) 扩容机制及存取原理
    HashMap原理(一) 概念和底层架构
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3600483.html
Copyright © 2011-2022 走看看