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 ;
    }
    
    
  • 相关阅读:
    MyBatis入门学习教程-优化MyBatis配置文件中的配置
    MyBatis入门学习教程-使用MyBatis对表执行CRUD操作
    MyBatis入门学习教程-MyBatis快速入门
    Apache Shiro 简单概念
    将 Shiro 作为应用的权限基础
    在 Web 项目中应用 Apache Shiro
    Shiro简单配置
    Shiro权限框架简单快速入门
    解决错误: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
    解决Shiro注解无效的问题
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6027298.html
Copyright © 2011-2022 走看看