标题:求助 dp问题 实在是想不出来
只看楼主
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
写了一段代码,但我没法验证它是否正确

程序代码:
#include <stdio.h>

typedef struct {
    unsigned a;
    unsigned b;
} pair;

static pair foo_( pair p[], unsigned n, unsigned m, pair cur )
{
    if( m == 0 )
        return cur;

    double cpr = 0;
    unsigned index = 0;
    for( unsigned i=0; i!=n; ++i )
    {
        if( p[i].a != 0 )
        {
            if( cpr < (p[i].a + cur.a)*1.0/(p[i].b + cur.b) )
            {
                cpr = (p[i].a + cur.a)*1.0/(p[i].b + cur.b);
                index = i;
            }
        }
    }
    pair t = { p[index].a+cur.a, p[index].b+cur.b };
    p[index].a = 0;
    return foo_(p,n,m-1,t);
}
double foo( pair p[], unsigned n, unsigned m )
{
    pair r = foo_( p, n, m, (pair){0,0} );
    return r.a*1.0/r.b;
}

int main( void )
{
    unsigned t;
    scanf( "%u", &t );
    for( unsigned i=0; i!=t; ++i )
    {
        pair p[10000];
        unsigned n, m;
        scanf( "%u%u", &n, &m );
        for( unsigned j=0; j!=n; ++j )
            scanf( "%u%u", &p[j].a, &p[j].b );
        printf( "Case #%u: %.2f\n", i+1, foo(p,n,m) );
    }
}
2020-11-30 10:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-504036-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.151679 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved