Codeforces 313D Ilya and Roads【Dp+思维】

修复道路的最小花费
针对一条包含多个洞的道路,本文介绍了一种算法来确定修复至少K个洞所需的最小花费。通过动态规划的方法,预处理出修复任意两个洞之间的最小成本,并利用这些信息有效地解决了问题。

D. Ilya and Roads
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as n holes in a row. We will consider the holes numbered from 1 to n, from left to right.

Ilya is really keep on helping his city. So, he wants to fix at least k holes (perharps he can fix more) on a single ZooVille road.

The city has m building companies, the i-th company needs ci money units to fix a road segment containing holes with numbers of at least li and at most ri. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment.

Determine the minimum money Ilya will need to fix at least k holes.

Input

The first line contains three integers n, m, k (1 ≤ n ≤ 300, 1 ≤ m ≤ 105, 1 ≤ k ≤ n). The next m lines contain the companies' description. The i-th line contains three integers li, ri, ci (1 ≤ li ≤ ri ≤ n, 1 ≤ ci ≤ 109).

Output

Print a single integer — the minimum money Ilya needs to fix at least k holes.

If it is impossible to fix at least k holes, print -1.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples
Input
10 4 6
7 9 11
6 9 13
7 7 7
3 5 6
Output
17
Input
10 7 1
3 4 15
8 9 8
5 6 8
9 10 6
1 4 2
1 4 10
8 10 13
Output
2
Input
10 1 9
5 10 14
Output
-1


题目大意:

给你N个洞,需要进行填补至少K个,现在有M个施工队,对于这M个施工队的元素有三个:

L,R,C.表示这个施工队只能对于区间【L,R】的洞进行填补,雇佣这个施工队就需要花费C单位的价钱。

问至少填补K个洞的最小花费。


思路:


显然直接贪心是不可取的,我们考虑Dp.对于Dp来讲,对于最终结果的无非有三种状态:

①位子

②填补了多少个洞

③花费为多少。

那么我们设定Dp【i】【j】表示为从左到右的Dp,到位子i,一共填补了j个洞口的最小花费。


那么状态转移过程我们可以O(n*k*m)去求,然而时间复杂度不允许;

所以我们要优化这个m.观察到数据范围N不大,所以我们考虑预处理出cost【i】【j】表示填补区间【i,j】的洞口的最小花费.

因为是线性的动态规划,所以预处理cost【i】【j】的时候没有必要O(m*n^2)去做,直接O(m*n*2)的以左右端点各作为起点和终点去画一段即可:



那么状态转移方程就可以O(n^3)的去转移:




注意实现代码的细节就没有别的难点了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
struct node
{
    int x,y;
    ll w;
}a[100040];
ll cost[305][305];
ll dp[305][305];
const ll INF=100000000000000000;
int main()
{
    int n,m,kk;
    while(~scanf("%d%d%d",&n,&m,&kk))
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                cost[i][j]=INF;
                dp[i][j]=INF;
            }
        }
        for(int i=1;i<=m;i++)
        {
            int x,y,w;
            scanf("%d%d%I64d",&a[i].x,&a[i].y,&a[i].w);
            for(int j=a[i].x;j<=a[i].y;j++)
            {
                cost[j][a[i].y]=min(cost[j][a[i].y],a[i].w);
                cost[a[i].x][j]=min(cost[a[i].x][j],a[i].w);
            }
            cost[a[i].x][a[i].y]=min(cost[a[i].x][a[i].y],a[i].w);
        }
        dp[0][0]=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=i;j++)
            {
                dp[i][j]=min(dp[i][j],dp[i-1][j]);
                for(int k=1;k<=i;k++)
                {
                    if(j-(i-k+1)>=0)
                    {
                        dp[i][j]=min(dp[i][j],cost[k][i]+dp[k-1][j-(i-k+1)]);
                    }
                }
            }
        }
        if(dp[n][kk]==INF)printf("-1\n");
        else
        printf("%I64d\n",dp[n][kk]);
    }
}





Security-feature-detection-system 安全检测系统 简介 安全检测系统-多目标识别(YOLOv5)和人脸识别(Facenet)快速部署系统。 功能上:本项目使用YOLOv5实现多目标识别,使用Facenet实现人脸识别,最终需要人脸和此人应具备的多目标同时满足才能通过安全检测,部署上:使用pyqt5实现前端可视化,在前端页面运行YOLOv5多目标识别系统(将模型运行封装到Qt中),使用Docker封装人脸识别后端系统,使用网络请求等包实现前后端交互 案例:进行多目标识别的同时,进行人脸识别,前端系统发送请求,携带参数到后端进行人脸识别,最终返回人脸识别结果,获取人脸识别结果后,检索该成员应具备的多目标特征,与YOLOv5多目标识别的实际结果进行比对,若无误则通过安全检测。 根据原作 https://pan.quark.cn/s/9784cdf4abfd 的源码改编 项目背景 出于一些比赛的需要,以及逃离懵懂状态开始探索,我于2023.12~2024.1(大二上)开始一些CV、LLM项目的研究,苦于能力有限,当时大部分的项目都是依托开源搭建而来,诸如本项目就是依托开源的Compreface和Yolov5搭建,我只不过做了缝合的工作,所以在此必须提及这两个项目的巨大贡献:https://.com/exadel-inc/CompreFace https://.com/ultralytics/yolov5 今天是2024.7.11(大二下暑假),时隔半年我才开始这个项目的开源工作是因为,半年前的水平有限,虽然自己能实现项目的运作,但是恐很多细节介绍不好,当然本文自发出,后续我还会跟进,欢迎指正:22012100039@stu.xidian.edu.c...
当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值