【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

题干:

You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.

If you choose the ith door, it can either take you back to the same position where you begun in xi minutes, or can take you out of the maze after xi minutes. If you come back to the same position, you can't remember anything. So, every time you come to the beginning position, you have no past experience.

Now you want to find the expected time to get out of the maze.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of doors. The next line contains nspace separated integers. If the ith integer (xi) is positive, you can assume that the ith door will take you out of maze after xi minutes. If it's negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.

Output

For each case, print the case number and the expected time to get out of the maze. If it's impossible to get out of the maze, print 'inf'. Print the result in p/q format. Where p is the numerator of the result and q is the denominator of the result and they are relatively prime. See the samples for details.

Sample Input

3

1

1

2

-10 -3

3

3 -6 -9

Sample Output

Case 1: 1/1

Case 2: inf

Case 3: 18/1

题目大意:

在n个门前选择一扇门出去, 然后如果第i扇门的 Xi值是正的话,你会花费Xi时间后出去 , 如果Xi是负数的话你会花费-Xi时间后回到老地方,并且忘记了刚才的选择, 选择一扇门的概率是等概的。求出去的期望。

解题报告:

设任意选择一个门,选择到正权值的概率是P1,负权值的概率是P2,正权值的平均值是v1,负权值的平均值是v2,设要求的期望是E,那么,就可以找到一个方程来:

E = P_1 \times v_1 + P_2 \times (v_2 + E)

