Sicily 1919. Build the Tower

本文介绍了一个基于Hanoi Tower玩具的游戏规则,并通过算法计算了游戏平均能持续多少轮的问题。玩家在一个堆栈上放置尺寸各异的圆盘,通过特定规则选择下一个放置的蓝色圆盘或移除红色特殊圆盘,直到游戏结束。

1919. Build the Tower

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

 One of our judges gets bored! Fortunately a toy named Hanoi Tower is there, which may help him to kill time. You know it well that the toy consists of three stacks and some disks. The size of each disk can be indicated by its radius and thickness. Tired of the usual ways of playing the toy, he devises his new rule. He uses only one stack in the game. Before the game, he colors one disk red, the others blue. Starting with an empty peg, the game runs on turn by turn. Each turn, he puts all the valid blue disks as well as the special red disk, which is not on the stack at the time, into a black box and picks out one blindly. By this way he can select one of the disks in the box with equal possibility. Which disk is so-called valid? It is decided by comparing its radius to the radius of the top-most disk on the stack. If its radius is strictly less than the top-most disk's, it's considered valid. If no disk is on the stack at that time, all the disks are considered valid. If a disk is chosen, our cute judge then puts it on the top of the stack, with one exceptional case, the red one. If the special red disk is chosen, instead of putting it onto the stack, the disk currently at the top of the stack should be removed. Wired, isn't it?

The stack's height is given. The game ends when after some turns the total thickness of all the disks on the stack is greater than the stack's height, or the red disk is picked but the stack is empty. By then, the guy may get bored again and start to complain about his tedious work (doesn't he suppose to be a judge?) Could you help us to figure out, after how many turns expectedly will the game last?

Input

 Input contains no more than 60 cases. There is one empty line between two cases. Your program should process to the end of file.

For each test case, the first line contains two integers, N and H (1 ≤ N, H ≤ 100) . The toy contains N + 1 disks, and the stack's height is H . Then N lines follow. Each contains two integers r1 and h1 (1 ≤ r1, h1 ≤ 100) , describing one blue disk by giving its radius and thickness.

Output

 For each case, output one line containing the expected turns the game lasts, to three (3) digits after the decimal point. If the answer is greater than 18000 (that's the 5 hours contest time measured in seconds), output a line ``INF" instead (without quotation).

Sample Input

2 2 
1 1 
2 2 

1 1 
1 2

Sample Output

3.333 

1.000

// Problem#: 1919
// Submission#: 3591565
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <algorithm>
using namespace std;

const int MAXN = 101;
const int MAXH = 101;
const int MAXR = 102;

int N, H;

struct Disk {
    int r, h;
    bool operator < (Disk x) const {
        return r < x.r;
    }
}disk[MAXN];

double a[MAXR][MAXH], b[MAXR][MAXH];

int input() {
    if (scanf("%d%d", &N, &H) != 2) return 0;
    for (int i = 0; i < N; i++) scanf("%d%d", &disk[i].r, &disk[i].h);
    return 1;
}

int main() {
    while (1) {
        if (!input()) break;
        sort(disk, disk + N);
        double sa, sb;
        for (int h = H; h >= 0; h--) {
            sa = sb = 0.0;
            for (int r = 1, c = 0; r <= disk[N - 1].r + 1; r++) {
                for (; c < N && disk[c].r < r; c++) {
                    int rt = disk[c].r;
                    int ht = disk[c].h + h;
                    if (ht > H) continue;
                    sa += a[rt][ht];
                    sb += b[rt][ht];
                }
                a[r][h] = 1 / (c + 1 - sa);
                b[r][h] = (c + 1 + sb) / (c + 1 - sa);
            }
        }
        double ans = b[disk[N - 1].r + 1][0];
        printf(ans > 18000.0 ? "INF\n" : "%.3lf\n", ans);
    }
    return 0;
}                                 


乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值