POJ 1018 Communication System 题解
本题一看似乎是递归回溯剪枝的方法。我一提交,结果超时。
然后又好像是使用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 题解的更多相关文章
- POJ 1018 Communication System(树形DP)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- POJ 1018 Communication System (动态规划)
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- poj 1018 Communication System
点击打开链接 Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21007 Acc ...
- poj 1018 Communication System 枚举 VS 贪心
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21631 Accepted: ...
- POJ 1018 Communication System(贪心)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- poj 1018 Communication System (枚举)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22380 Accepted: ...
- POJ 1018 Communication System(DP)
http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...
- POJ 1018 Communication System 贪心+枚举
看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...
- poj 1018 Communication System_贪心
题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...
随机推荐
- 总结:PHP值得注意的几个问题
1.除了变量和常量区分大小写外,其他的标识符不区分大小写(例如关键字,类名,函数名等): 2. >>>是无符号右移,不管第一位是0还是1,右移后前面都是补0: 3.在函数中传递数组, ...
- K-means算法的优缺点
K-means算法的优缺点 优点:原理简单,实现容易 缺点: 收敛较慢 算法时间复杂度比较高 \(O(nkt)\) 不能发现非凸形状的簇 需要事先确定超参数K 对噪声和离群点敏感 结果不一定是全局最优 ...
- DefaultTransactionStatus源码
package org.springframework.transaction.support; import org.springframework.transaction.NestedTransa ...
- 【mysql优化 2】索引条件下推优化
原文地址:Index Condition Pushdown Optimization 索引条件下推(ICP:index condition pushdown)是mysql中一个常用的优化,尤其是当my ...
- 【java基础 17】集合中各实现类的性能分析
大致的再回顾一下java集合框架的基本情况 一.各Set实现类的性能分析 1.1,HashSet用于添加.查询 HashSet和TreeSet是Set的两个典型实现,HashSet的性能总是比Tree ...
- 九度oj 题目1456:胜利大逃亡
题目描述: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius ...
- HUST——1103Party(拓扑排序+个人见解)
1103: Party Time Limit: 2 Sec Memory Limit: 64 MB Submit: 11 Solved: 7 Description N students were ...
- Installing Metasploit Framework on Ubuntu 14.04 LTS and Debian 7
原文链接:http://www.darkoperator.com/installing-metasploit-in-ubunt/ This Guide covers the installation ...
- java面试题之如何实现处理线程的返回值?
有三种实现方式: 主线程等待法: 使用Thread类的join方法阻塞当前线程以等待子线程处理完毕: 通过Callable接口实现,通过FutureTask 或者线程池:
- [暑假集训--数位dp]LightOj1205 Palindromic Numbers
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...