Codeforces Round #426 (Div. 2) D. The Bakery(DP+线段树) 好题

本文介绍了一种使用动态规划和线段树解决特定问题的方法。该问题是将一系列蛋糕按类型打包进盒子以最大化总价值,每个盒子的价值由其包含的不同类型的蛋糕数量决定。文章详细解释了如何通过构建DP状态和利用线段树来高效地解决问题。

直接转载自:http://blog.youkuaiyun.com/ssimple_y/article/details/76410328

Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery.

Soon the expenses started to overcome the income, so Slastyona decided to study the sweets market. She learned it’s profitable to pack cakes in boxes, and that the more distinct cake types a box contains (let’s denote this number as the value of the box), the higher price it has.

She needs to change the production technology! The problem is that the oven chooses the cake types on its own and Slastyona can’t affect it. However, she knows the types and order of n cakes the oven is going to bake today. Slastyona has to pack exactly k boxes with cakes today, and she has to put in each box several (at least one) cakes the oven produced one right after another (in other words, she has to put in a box a continuous segment of cakes).

Slastyona wants to maximize the total value of all boxes with cakes. Help her determine this maximum possible total value.
Input

The first line contains two integers n and k (1 ≤ n ≤ 35000, 1 ≤ k ≤ min(n, 50)) – the number of cakes and the number of boxes, respectively.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ n) – the types of cakes in the order the oven bakes them.
Output

Print the only integer – the maximum total value of all boxes with cakes.
Examples
input

4 1
1 2 2 1

output

2

input

7 2
1 3 3 1 4 4 4

output

5

input

8 3
7 7 8 7 7 8 1 7

output

6

Note

In the first example Slastyona has only one box. She has to put all cakes in it, so that there are two types of cakes in the box, so the value is equal to 2.

In the second example it is profitable to put the first two cakes in the first box, and all the rest in the second. There are two distinct types in the first box, and three in the second box then, so the total value is 5.

题意:把n个数分成k段,每段的价值等于这一段内不同数字的个数,求总的最大价值。

可以很快发现这是一个dp,dp[i][j]表示到第i个数字,已经分成了k段的最大价值。

dp[i][j] = max(dp[t][j-1]) (1<= t < i)

可以发现转移不是那么容易,所以我们用到线段树去维护当前位置前面的最大价值。

对于状态i,j,线段树维护的是1~i-1的最大值

对于每一个位置,找到前面最后一个与它数字相同的的位置,把这之间线段树的值都加上1,然后dp[i][j]的值就是j-1到i-1的最大值。

最后答案就是dp[n][k]。

(注意线段树的区间范围是0~n,因为可以直接从0转移过来)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>


using namespace std;


const int maxn = 45000;
int maxv[maxn*4];
int add[maxn*4];


void PushUp(int rt)
{
    maxv[rt] = max(maxv[rt*2],maxv[rt*2+1]);
}
void PushDown(int l,int r,int rt)
{
    if(add[rt])
    {
        int m = (l+r)/2;
        add[rt*2] += add[rt];
        add[rt*2+1] += add[rt];
        maxv[rt*2] += add[rt];
        maxv[rt*2+1] += add[rt];
        add[rt] = 0;
    }
}


void update(int x,int y,int c, int l, int r, int rt)
{
    if (x<=l && y>=r)
    {
        maxv[rt] += c;
        add[rt] += c;
        return;
    }
    PushDown(l,r,rt);
    int m = (l + r) / 2;
    if (x <= m)
        update(x, y, c, l, m, rt*2);
    if(y > m)
        update(x, y, c, m + 1, r, rt*2+1);


    PushUp(rt);
}


int query(int ll, int rr, int l, int r, int rt)
{
    if (ll <= l && rr >= r) return maxv[rt];
    PushDown(l,r,rt);
    int m = (l + r) / 2;
    int ret = 0;
    if (ll <= m)
        ret = max(ret,query(ll, rr, l, m, rt*2));
    if (rr > m)
        ret = max(ret,query(ll, rr, m + 1, r, rt*2+1));
    return ret;
}


int a[maxn],last[maxn],now[maxn];
int dp[maxn][55];


