Regionals 2010, Asia - Kuala Lumpur

本文探讨了一个关于建筑扔鸡蛋实验的问题,通过动态规划算法求解鸡蛋的硬度测试问题。首先,解释了题目的核心概念和初始困惑,然后介绍了如何运用二分法进行初步尝试。接着,详细阐述了采用动态规划的方法来解决问题的过程,包括状态转移方程的设定和实例分析。最后,提供了关键代码实现,帮助读者理解并应用动态规划解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C题:Scientefic Experiment

题意:让你走进一个建筑扔鸡蛋,测试鸡蛋的硬度,即求出在哪层扔开始碎。进入建筑需要花费y元(不带鸡蛋),花费z元(带鸡蛋),在建筑里买鸡蛋需要花费x元。

开始一直没搞清楚题意,第一组和第二组样例出的时候,都是根据二分的思想来做的,不过对于后面的样例就不适用了。在看了xl的代码后,想到要枚举最后一次测试的层数,并且利用动态规划来搞。

f[i]表示要执行i次的最坏情况的最小花费,注意这里考虑的是从外面走进建筑的费用。

通过枚举最后一次测试的层数:

第一层和最后一层:

f[i-1]+z  (通过测试1-(i-1)层,最后一次没碎则要测试第i层)

f[i-1]+x+y(测试2-(i)层,最后一次一次碎了则要测试第1层)

其他层可以有两种方式过来:

左边:f[j-1]+z

右边:f[i-j]+x+y

2<=j<=i-1

这题和上次那题差不多,通过枚举最后一个元素的情况来进行状态转移。

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 1005
#define INF 0xfffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,s,t) for(int i=s;i<=t;i++)
#define ull unsigned long long
#define ll long long
using namespace std;
int n,x,y,z;
int f[maxn];
int main()
{
    int t,tt=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&x,&y,&z);
        int tmp;
        f[0]=0,f[1]=x+y;
        for(int i=2;i<n;i++)
        {
            f[i]=min(f[i-1]+z,f[i-1]+x+y);//第i-1次碎了测试第一层,没碎测试最后一层的最坏情况
            for(int j=2;j<=i-1;j++)//最后一次测试其他层
            {
                tmp=max(f[j-1]+z,f[i-j]+x+y);
                f[i]=min(tmp,f[i]);
            }

        }
        printf("Case %d: %d\n",tt++,f[n-1]);
    }
	return 0;
}


