poj_1018_Communication System

此题一开始以为是dp题目,但是想了很久都想不出dp的解法,最后是用贪心过的
贪心思路如下:
          1.首先对所有的宽带进行排序
          2.对每组数据按照价钱排序
          3.然后从小到打枚举每种宽带的可能性
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAXN    101

typedef struct B_NODE {
        int b, p, ori_pos;
}BNODE;

typedef struct VAL_ {
        int b, p;
}VAL;

typedef struct NODE_ {
        int m;
        VAL val[MAXN];
}NODE;

NODE type[MAXN];
BNODE b_node[MAXN*MAXN];
int pos;

int b_node_cmp(const BNODE &a, const BNODE &b)
{
        return a.b < b.b;
}

int type_cmp(const VAL &a, const VAL &b)
{
        return a.p < b.p;
}

int select_ans(const int &x_pos, const int &up, const int &n)
{
        int rst(0), flag;
        for(int i = 0; i < n; i ++) {
                if( i == x_pos ) {
                        continue;
                }
                flag = 0;
                for(int j = 0; j < type[i].m; j ++) {
                        if( type[i].val[j].b < up ) {
                                continue;
                        }
                        rst += type[i].val[j].p; flag = 1; break;
                }
                if( !flag ) {
                        return -1;
                }
        }
        return rst;
}

double forces_greed(const int &n)
{
        double ans(0.0);
        int select_rst, up, down;
        for(int i = 0; i < pos; i ++) {
                up = b_node[i].b; down = b_node[i].p;
                if( -1 == (select_rst = select_ans(b_node[i].ori_pos, up, n)) ) {
                        return ans;
                }
                ans = max(ans, (double)up/(down+select_rst));
        }
        return ans;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int cas, n;
        scanf("%d", &cas);
        while( cas -- ) {
                scanf("%d", &n); pos = 0;
                for(int i = 0; i < n; i ++) { scanf("%d", &type[i].m);
                        for(int j = 0; j < type[i].m; j ++) {
                                scanf("%d %d", &type[i].val[j].b, &type[i].val[j].p); b_node[pos].ori_pos = i;
                                b_node[pos].b = type[i].val[j].b; b_node[pos ++].p = type[i].val[j].p;
                        }
                }
                sort(b_node, b_node+pos, b_node_cmp);

                for(int i = 0; i < n; i ++) {
                        sort(type[i].val, type[i].val+type[i].m, type_cmp);
                }
                printf("%.3lf\n", forces_greed(n));
        }
        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值