int main(void)
{
    int n,k,i,j;
    while(scanf("%d%d",&n,&k)==2)
    {
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(now,0,sizeof(now));
        for(i=1;i<=n;i++)
        {
            last[i] = now[a[i]];
            now[a[i]] = i;
        }
        memset(dp,0,sizeof(dp));
        for(j=1;j<=k;j++)
        {
            memset(maxv,0,sizeof(maxv));
            memset(add,0,sizeof(add));
            for(i=1;i<=n;i++)
            {
                update(i,i,dp[i][j-1],0,n,1);
            }
            for(i=j;i<=n;i++)
            {
                update(max(last[i],j-1),i-1,1,0,n,1);
                dp[i][j] = query(j-1,i-1,0,n,1);
            }
        }
        printf("%d\n",dp[n][k]);
    }


    return 0;
}
具有多种最大功率点跟踪(MPPT)方法的光伏发电系统(P&O-增量法-人工神经网络-模糊逻辑控制-粒子群优化)之使用粒子群算法的最大功率点追踪(MPPT)(Simulink仿真实现)内容概要:本文介绍了一个涵盖多个科研领域的综合性MATLAB仿真资源集合,重点聚焦于光伏发电系统中基于粒子群优化(PSO)算法的最大功率点追踪(MPPT)技术的Simulink仿真实现。文档还列举了多种MPPT方法(如P&O、增量电导法、神经网络、模糊逻辑控制等),并展示了该团队在电力系统、智能优化算法、机器学习、路径规划、无人机控制、信号处理等多个方向的技术服务能力与代码实现案例。整体内容以科研仿真为核心,提供大量可复现的Matlab/Simulink模型和优化算法应用实例。; 适合人群:具备一定电力电子、自动控制或新能源背景,熟悉MATLAB/Simulink环境,从事科研或工程仿真的研究生、科研人员及技术人员。; 使用场景及目标:①学习并实现光伏系统中基于粒子群算法的MPPT控制策略;②掌握多种智能优化算法在电力系统与自动化领域的建模与仿真方法;③获取可用于论文复现、项目开发和技术攻关的高质量仿真资源。; 阅读建议:建议结合提供的网盘资料,按照研究方向选取对应模块进行实践,重点关注Simulink模型结构与算法代码逻辑的结合,注重从原理到仿真实现的全过程理解,提升科研建模能力。
热成像人物检测数据集 一、基础信息 数据集名称:热成像人物检测数据集 图片数量: 训练集:424张图片 验证集:121张图片 测试集:61张图片 总计:606张热成像图片 分类类别: - 热成像人物:在热成像图像中的人物实例 - 非热成像人物:在非热成像或普通图像中的人物实例,用于对比分析 标注格式: YOLO格式,包含边界框和类别标签,适用于目标检测任务。数据来源于热成像和视觉图像,覆盖多种场景条件。 二、适用场景 热成像监控与安防系统开发: 数据集支持目标检测任务,帮助构建能够在低光、夜间或恶劣环境下自动检测和定位人物的AI模型,提升监控系统的可靠性和实时响应能力。 红外视觉应用研发: 集成至红外摄像头或热成像设备中,实现实时人物检测功能,应用于安防、军事、救援和工业检测等领域。 学术研究与创新: 支持计算机视觉与热成像技术的交叉研究,助力开发新算法用于人物行为分析或环境适应型检测模型。 教育与培训: 可用于高校或培训机构,作为学习热成像人物检测和AI模型开发的教学资源,提升实践技能。 三、数据集优势 精准标注与多样性: 每张图片均由专业标注员标注,确保边界框定位准确,类别分类清晰。包含热成像和非热成像类别,提供对比数据,增强模型的泛化能力和鲁棒性。 场景实用性强: 数据覆盖多种环境条件,如不同光照和天气,模拟真实世界应用,适用于复杂场景下的人物检测任务。 任务适配性高: YOLO标注格式兼容主流深度学习框架(如YOLOv5、YOLOv8等),可直接加载使用,支持快速模型开发和评估。 应用价值突出: 专注于热成像人物检测,在安防、监控和特殊环境检测中具有重要价值,支持早期预警和高效决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值