其实是找到了和如何回归到原来的状态,从而可以写出方程。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int a[MAX];
int main()
{int t,iCase=0;cin>>t;while(t--) {int zheng=0,fu=0,z=0,f=0,n;scanf("%d",&n);for(int i = 1; i<=n; i++) {scanf("%d",a+i);if(a[i] > 0) zheng += a[i],z++;else fu += -a[i],f++;;}printf("Case %d: ",++iCase);if(z == 0) {puts("inf");continue;}//  zheng+fu/zint g = __gcd(zheng+fu,z);printf("%d/%d\n",(zheng+fu)/g,z/g);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440050.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

由浅到深理解ROS(2)

ROS文件系统 用户可以直接参看官网&#xff1a; http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念&#xff1a;Package和Manifest&#xff0c;即包和清单文件。 &#xff08;1&#xff09;Package是组织ROS代码的最基本单位&…

Django员工管理系统

Django员工管理系统&#xff08;ems&#xff09; 需求分析&#xff1a; 1.实现管理员的注册、登陆页面注册&#xff1a;用户名、真实名字、密码、确认密码、性别、验证码用户名需要判断是否合法、是否存在&#xff0c;loading图片提示密码和确认密码是否相同&#xff0c;load…

(4)栈

目录 栈的知识点&#xff1a; 栈的操作&#xff1a; 创建栈&#xff1a; 压栈&#xff1a; 遍历栈&#xff1a; 判断栈是否为空&#xff1a; 出栈 清空栈 这篇笔记是根据郝斌老师的上课讲义整理而得。 栈的知识点&#xff1a; 栈的定义&#xff1a;一种可以实现"…

【LightOJ - 1104】Birthday Paradox(概率,思维)

题干&#xff1a; Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party…

爬虫小记

中国商标网 找到正确的入口 在此页面加入全部data数据获取xhr请求包

由浅到深理解ROS(3)-命名空间

全局命名空间&#xff1a; /rosout前面的反斜杠“/”表明该节点名称属于全局命名空间。之所以叫做全局名称因为它们在任何地方&#xff08;包括代码、命令行工具、图形界面工具等的任何地方&#xff09;都可以使用。无论这些名称用作众多命令行工具的参数还是用在节点内部&…

(5)队列

目录 队列知识点&#xff1a; 循环队列&#xff1a; 队列的操作&#xff1a; 创建队列&#xff1a; 判断队列是否已满&#xff1a; 入队&#xff1a; 遍历队列&#xff1a; 判断队列是否为空&#xff1a; 出队&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得&…

【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)

题干&#xff1a; Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D. In each turn he randomly choose…

使用matplotlib进行简单的数据展示

import numpy as np import matplotlib.pyplot as plt# 解决中文乱码 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号# 建立一个坐标系 plt.subplot(1, 1, 1) # 指明x和y值 x np.array([1, 2…

(6)递归

目录 递归知识点&#xff1a; 递归操作&#xff1a; 阶乘的循环实现&#xff1a; 阶乘的递归实现&#xff1a; 经典的汉诺塔问题&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得。 递归知识点&#xff1a; 定义&#xff1a;一个函数自己直接或间接调用自己 。 …

由浅到深理解ROS(4)

消息和消息类型 节点能相互传递消息&#xff0c;节点之间通信也是基于消息。消息类型也就是数据类型数据类型&#xff0c;理解消息的类型很重要&#xff0c;因为它决定了消息的内容。也就是说&#xff0c;一个话题的消息类型能告诉你该话题中每个消息携带了哪些信息&#xff0…

【CodeForces - 1197C】Array Splitting(水题)

题干&#xff1a; You are given a sorted array a1,a2,…,ana1,a2,…,an (for each index i>1i>1 condition ai≥ai−1ai≥ai−1holds) and an integer kk. You are asked to divide this array into kk non-empty consecutive subarrays. Every element in the array…

Django项目部署在Centos7

把Django项目部署在Centos7下 先有一个Django项目 一个员工管理系统&#xff08;ems&#xff09; 员工管理系统 使用VMware创建一个虚拟的Linux系统 Centos7下安装MySql5.7 详细安装 Centos7下安装Python3.5 详细安装 Centos7下安装Django2.0.6 - 安装数据库驱动&#xf…

(7)树

目录 树的知识点&#xff1a; 定义&#xff1a; 专业术语&#xff1a; 操作(重点): 树的操作&#xff1a; 创建二叉树&#xff1a; 先序遍历&#xff1a; 中序遍历&#xff1a; 后序遍历&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得。 树的知识点&#xf…

由浅到深理解ROS(5)- launch启动文件的理解与编写

ROS提供了一个同时启动节点管理器&#xff08;master&#xff09;和多个节点的途径&#xff0c;即使用启动文件&#xff08;launch file&#xff09;。事实上&#xff0c;在ROS功能包中&#xff0c;启动文件的使用是非常普遍的。任何包含两个或两个以上节点的系统都可以利用启动…

【CodeForces - 1199C】MP3(思维,离散化)

题干&#xff1a; One common way of digitalizing sound is to record sound intensity at particular time moments. For each time moment intensity is recorded as a non-negative integer. Thus we can represent a sound file as an array of nn non-negative integers…

Centos7下安装MySql

1、安装方式 1.1 方式一 yum安装&#xff08;推荐使用该方式&#xff09; 在CentOS中默认安装有MariaDB&#xff0c;这个是MySQL的分支 如果必须要安装MySQL&#xff0c;首先必须添加mysql社区repo通过输入命令&#xff1a; rpm -ivh http://dev.mysql.com/get/mysql-commu…

(8)排序

排序&#xff1a; 排序和查找的关系&#xff1a;排序是查找的前提&#xff0c;排序是重点。 冒泡 插入 选择 归并排序 快速排序&#xff1a;一次确定一个数的位置 #include <stdio.h>void QuickSort(int *a, int low, int high) {int pos;if(low < high){pos FindP…

由浅到深理解ROS(6)-坐标转换

转自 ROS 中对于多坐标系的处理是使用树型表示&#xff0c;在机器人自主导航中&#xff0c;ROS会构建这几个很重要的坐标系&#xff1a; base_link: 一般位于tf tree的最根部&#xff0c;物理语义原点一般为表示机器人中心&#xff0c;为相对机器人的本体的坐标系。(base_foot…

Centos7下安装Python3.5

1、安装依赖 - yum -y install python-devel openssl-devel bzip2-devel zlib-devel expat-devel ncurses-devel sqlite-devel gdbm-devel xz-devel tk-devel readline-devel gcc - yum -y groupinstall "Development tools"如上两步&#xff0c;汇总安装了p…