data: {"code": 0, "is_long_text": false, "is_longphoto": true, "keywords": ["Sean Solo", "Hongdae Guy", "Malaysia Visit", "Kuala Lumpur"], "labels": [{"labelType": "l2_labels", "labelName": "Celebrity", "labelAlias": "Celebrity", "labelId": "17aca187083a0303bf2e3ae293991cca"}, {"labelType": "l2_labels", "labelName": "Sean Solo", "labelAlias": "Sean Solo", "labelId": "b94a5fa8c816d072644d2332618c3e9d"}, {"labelType": "l2_labels", "labelName": "Hongdae Guy", "labelAlias": "Hongdae Guy", "labelId": "b1776d972d5f0cfd3ef0adc0dc56ac14"}, {"labelType": "l2_labels", "labelName": "Malaysia Visit", "labelAlias": "Malaysia Visit", "labelId": "5d191baf9b6c09345a8782ab4d52d100"}, {"labelType": "l2_labels", "labelName": "Kuala Lumpur", "labelAlias": "Kuala Lumpur", "labelId": "0b52c720e218f809fe6aea56d201f15c"}], "combineEntityList": [{"type": "schedule", "subtype": "", "detail": "{\"content\": \"Sean Solo fan meet-up and content shooting\", \"time\": \"2025-07-24T12:00:00\", \"start_time\": \"2025-07-24T12:00:00\", \"end_time\": \"2025-07-24T13:00:00\", \"location\": \"Sunway area\", \"period\": \"\", \"reminder\": \"\", \"note\": \"\"}"}], "title": "Sean Solo: Hongdae Guy in Malaysia", "title_st": 1754300624424, "sceneType": ["Articles/Paragraphs"], "summary": "- Source: thestaronline\n- Time since posted: 4d\n- Person: Sean Solo, also known as the \"Hongdae Guy\"\n- Location mentioned: Kuala Lumpur, Malaysia\n- District mentioned: Hongdae district, Seoul\n- Date of sharing location: July 24\n- Location for content shooting: Sunway area\n- Catchphrases: \"OPEN MINDEU?\"\n- Call to action for more stories: fb.com/thestaronline\n- Hashtags: #TheStarMalaysia #News\n", "requestID": "be9db863-0ef5-48f2-8c44-4838c09addf6"}如何提取data:之后的内容
最新发布
08-05
要提取 `data:` 之后的内容,可以按照以下步骤进行操作,具体取决于你使用的编程语言或工具。以下以 Python 为例,展示如何提取 `data:` 后的字典内容: ```python import json # 假设你的原始数据是一个字符串 raw_data = ''' data: {"code": 0, "is_long_text": false, "is_longphoto": true, "keywords": ["Sean Solo", "Hongdae Guy", "Malaysia Visit", "Kuala Lumpur"], "labels": [{"labelType": "l2_labels", "labelName": "Celebrity", "labelAlias": "Celebrity", "labelId": "17aca187083a0303bf2e3ae293991cca"}, {"labelType": "l2_labels", "labelName": "Sean Solo", "labelAlias": "Sean Solo", "labelId": "b94a5fa8c816d072644d2332618c3e9d"}, {"labelType": "l2_labels", "labelName": "Hongdae Guy", "labelAlias": "Hongdae Guy", "labelId": "b1776d972d5f0cfd3ef0adc0dc56ac14"}, {"labelType": "l2_labels", "labelName": "Malaysia Visit", "labelAlias": "Malaysia Visit", "labelId": "5d191baf9b6c09345a8782ab4d52d100"}, {"labelType": "l2_labels", "labelName": "Kuala Lumpur", "labelAlias": "Kuala Lumpur", "labelId": "0b52c720e218f809fe6aea56d201f15c"}], "combineEntityList": [{"type": "schedule", "subtype": "", "detail": "{\"content\": \"Sean Solo fan meet-up and content shooting\", \"time\": \"2025-07-24T12:00:00\", \"start_time\": \"2025-07-24T12:00:00\", \"end_time\": \"2025-07-24T13:00:00\", \"location\": \"Sunway area\", \"period\": \"\", \"reminder\": \"\", \"note\": \"\"}"}], "title": "Sean Solo: Hongdae Guy in Malaysia", "title_st": 1754300624424, "sceneType": ["Articles/Paragraphs"], "summary": "- Source: thestaronline\\n- Time since posted: 4d\\n- Person: Sean Solo, also known as the \\\"Hongdae Guy\\\"\\n- Location mentioned: Kuala Lumpur, Malaysia\\n- District mentioned: Hongdae district, Seoul\\n- Date of sharing location: July 24\\n- Location for content shooting: Sunway area\\n- Catchphrases: \\\"OPEN MINDEU?\\\"\\n- Call to action for more stories: fb.com/thestaronline\\n- Hashtags: #TheStarMalaysia #News\\n", "requestID": "be9db863-0ef5-48f2-8c44-4838c09addf6"} ''' # 分割字符串,提取 data: 后的内容 data_str = raw_data.split("data:")[1].strip() # 将提取的字符串转换为 Python 字典 data_dict = json.loads(data_str) # 现在你可以访问 data 中的任意字段 print(data_dict["title"]) # 输出: Sean Solo: Hongdae Guy in Malaysia print(data_dict["summary"]) # 输出 summary 字段内容 print(data_dict["combineEntityList"][0]["detail"]) # 输出日程详情 ``` ### 总结: - 使用 `split("data:")[1]` 提取 `data:` 后面的内容。 - 使用 `json.loads()` 将 JSON 字符串转换为 Python 字典。 - 之后可以根据键名访问任意字段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值