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

相关文章

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语