uva 1456 - Cellular Network 蜂窝网络 概率DP
2016-01-20 20:25
585 查看
题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4202
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source£ºuva 1456 - Cellular Network
* @type: dp
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=100 ;
//const int maxV=12 ;
int T,n,w;
int a[maxn+5];
double dp[maxn+5][maxn+5];
bool cmp(double x,double y)
{
return x>y;
}
int tot;
double get(int num,int x)
{
int ans=0;
for(int i=x;i<x+num;i++)
{
ans+=a[i];
}
return ans*1.0/tot;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&w);
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]),tot+=a[i];
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++) dp[0][i]=INF;/**double do not use memset 0x3f**/
dp[0][0]=0;/**if for i=1...n dp[0][i]=0 will be wrong **/
for(int ind=1;ind<=w;ind++)
{
for(int ed=ind;ed<=n;ed++)
{
dp[ind][ed]=INF;
for(int k=ind-1;k<ed;k++)
{
int num=ed-(k+1)+1;
dp[ind][ed]=min(dp[ind][ed], dp[ind-1][k]+ed*get(num,k+1) );
}
}
}
printf("%.4f\n",dp[w]
);
}
return 0;
}
/*
2
5 2
30 5 10 30 25
5 5
30 5 10 30 25
*/
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source£ºuva 1456 - Cellular Network
* @type: dp
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=100 ;
//const int maxV=12 ;
int T,n,w;
int a[maxn+5];
double dp[maxn+5][maxn+5];
bool cmp(double x,double y)
{
return x>y;
}
int tot;
double get(int num,int x)
{
int ans=0;
for(int i=x;i<x+num;i++)
{
ans+=a[i];
}
return ans*1.0/tot;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&w);
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]),tot+=a[i];
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++) dp[0][i]=INF;/**double do not use memset 0x3f**/
dp[0][0]=0;/**if for i=1...n dp[0][i]=0 will be wrong **/
for(int ind=1;ind<=w;ind++)
{
for(int ed=ind;ed<=n;ed++)
{
dp[ind][ed]=INF;
for(int k=ind-1;k<ed;k++)
{
int num=ed-(k+1)+1;
dp[ind][ed]=min(dp[ind][ed], dp[ind-1][k]+ed*get(num,k+1) );
}
}
}
printf("%.4f\n",dp[w]
);
}
return 0;
}
/*
2
5 2
30 5 10 30 25
5 5
30 5 10 30 25
*/
相关文章推荐
- Python配合BeautifulSoup读取网络图片并保存在本地
- IOS学习开发网络篇:文件上传
- Java--多线程读取网络图片并保存在本地
- 自定义tcp流媒体传输协议延时在300ms以内
- java ssl https 连接详解 生成证书
- Hyper-V 网络安装 CentOS
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片比较
- Run busybox httpd with php, sqlite
- http请求
- http error 错误码大全
- C# Http 发送Post请求实例
- tcp cubic代码分析
- AT指令建立TCP/IP 链接,并自收发数据
- TCP三次握手/四次挥手详解
- HTTP协议分析中几种常见的中文编码及其解码
- TCP长连接与短连接的区别
- moo 7 Servlet init() doget() dopost() service()方法之间的区别及应用/GenericServlet、Servlet、HttpServlet借口之间的关系
- git 错误: Unable to find remote helper for 'https'解决方法
- HTTP协议和web工作原理
- 老李分享:https协议