The secret code

Input file: stdinOutput file: stTime limit: 1 sec

Memory limit: 256 Mb
After returning from the trip, Alex was unpleasantly surprised: his porch door had a new combination lock. Alex
can not get into his house! Code lock contains N disks, each of which can be in one of M positions. There
is only one correct position. Alex thoroughly inspected discs and by fingerprints and scratches determined the
probability of each position for each disk. Now Alex has K attempts to guess the correct code: if he fails, his
vigilant neighbors will call the police, and Alex will have a hard time persuading cops that he is not a thief, but
tried to get home. Help Alex to calculate the maximum probability of getting home, not to the police.
Limit
1 ≤ N ≤ 100
1 ≤ M ≤ 20
1 ≤ K ≤ 100
0 ≤ P ij ≤ 100
Input
The first line of input file contains three integers: N, M Рё K.
Next N lines contain M integers each: j-th number of the i-th line (P ij ) — the probability of a situation where
i-th disc’s correct position is j. Given that
P M
j=1 P ij
= 100.
Output
Print a single number — Alex’s chances to guess the correct code in time. Output result should have an absolute
percentage error not grater than 10 −7 .
Example
stdin

2 2 1
50 50
10 90
3 5 4
10 15 20 25 30
1 2 3 4 90
100 0 0 0 0

stdout

0.45
0.81

题目大意:

   n个数列,每个数列取一个数,得到这些数的乘积,求前k个最大的乘积。

多谢syx的指导!

解题思路:

  对每个数列排序后。

  首先,第一大的自然是所有数列最大值乘积。

  接着,将最大乘积加到ans上,然后将这个乘积能够达到的所有下一个状态均加入优先队列中。至于保存状态,每个状态只需保存其在每个数列的取到第几个数的指针即可,显然下一个状态是在该状态之后,并且出现在某一个指针向后移位。故将所有下一个状态加入优先队列。

  然后,当找到第k个或者是空队列时退出循环,否则返回上一步。

  最后,输出答案。

实现方法,直接使用STL中的priority_queue,若数据量加大,可惜优先队列不能删除尾节点,不过可利用最大最小堆保存前k个即可。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y)) using namespace std; struct State{
int head[];
double x;
bool operator<(const State &b) const
{
return x<b.x;
}
}; double a[][];
int n,m,k;
priority_queue<State> q;
bool cmp(double x, double y)
{
return x>y;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
scanf("%lf",&a[i][j]);
a[i][j]/=;
}
sort(a[i],a[i]+m,cmp);
} State tmp;
tmp.x=1.0;
for(int i=; i<n; i++)
{
tmp.head[i]=;
tmp.x*=a[i][];
}
q.push(tmp); double ans=0.0;
for(int i=; i<k&&!q.empty(); i++)
{
tmp=q.top();
q.pop();
ans+=tmp.x;
for(int j=; j<n; j++)
if(tmp.head[j]<m-)
{
if(a[j][tmp.head[j]+]==) continue;
tmp.x=tmp.x / a[j][tmp.head[j]] * a[j][tmp.head[j]+];
tmp.head[j]++;
q.push(tmp);
tmp.x=tmp.x / a[j][tmp.head[j]] * a[j][tmp.head[j]-];
tmp.head[j]--;
}
} printf("%.8f\n",ans);
return ;
}

The secret code的更多相关文章

  1. Android Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系 ...

  2. hdu.1111.Secret Code(dfs + 秦九韶算法)

    Secret Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. Android 编程下的 Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入 *#*#4636#*#* 等字符就会弹出一个界面显示手机相关的一些信息,这个功能在 Android 中被称为 Android Secret Code, ...

  4. [swustoj 679] Secret Code

    Secret Code 问题描述 The Sarcophagus itself is locked by a secret numerical code. When somebody wants to ...

  5. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  6. HDU 1111 Secret Code(数论的dfs)

    Secret Code Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  7. Secret Code

    Secret Code 一.题目 [NOIP模拟赛A10]Secret Code 时间限制: 1 Sec  内存限制: 128 MB 提交: 10  解决: 6 [提交][状态][讨论版] 题目描述 ...

  8. 【微信】根据appid, secret, code获取用户基本信息

    function getUserInfo(){ $appid = "yourappid"; $secret = "yoursecret"; $code = $_ ...

  9. 洛谷P3102 [USACO14FEB]秘密代码Secret Code

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

随机推荐

  1. bndtools教程

    使用工具编程的确能给人们带来很多便利,但是在不会用之前,且缺乏相应的中文资料让你去了解时,真是一种折磨,同时也是一种挑战. bndTools其实就是用来开发OSGi的一个工具,它为开发提供了便利,具体 ...

  2. MYSQL远程登录权限设置

    Mysql默认关闭远程登录权限,如下操作允许用户在任意地点登录: 1. 进入mysql,GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY ...

  3. 关于MDK中:RO-data、RW-data、ZI-data

    最近在LPC2109上调试ENC28J60,协议栈使用的是UIP,刚开始用的telnet服务,能够正常编译运行.然后换成webserver提示: enc28j60.axf: Error: L6406E ...

  4. windows android studio 编译Jni动态库

    项目需要,折腾了半天搞定windows android studio环境编译Jni动态库,现记录下来. 准备安装环境: 1. android studio 下载地址是http://www.androi ...

  5. R语言学习笔记 之 可视化地研究参议员相似性

    基于相似性聚类 很多时候,我们想了解一群人中的一个成员与其他成员之间有多么相似.例如,假设我们是一家品牌营销公司,刚刚完成了一份有潜力新品牌的研究调查问卷.在这份调查问卷中,我们向一群人展示了新品牌的 ...

  6. PHP Cookie处理函数

    (o゜▽゜)o☆[BINGO!] ok,我们先看看cookie是什么东东? cookie是服务器留在客户端的用于识别用户或者存储一些数据的小文件(注意,session存储在服务器端,这是两者的区别之一 ...

  7. "Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b7

    1.错误背景 系统安装了.net framework4.0.4.5,项目先使用VS2013(4.5)开发,后来又重新用VS2010开发(4.0),运行时出现这个错误 2.错误原因 In .Net 4. ...

  8. 用户 IIS APPPOOL\DefaultAppPool 登录失败的解决方法

    解决方案  1.先查看你的网站使用的是哪个应用程序池 2.设置应用程序池的选项 3.标识选项 选LocalSystem或NetworkService就可以解决问题

  9. CSS属性--过渡(transtion)

    首先介绍一下transition的属性取值: transition-property : 主要设置对象中的参与过渡的属性,包括(border-color,background-color,color) ...

  10. matlab中读取txt数据文件(txt文本文档)

    matlab中读取txt数据文件(txt文本文档) 根据txt文档不同种类介绍不同的读取数据方法 一.纯数据文件(没有字母和中文,纯数字) 对于这种txt文档,从matalb中读取就简单多了 例如te ...