// uva live 4731
//
// 状态很好想:
// d(i,j)表示前i个网络分为j组所得到的数学期望的最小值
// 转移方程:
// d(i,j) = min(d(k,j-1)+cost);
// cost由题目给出的条件可知cost = (k+1...i)段的概率和 * i;
//
// 注意:
// 1)肯定概率大的网络分在前面,这样在后面的话,这个大的概率出现在
// 后面的机会就会小。因为每个概率都会至少要算一次,所以先算大的
// 可以尽量减少后面再算大的。
// 2 )初始化的时候只有d(0,0)才是0.0,其他都是inf,因为任意一个不是0的i
// 都不可能有d(i,0)这种状况出现;// 感悟:
// 总的来说,并不算是很难,只要题目意思看懂了就还行,一直在纠结
// 这个公式是怎么证明的,最后本人实在太笨,只能按照题目给的公式
// 直接硬上喽,如果有人能解答我的疑惑,$2.56小子双手奉上,并致以
// 最为诚挚的感谢#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L);
int n,w;
const int maxn = 180;
double d[maxn][maxn];
bool vis[maxn][maxn];
int x[maxn];
double s[maxn];bool cmp(int x,int y){return x>y;
}double dp(int num,int wi){if (vis[num][wi])    return d[num][wi];vis[num][wi] = 1;if (wi==0 || num == 0)  return d[num][wi];for (int i=0;i<=num;i++)d[num][wi] = min(d[num][wi],dp(i,wi-1)+(s[num]-s[i]) * num);return d[num][wi];
}void print(){for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){cout << d[i][j] << " ";}cout << endl;}
}void prin(){for (int i=1;i<=n;i++)cout << s[i] << " ";cout << endl;
}void init(){scanf("%d%d",&n,&w);double sum = 0.0;for (int i=1;i<=n;i++){scanf("%d",&x[i]);sum +=x[i];}sort(x+1,x+n+1,cmp);s[0] = 0.0;for (int i=1;i<=n;i++){s[i] = s[i-1] + x[i]/sum;}//prin();memset(vis,0,sizeof(vis));for(int i=0;i<=n;i++)for (int j=0;j<=n;j++){d[i][j] = 999999999;}d[0][0]=0.0;printf("%.4lf\n",dp(n,w));//print();//puts("");
}int main() {int t;//freopen("1.txt","r",stdin);scanf("%d",&t);while(t--){init();}return 0;
}

uva live 4731 Cellular Network 线性dp相关推荐

  1. UVa在线比赛单题汇总-----DP专题

    动态规划基础 例题 LA 3882 UVa 3882 - And Then There Was One 递推------------无力orz UVa 10635 10635 - Prince and ...

  2. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

  3. codeforces 702C Cellular Network

    C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  4. 0x51.动态规划 - 线性DP(习题详解 × 10)

    目录 0x51.动态规划 - 线性DP 0x51.1 LIS问题 Problem A. 登山 (最长下降子序列) Problem B. 友好城市(思维) Problem C. 最大上升子序列和 0x5 ...

  5. UVA11584 划分成回文串 Partitioning by Palindromes(线性DP划分+DP判断回文串)

    整理的算法模板合集: ACM模板 依旧是线性DP 我们使用闫氏DP分析法 总体DP转移的时间复杂度为O(n2)O(n^2)O(n2). 但是这里牵扯到判断 i\tt ii 到 j\tt jj 是否为回 ...

  6. 洛谷P2401 不等数列(线性DP)

    本题使用的是线性DP.就是DP数组难以思考,这里我直接给出 dp[i][j]:表示 1 ~ i 这 i 个数 , 其中j 个 " < " 有几种方法 假设我们已经把 n - ...

  7. CodeForces - 1096D Easy Problem(线性dp)

    题目链接:点击查看 题目大意:给出一个字符串,每个字符都有一个权值,现在需要删除权值和最少的字符,满足字符串中不再含有子序列"hard" 题目分析:线性dp,但我不会,看着题解写的 ...

  8. CodeForces - 456C Boredom(线性dp)

    题目链接:点击查看 题目大意:给出一个由n个数字组成的数列,现在给出规则是,每次选择数列中的一种数字 x,选择后的贡献为 x,不过操作后会删除掉所有数值为 x + 1 和 x - 1 的数,现在问如何 ...

  9. POJ - 1050 To the Max(最大连续子段和,线性dp)

    题目链接:点击查看 题目大意:给出一个n*n的矩阵,每个点都有一个权值,现在要从中选取一个子矩阵要求权值和最大,问这个最大权值和是多少 题目分析:因为是要求子矩阵的权值和最大的问题,我们可以直接维护一 ...

最新文章

  1. Memcached、Redis OR Tair
  2. angular5.0封装underscore常用pipe并发布到npm全套流程
  3. 搞定所有的跨域请求问题
  4. 华为云API Explorer开发者生态平台正式上线
  5. jQuery 历史版本
  6. C++/Qt文件写入读取速度比较
  7. python微信开发入门_python tornado微信开发入门代码
  8. vue组件之间的参数传递
  9. LINUX删除指定子目录下所有指定文件名
  10. 利用matlab使用窗函数,MATLAB中的窗函数
  11. LibreELEC(kodi)安装 IPTV
  12. vue html if,vue中v-if使用方法详解
  13. 工具一栏,就是小锤子右边的一排都没亮
  14. Laravel框架使用maatwebsite/excel导出自动换行
  15. Luogu3426 [POI2005]SZA-Template (KMP)(未完成)
  16. 【BSP视频教程】STM32H7视频教程第3期:整体捋顺STM32H7的HAL库和LL库的框架,再配合寄存器造轮子找到更适合自己的玩法(2022-01-21)
  17. MaxCompute SQL示例解析
  18. 如何培养《未来架构师》(2)
  19. JS 异步编程六种方案
  20. 苹果笔记本android_studio快捷键

热门文章

  1. 贴代码(ubuntu Pastebin)
  2. 无限法则服务器选择吗,Switch OLED来了 周边如何选
  3. 「CodePlus 2018 3 月赛」白金元首与克劳德斯
  4. python+selenium实现脉脉网站爬取用户朋友圈,自动评论
  5. 自动重启某个网卡脚本(根据IP确定网卡)
  6. 显示桌面图标放到任务栏
  7. 三种搭建网站的方法(基于域名,IP,端口)
  8. IntelliJ IDEA 在线破解
  9. 民族证券午间看盘:短线有望企稳,把握反弹机会
  10. Shopee招聘计算机视觉算法工程师