CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)

本文探讨了如何运用斜率DP算法解决林业砍伐问题,目标是在确保所有树木被完全砍伐的同时,以最低的成本进行操作。通过输入树木高度和砍伐成本,算法能够确定最优砍伐策略。

 

C. Kalila and Dimna in the Logging Industry
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money.

The manager of logging factory wants them to go to the jungle and cut n trees with heights a1, a2, ..., an. They bought a chain saw from a shop. Each time they use the chain saw on the tree number i, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is i (the tree that have height ai in the beginning), then the cost of charging the chain saw would be bi. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each i < jai < aj and bi > bj and also bn = 0and a1 = 1. Kalila and Dimna want to cut all the trees completely, with minimum cost.

They want you to help them! Will you?

Input

The first line of input contains an integer n (1 ≤ n ≤ 105). The second line of input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line of input contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 109).

It's guaranteed that a1 = 1bn = 0a1 < a2 < ... < an and b1 > b2 > ... > bn.

Output

The only line of output must contain the minimum cost of cutting all the trees completely.

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

Sample test(s)
input
5
1 2 3 4 5
5 4 3 2 0
output
25
input
6
1 2 3 10 20 30
6 5 4 3 2 0
output
138

 

 


这题就是斜率DP(555...早知道直接做第3题)

方程我就不写了

long long 溢出我居然De了一上午Bug才发现,我……

 

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMI(a) memset(a,127,sizeof(a))
#define MEMi(a) memset(a,128,sizeof(a))
#define MAXN (100000+10)
#define size (tail-head+1)
int n;
long long a[MAXN],b[MAXN],f[MAXN]={0};
struct node
{
	int i;
	long long x,y;
	node(){}
	node(int _i):i(_i),x(b[i]),y(f[i]){}
	friend long double k(node a,node b){return (long double)(a.y-b.y)/(long double)(a.x-b.x);	}
}q[MAXN];
int head=1,tail=1;
struct V
{
	long long x,y;
	V(node a,node b):x(b.x-a.x),y(b.y-a.y){}
	friend long long operator*(V a,V b){return ((double)a.x/a.y-(double)b.x/b.y)>0?1:-1;	}
};
int main()
{
//	freopen("CF319C.in","r",stdin);
//	freopen("CF319C.out","w",stdout);
	
	cin>>n;
	For(i,n) cin>>a[i];
	For(i,n) cin>>b[i];
	f[1]=b[1];
	q[1]=node(1);
	Fork(i,2,n)
	{
		while (size>=2&&k(q[head],q[head+1])>1-a[i] ) head++;
		int j=q[head].i;
		f[i]=f[j]+(long long)(a[i]-1)*(long long)b[j]+b[i];
		while (size>=2&&(V(q[tail-1],q[tail])*V(q[tail],node(i))>0)) tail--; //维护上凸壳 
		q[++tail]=node(i);
	}
	//For(i,n) cout<<b[i]<<' ';cout<<endl;
	//For(i,n) cout<<f[i]<<' ';cout<<endl;
	cout<<f[n]<<endl;
	
	return 0;
}


 


 

转载于:https://www.cnblogs.com/snake-hand/p/3153451.html

编写一个 SQL 查询,筛选出过去一年中订单总量 少于10本 的 书籍 ,不考虑 上架(available from)距今 不满一个月 的书籍。并且 假设今天是 2019-06-23。 建表语句如下: Create table If Not Exists Books (book_id int, name varchar(50), available_from date); Create table If Not Exists Orders (order_id int, book_id int, quantity int, dispatch_date date); Truncate table Books; insert into Books (book_id, name, available_from) values ('1', 'Kalila And Demna', '2010-01-01'); insert into Books (book_id, name, available_from) values ('2', '28 Letters', '2012-05-12'); insert into Books (book_id, name, available_from) values ('3', 'The Hobbit', '2019-06-10'); insert into Books (book_id, name, available_from) values ('4', '13 Reasons Why', '2019-06-01'); insert into Books (book_id, name, available_from) values ('5', 'The Hunger Games', '2008-09-21'); Truncate table Orders; insert into Orders (order_id, book_id, quantity, dispatch_date) values ('1', '1', '2', '2018-07-26'); insert into Orders (order_id, book_id, quantity, dispatch_date) values ('2', '1', '1', '2018-11-05'); insert into Orders (order_id, book_id, quantity, dispatch_date) values ('3', '3', '8', '2019-06-11'); insert into Orders (order_id, book_id, quantity, dispatch_date) values ('4', '4', '6', '2019-06-05'); insert into Orders (order_id, book_id, quantity, dispatch_date) values ('5', '4', '5', '2019-06-20'); insert into Orders (order_id, book_id, quantity, dispatch_date) values ('6', '5', '9', '2009-02-02'); insert into Orders (order_id, book_id, quantity, dispatch_date) values ('7', '5', '8', '2010-04-13');
07-15
标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值