本题一看似乎是递归回溯剪枝的方法。我一提交,结果超时。

然后又好像是使用DP,还可能我剪枝不够。

想了非常久,无奈忍不住偷看了下提示。发现方法真多。有贪心,DP,有高级剪枝的。还有三分法的。八仙过海各显神通啊。

坏习惯了,没思考够深入就偷看提示了。

幸好及时回头,还不须要看别人的代码了。自己做出来之后,有空看看多种解法的代码也好。

然后我想出自己的思路了,使用贪心,剪枝,DP综合优化下,呵呵。最后程序有点复杂。优化到了16ms,运气好点,或者vector换成原始数组的话,应该能够0MS了。

整体思路就是:

1 利用STL 的set容器记录有多少不同的B值

2 依据不同的B值,用表tbl记录该B值下的最优解

最后比較全部B值下的最优解。得出终于最优解。

以下是优化过的程序,用了不少技巧,加了凝视,希望提高參考价值吧。

#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std; const int MAX_N = 101;
int N, M; struct BP
{
int B, P;
bool operator<(const BP &b) const
{
return B < b.B;
}
}; BP arr[MAX_N][MAX_N]; float DP(set<int> &bset)
{
for (int i = 0; i < N; i++)
{
for (int j = arr[i][0].B-1; j > 0 ; j--)
{
arr[i][j].P = min(arr[i][j].P, arr[i][j+1].P);
}//计算结果为当前大于某个B的最小P值,优化以下填表
} vector<int> bvec(bset.begin(), bset.end());
int M = (int)bvec.size(); //总共同拥有多少个不同的B值
vector<vector<int> > tbl(N, vector<int>(M));//记录当前B下的最优P值
vector<int> idx(N, 1); //arr行的当前下标 for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
for ( ; idx[i] <= arr[i][0].B; idx[i]++)
{
if (arr[i][idx[i]].B >= bvec[j])
{
tbl[i][j] = arr[i][idx[i]].P;
break;
}
}
if (idx[i] > arr[i][0].B)//某行无法选出比B更大的值了
{
tbl[0][j] = -1;//做好标志,剪枝
goto out;
}
}
}
out:;
float ans = 0.0f;
for (int j = 0; j < M && tbl[0][j] != -1; j++)
{
int totalP = 0;
for (int i = 0; i < N; i++)
{
totalP += tbl[i][j];
}
ans = max(ans, float(bvec[j])/float(totalP));
}
return ans;
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
set<int> bset;
for (int i = 0; i < N; i++)
{
scanf("%d", &arr[i][0].B); //记录当前维长度
for (int j = 1; j <= arr[i][0].B; j++)
{
scanf("%d %d", &arr[i][j].B, &arr[i][j].P);
bset.insert(arr[i][j].B);//记录有多少个不同的B值
}
sort(arr[i]+1, arr[i]+arr[i][0].B+1);//每维按B值由小到大排序
}
printf("%.3f\n", DP(bset));
}
return 0;
}

POJ 1018 Communication System 题解的更多相关文章

  1. POJ 1018 Communication System(树形DP)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  2. POJ 1018 Communication System (动态规划)

    We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...

  3. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  4. poj 1018 Communication System 枚举 VS 贪心

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21631   Accepted:  ...

  5. POJ 1018 Communication System(贪心)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  6. poj 1018 Communication System (枚举)

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted:  ...

  7. POJ 1018 Communication System(DP)

    http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...

  8. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  9. poj 1018 Communication System_贪心

    题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...

随机推荐

  1. 在从1到n的正数中1出现的次数 【微软面试100题 第三十题】

    题目要求: 给定 一个十进制正整数N,写下从1开始,到N的所有整数,然后数一下其中出现的所有“1”的个数.    例如:N = 2,写下1,2.这样只出现了1个“1”.          N = 12 ...

  2. 关于dispatch_sync死锁问题

    首先,我们来看下下面一个例子: 代码:(串行队列里同步线程嵌套)     NSLog(@"haha");     dispatch_queue_t queue = dispatch ...

  3. day03_05 Python程序文件执行和与其他编程语言对比

    python在windows操作系统上是没有的,但是在linux上默认就有python 执行python程序的方式有两种: 1.交互器,缺点 程序不能永久保存,主要用于简单的语法测试 2.文件执行 对 ...

  4. MacPorts的安装和使用

    1.安装 MacPorts的官方网站:http://www.macports.org/install.php 有dmg安装和源代码安装两种方式,下载dmg格式一步步安装即可 2.使用 更新ports ...

  5. bzoj1433 [ZJOI2009]假期的宿舍 最大流

    [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3429  Solved: 1459[Submit][Status][D ...

  6. [暑假集训--数位dp]cf55D Beautiful numbers

    Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb ...

  7. 【CF679B】Theseus and labyrinth(数学,贪心)

    题意: 给一个m<=10^15,每次都减最接近当前值的立方数 让你找一个不大于m的最大的数并且这个数是减法次数最多的数 思路:见http://blog.csdn.net/miracle_ma/a ...

  8. angular中关于文件引入

    var angular = require('angular'); module.exports = angular.module('app.mymodule2', []).controller('H ...

  9. es6总结(七)--数据结构-Set & Map

  10. MySQL的@与@@区别

    MySQL的@与@@区别 @x 是 用户自定义的变量  (User variables are written as @var_name) @@x 是 global或session变量  (@@glo ...