TOJ 4369 ZOJ 3632 Watermelon Full of Water / 线段树优化DP

2024-06-15 12:18

本文主要是介绍TOJ 4369 ZOJ 3632 Watermelon Full of Water / 线段树优化DP,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Watermelon Full of Water

时间限制(普通/Java):3000MS/9000MS     运行内存限制:65536KByte

描述

Watermelon is very popular in the hot summer. Students in ZJU-ICPC Team also love watermelon very much and they hope that they can have watermelon to eat every day during the summer vacation. Suppose there are n days and every day they can buy only one watermelon. The price of watermelon may be different in each day. Besides, sometimes the watermelon they choose to buy may be very big, which means if they buy this watermelon, they will need several days to eat it up. The students want to spend the minimum money to buy enough watermelon so that they can eat watermelon every day. Can you help them?

Notice: When they buy a new watermelon, if they still have an old watermelon, they will throw the old one into dustbin. For example, suppose they buy a watermelon on the fisrt day, and it needs 4 days to eat up the watermelon. But if they buy a new watermelon on the second day and it needs 2 days to eat up the new watermelon, then they will throw the old one, and they have to buy a new watermelon on the fourth day since they don't have any watermelon to eat on that day.

输入

The input contains multiple test cases ( no more than 200 test cases ).
In each test case, first there is an integer, n ( 1 <= n <=50000 ) , which is the number of summer days.
Then there is a line containing n positive integers with the ith integer indicating the price of the watermelon on the ith day.
Finally there is line containing n positive integers with the ith integer indicating the number of days students need to eat up the watermelon bought on the ith day.
All these integers are no more than 100000 and integers are seperated by a space.

输出

For each case, output one line with an integer which is the minimum money they must spend so that they can have watermelon to eat every day.

样例输入

4
10 20 1 40
3 2 3 1

样例输出

11

让我做 是对于每一天的西瓜可以吃k天的话 去更新 dp[i]到dp[i+k-1]的最小值 用线段树成段更新

可以大神的做法就是更新i+k-1这一天 然后询问的时候询问 i到n天的最小值


 

#include <stdio.h>
#include <string.h>
const long long Max = 1000000000000000;
using namespace std;
const long long MAX = 50010;
struct node
{long long l;long long r;long long min;
}a[MAX*4];
long long dp[MAX];
long long b[MAX];
long long c[MAX];long long min(long long x,long long y)
{return x < y ? x : y;
}
void build(long long l,long long r,long long rt)
{a[rt].l = l;a[rt].r = r;a[rt].min = Max;if(l == r)return;long long m = (l + r) >> 1;build(l,m,rt<<1);build(m+1,r,rt<<1|1);
}
long long query(long long x,long long y,long long rt)
{if(x <= a[rt].l && y >= a[rt].r)return a[rt].min;long long m = (a[rt].l + a[rt].r) >> 1;long long ret = Max;if(x <= m)ret = min(ret,query(x,y,rt<<1));if(y > m)ret = min(ret,query(x,y,rt<<1|1));return ret;
}void update(long long x,long long rt,long long mi)
{if(a[rt].l == a[rt].r){a[rt].min = min(mi,a[rt].min);return;}long long m = (a[rt].l + a[rt].r) >> 1;if(x <= m)update(x,rt<<1,mi);elseupdate(x,rt<<1|1,mi);a[rt].min = min(a[rt<<1].min,a[rt<<1|1].min);
}int main()
{long long i,j,k,n;while(scanf("%lld",&n)!=EOF){for(i = 1; i <= n; i++)scanf("%lld",&b[i]);for(i = 1; i <= n; i++)scanf("%lld",&c[i]);build(1,n,1);for(i = 1;i <= n; i++){j = i + c[i] - 1;if(j > n)j = n;k = dp[i-1] + b[i];update(j,1,k);dp[i] = query(i,n,1);}printf("%lld\n",dp[n]);}return 0;
}


 

这篇关于TOJ 4369 ZOJ 3632 Watermelon Full of Water / 线段树优化DP的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1063430

相关文章

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

服务器雪崩的应对策略之----SQL优化

SQL语句的优化是数据库性能优化的重要方面,特别是在处理大规模数据或高频访问时。作为一个C++程序员,理解SQL优化不仅有助于编写高效的数据库操作代码,还能增强对系统性能瓶颈的整体把握。以下是详细的SQL语句优化技巧和策略: SQL优化 1. 选择合适的数据类型2. 使用索引3. 优化查询4. 范式化和反范式化5. 查询重写6. 使用缓存7. 优化数据库设计8. 分析和监控9. 调整配置1、

Java中如何优化数据库查询性能?

Java中如何优化数据库查询性能? 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将深入探讨在Java中如何优化数据库查询性能,这是提升应用程序响应速度和用户体验的关键技术。 优化数据库查询性能的重要性 在现代应用开发中,数据库查询是最常见的操作之一。随着数据量的增加和业务复杂度的提升,数据库查询的性能优化显得尤为重

打包体积分析和优化

webpack分析工具:webpack-bundle-analyzer 1. 通过<script src="./vue.js"></script>方式引入vue、vuex、vue-router等包(CDN) // webpack.config.jsif(process.env.NODE_ENV==='production') {module.exports = {devtool: 'none

Clickhouse 的性能优化实践总结

文章目录 前言性能优化的原则数据结构优化内存优化磁盘优化网络优化CPU优化查询优化数据迁移优化 前言 ClickHouse是一个性能很强的OLAP数据库,性能强是建立在专业运维之上的,需要专业运维人员依据不同的业务需求对ClickHouse进行有针对性的优化。同一批数据,在不同的业务下,查询性能可能出现两极分化。 性能优化的原则 在进行ClickHouse性能优化时,有几条

群体优化算法---电磁共振优化算法(EROA)介绍包含示例滤波器设计

介绍 电磁共振优化算法(Electromagnetic Resonance Optimization Algorithm, EROA)是一种新型的元启发式优化算法,其灵感来源于电磁共振现象。电磁共振是一种物理现象,当一个系统在特定频率下响应最大时,这个频率被称为共振频率。在优化算法中,共振频率可以用来引导搜索过程,提高优化效率 EROA 算法的基本原理 种群初始化: 在搜索空间内随机生成一定

怎么优化ArcEngine组件开发mfc程序界面?

🏆本文收录于「Bug调优」专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!! 问题描述   这种VS2015 + ArcEngine10.2开发的mfc小程序怎么优化界面,使系统看上去更美观 如上问题有来自我自身项目开发,有的收集网站

上海邀请赛 A题目 HDU 5236(dp)

先求出没有ctrl+s的时候构造长度为i的期望f[i] 。然后枚举保存的次数,求出最小即可。 #include<cstdio>#include<cstdio>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<iostream>#include<map>

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点。实际上集合内的每个点是相同的,这样的集合就是一个强连通分量。 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图。在dag的图上面进行dp。可以先用拓扑排序后dp。或者建反响边记忆化搜索 。 VIEW

SEO 优化注意事项

一.站内优化 1.做好HTML头标签 标题(title):标题是网页优化中相当有分量,一般网页title主要包含一些关键词、网站名称等。 关键词(keyword):重要性大家都知道!关键词设定要参考热度、百度指数等一些手段,当然选择这些的前提要与自己网站的主题相关。关键词不宜多,一般就是1-3个。 描述(description):主要是对网站的一个介绍,虽然没有前两个标签在搜索引擎