lightoj 1381 - Scientific Experiment dp

1381 - Scientific Experiment

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://www.lightoj.com/volume_showproblem.php?problem=1381

Description

John wants to be a scientist. A first step of becoming a scientist is to perform experiment. John has decided to experiment with eggs. He wants to compare the hardness of eggs from different species. He has decided to use a nearby large multi-storied building for this purpose. For each species he will try to find the highest floor from which he can drop the egg and it will not break. The building has (n+1) floors numbered from 0 to n. John has a book from which he knows that

  1. If an egg is dropped from the topmost floor, it will surely break.
  2. If an egg is dropped from floor 0, it will not break.
  3. The eggs of same species are of same strength. That means if any egg breaks when dropped from the kth floor; all the eggs of that species will break if dropped from kth floor.
  4. If an egg is unbroken after dropping it from any floor, it remains unharmed, that means the strength of the egg remains same.

Unfortunately John has a few problems:

  1. He can only carry one egg at a time.
  2. He can buy eggs from a shop inside the building and an egg costs x cents.
  3. To enter the building he has to pay y cents if he has no egg with him and z cents if he carries an egg with him.
  4. After dropping an egg, John must go outside the building to check whether it‘s broken or not.
  5. He does not want to waste any egg so he will not leave any unbroken egg on the ground. But if an egg is broken, he leaves it there.
  6. If he has an intact egg at the end, he can sell it for x/2 cents. He does not need to enter the building to sell the egg.

These problems are not going to tame John‘s curious mind. So he has decided to use an optimal strategy and minimize his cost in worst case. As John is not a programmer, he asked your help.

Input

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

Each case starts with a line containing four integers n x y z as described in the statement. You may assume that 1 < n ≤ 1000 and 1 ≤ x, y, z ≤ 105 and x is even.

Output

For each test case, print the case number and the minimized worst case cost.

Sample Input

7

4 2 998 1000

16 2 1000 1000

16 1000 1 1

4 1000 1 1

7 2 2 2

9 2 1 100

11 2 100 1

Sample Output

Case 1: 2000

Case 2: 4008

Case 3: 1015

Case 4: 1003

Case 5: 10

Case 6: 24

Case 7: 111

HINT

For case 1, John knows that the egg will break if dropped from 4th floor, but will not break if dropped from 0th floor. An optimal solution may be

  1. John enters the building without any egg (¢998).
  2. John buys an egg (¢2).
  3. John drops an egg from 2nd floor. John goes out and checks the egg.
    1. If it breaks,

      1. i.      John again enters the building without any egg (¢998) and buys an egg there ¢2.
      2. ii.      He drops the egg from 1st floor.
        1. If it does not break then answer to his problem is 1 and he can sell the egg for ¢1. So his final cost in ¢1999.
        2. If it breaks then the answer to his problem is 0th floor and his final cost is ¢2000.
  4. If it does not break
    1. i.      John enters the building with the egg (¢1000).
    2. ii.      He drops it from 3rd floor.
      1. If it does not break then answer to his problem is 3 and he can sell the egg for ¢1. So his final cost in ¢1999.
      2. If it breaks then the answer to his problem is 2 and final cost is ¢2000.

So, using this strategy, his worst case cost is ¢2000.

题意

一个评估蛋的硬度方法是测量蛋从多高摔下来会碎。现在佳佳想以楼层高度作为考量依据评估蛋的 硬度。如果蛋从i楼上掉下来会碎,而i-1不会,那么蛋的硬度为i。高为n层的实验楼里面有蛋卖,一个X元。佳佳开始没有蛋,并且他只能随身携带一个蛋, 不带蛋进楼需要Y元,带蛋需要Z元,做完试验之后如果还有一个蛋,可以卖掉得X/2元(卖蛋不需要进楼)。佳佳把鸡蛋扔出去后,会出楼检查蛋的情况。如果 蛋扔下后没有碎掉,佳佳一定会把蛋捡起,然后进楼,如蛋碎掉了,佳佳就不会管它。 佳佳想知道在最糟糕的情况下,测出蛋的硬度最少需要花费多少钱。

题解:

dp[i]表示在楼层高度为i的情况下,检测出碎蛋的位置的最小最差花费是多少

