zoukankan      html  css  js  c++  java
  • POJ3111 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 = {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
    最近刚学习了01规划,嗯,这差不多是裸题,然后,没有用binary_search ,学习了Dinkelbach ,其实感觉很迷吧, 会用就行了?
    嗯,后面会多练习几道类似的题 。
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    const int N = 100000 + 11 ; 
    using namespace std ;
    int n , k , maxn ; 
    struct node
    {
        int v , w , id ; double val ;
        bool operator < ( const node a ) const { return val > a.val ; }
    } num[N] ;
    
    void Init( )
    {
        scanf( "%d%d" , &n , &k ) ;
        for( int i = 1 ; i <= n ; ++i )
        {    scanf( "%d%d" , &num[i].v , &num[i].w ) ;    
            num[i].id = i ;
        }
    }
    
    double abse( double a )
    {
        return ( a < 0 ) ? -a : a ;
    }
    
    double search( double l )
    {
        for( int x = 1 ; x <= n ; ++x ) num[x].val = (double)num[x].v - (double)num[x].w * l ;
        sort( num + 1 , num + 1 + n ) ;
        int tv = 0 , tw = 0 ;
        for( int i = 1 ; i <= k ; ++i )
            tv += num[i].v , tw += num[i].w ;    
        double ret = (double)tv/(double)tw ;
        return ret ; 
    }
    
    
    void Solve( )
    {
        double ans = 1 , tmp ; 
        while( 1 )
        {
            tmp = search( ans ) ;
            if( abse( ans - tmp ) < 0.001 ) break ;
            ans = tmp ;
        }
        for( int x = 1 ; x <= k ; ++x )
            printf( "%d " , num[x].id ) ;
        puts( "" ) ;
        
    }
    
    int main( )
    {
        Init( ) ;
        Solve( ) ;
        return 0 ;
    }
    
    
  • 相关阅读:
    js中属性和方法的类型和区别
    深入理解js构造函数
    null和undefined区别(转)
    理解闭包 js回收机制
    php:判断 是否开启 SSL,CURL,ZIP,GD2,MYSQL,是否安装MEMCACHED
    php:封装了个时间函数,返回类似“1分钟前发布”,“5小时前发布”,“3年前发布”
    php:获取一个表不含text类型的全部字段
    php:两个文件夹递归地比较,没有的文件自动复制过去
    php:php相关的函数或用法记录
    js:常用到的js操作记录
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6027298.html
Copyright © 2011-2022 走看看