zoukankan      html  css  js  c++  java
  • 【动态规划】HDU 5781 ATM Mechine

    题目链接:

      http://acm.hdu.edu.cn/showproblem.php?pid=5781

    题目大意:

      一个人有[0,K]内随机的钱,每次可以随意取,但是不知道什么时候取完,取钱超过剩余额度会警告一次,最多警告不能超过W。求期望取出钱的次数。

    题目思路:

      【动态规划】

      二分居然错了。。。看来二分出的答案不一定最优。。起码第三个样例过不去。

      f[i][j]表示钱在[0,i]区间内,警告次数不超过j的期望取钱次数。那么取一次钱k相当于把钱分成两块,[0,k]和[k+1,i],即[0,k]和[0,i-k]

      枚举k即可推出答案。

      f[i][j]=min(f[i][j],(f[i-k][j]*(i-k+1)+f[k-1][j-1]*k)/(i+1)+1);

     1 //
     2 //by coolxxx
     3 //#include<bits/stdc++.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<map>
     9 #include<memory.h>
    10 #include<time.h>
    11 #include<stdio.h>
    12 #include<stdlib.h>
    13 #include<string.h>
    14 //#include<stdbool.h>
    15 #include<math.h>
    16 #define min(a,b) ((a)<(b)?(a):(b))
    17 #define max(a,b) ((a)>(b)?(a):(b))
    18 #define abs(a) ((a)>0?(a):(-(a)))
    19 #define lowbit(a) (a&(-a))
    20 #define sqr(a) ((a)*(a))
    21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    22 #define mem(a,b) memset(a,b,sizeof(a))
    23 #define eps (1e-8)
    24 #define J 10
    25 #define mod 1000000007
    26 #define MAX 0x7f7f7f7f
    27 #define PI 3.14159265358979323
    28 #define N 2004
    29 using namespace std;
    30 typedef long long LL;
    31 int cas,cass;
    32 int n,m,lll,ans;
    33 double f[N][24];
    34 void init()
    35 {
    36     int i,j,k;
    37     for(i=0;i<18;i++)f[0][i]=0;
    38     for(i=1;i<N;i++)f[i][0]=2000000000;
    39     for(j=1;j<18;j++)
    40     {
    41         for(i=1;i<N;i++)
    42         {
    43             f[i][j]=2000000000;
    44             for(k=1;k<=i;k++)
    45                 f[i][j]=min(f[i][j],(f[i-k][j]*(i-k+1)+f[k-1][j-1]*k)/(i+1)+1);
    46         }
    47     }
    48 }
    49 int main()
    50 {
    51     #ifndef ONLINE_JUDGE
    52 //    freopen("1.txt","r",stdin);
    53 //    freopen("2.txt","w",stdout);
    54     #endif
    55     int i,j,k;
    56     init();
    57 //    for(scanf("%d",&cas);cas;cas--)
    58 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    59 //    while(~scanf("%s",s+1))
    60     while(~scanf("%d",&n))
    61     {
    62         scanf("%d",&m);
    63         printf("%.6lf
    ",f[n][min(m,17)]);
    64     }
    65     return 0;
    66 }
    67 /*
    68 //
    69 
    70 //
    71 */
    View Code
  • 相关阅读:
    9th week
    8th Week 2
    8th Week 1
    课后作业-阅读任务-阅读提问-4
    2017-11-30-构建之法:现代软件工程-阅读笔记
    《团队-OldNote-项目总结》
    个人编程作业2-课程总结
    《团队-Oldnote-最终程序》
    课后作业-阅读任务-阅读提问-3
    《20171102-构建之法:现代软件工程-阅读笔记》
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5815372.html
Copyright © 2011-2022 走看看