然后转移就好,注意这个dp[i]只和楼层高度有关!

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************
ll dp[maxn];
ll d1,d2;
int n,x,y,z;
int main()
{
    int t=read();
    for(int cas=1;cas<=t;cas++)
    {
        n=read(),x=read(),y=read(),z=read();
        dp[0]=dp[1]=0;
        for(int i=2;i<=n;i++){
            dp[i]=inf;
            for(int j=1;j<i;j++)
            {
                d1=dp[j]+x+y;
                d2=dp[i-j]+z;
                if(i-j==1)
                    d2=y+x/2;
                dp[i]=min(dp[i],max(d1,d2));
            }
        }
        printf("Case %d: %lld\n",cas,dp[n]);
    }
}
时间: 2024-10-15 20:02:12

lightoj 1381 - Scientific Experiment dp的相关文章

Lightoj 1044 - Palindrome Partitioning (DP)

题目链接: Lightoj  1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不是回文串处理出来,然后用dp[i] 表示 从起点到串i的位置的最少分割次数,然后结合处理出来的回文串转移一下即可! 还是好蠢哦!自己竟然感觉是一个区间DP,但是n又那么大,完全不是区间DP的作风啊! 1 #include <cmath> 2 #include <cstdio> 3 #i

LightOJ 1030 【概率DP求期望】

借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步,然后把那个格子的金子拿走: 如果扔出的骰子+所在位置>n,就重新扔,直到在n: 问取走这些值的期望值是多少 解析: [1] [2] [3][4] [5] [6] [7] [8] [9] //格子和值都是一样,所以下述的话,值就是格子,格子就是值... 比如这样的9个格子,我们总底往上来 对于第9个格

LightOJ - 1246 - Colorful Board(DP)

链接: https://vjudge.net/problem/LightOJ-1246 题意: You are given a rectangular board. You are asked to draw M horizontal lines and N vertical lines in that board, so that the whole board will be divided into (M+1) x (N+1) cells. So, there will be M+1 ro

留学生R经管统计作业代写代做、Stat/ME代写

留学生R经管统计作业代写代做.Stat/ME代写Requirements for Stat/ME 424 Class Project? This is an individual project. You cannot join forces with anyone else in the class,but (if you wish) you can collaborate with someone in your lab or research team.? The basic requir

6.24我要移民!

10. 备忘录/报告:P.39• To: All club members• From: Li Ming• Date: December 23, 2017• Subject: Creating Green Culture and Building Low-Carbon Campus• We are going to launch a series of activities to promote low-carbon lifestyle, or more specifically to advo

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

Lightoj 1071 - Baker Vai (双线程DP)

题目连接: http://lightoj.com/volume_showproblem.php?problem=1071 题目大意: 一个n*m的格子,Baker Vai要从(1,1)到(n,m)再回到(1,1),每到一个格子可以收集格子上的数字(每个格子只能走一次,(1,1)这个格子除外),问最终搜集的数字之和最大为多少? 解题思路: 可以把题目转化为求两个对象同时从(1,1)出发到(n,m)途中不能相遇,状态转移的时候可以用dp[step][x][y],step代表当前步数,x,y分别代表两

URAL 1203 Scientific Conference(贪心 || DP)

Scientific Conference 之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校,补多校的题目,刷一下一直薄弱的DP.多校如果有计算几何一定要干掉-.- 题意:给你N个报告会的开始时间跟结束时间,问你做多可以听几场报告会.要求报告会之间至少间隔为1. 思路:其实是个活动安排问题,可以用贪心也可以用DP,贪心写起来会比较简单一些,因为练习DP,所以又用DP写了一遍. 贪心的话就是一个很简单的活动选择问题,从结束时间入手,找每次的最优选择. 1 struct n

hdu 5693 &amp;&amp; LightOj 1422 区间DP

hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3组合成. 区间DP,用dp[i][j]表示在i到j之间可以删除的最大数,枚举区间长度,再考虑区间两端是否满足等差数列(这是考虑两个数的),再i到j之间枚举k,分别判断左端点右端点和k是否构成等差数列(还是考虑两个数的),判断左端点,k,右端点是否构成等差数列(这是老驴三个数的) 1 #include