F. Rain and Umbrellas(dp)

本文介绍了一个关于寻找从起点到终点携带雨伞行走的最小疲劳值的问题。通过动态规划方法,考虑雨区段分布及不同位置的雨伞重量,实现最优路径选择。

http://codeforces.com/problemset/problem/988/F

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp lives on a coordinate line at the point x=0x=0 . He goes to his friend that lives at the point x=ax=a . Polycarp can move only from left to right, he can pass one unit of length each second.

Now it's raining, so some segments of his way are in the rain. Formally, it's raining on nn non-intersecting segments, the ii -th segment which is in the rain is represented as [li,ri][li,ri] (0≤li<ri≤a0≤li<ri≤a ).

There are mm umbrellas lying on the line, the ii -th umbrella is located at point xixi (0≤xi≤a0≤xi≤a ) and has weight pipi . When Polycarp begins his journey, he doesn't have any umbrellas.

During his journey from x=0x=0 to x=ax=a Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from xx to x+1x+1 if a segment [x,x+1][x,x+1] is in the rain (i.e. if there exists some ii such that li≤xli≤x and x+1≤rix+1≤ri ).

The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain.

Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving.

Can Polycarp make his way from point x=0x=0 to point x=ax=a ? If yes, find the minimum total fatigue after reaching x=ax=a , if Polycarp picks up and throws away umbrellas optimally.

Input

The first line contains three integers aa , nn and mm (1≤a,m≤2000,1≤n≤⌈a2⌉1≤a,m≤2000,1≤n≤⌈a2⌉ ) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas.

Each of the next nn lines contains two integers lili and riri (0≤li<ri≤a0≤li<ri≤a ) — the borders of the ii -th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments ii and jj either ri<ljri<lj or rj<lirj<li .

Each of the next mm lines contains two integers xixi and pipi (0≤xi≤a0≤xi≤a , 1≤pi≤1051≤pi≤105 ) — the location and the weight of the ii -th umbrella.

Output

Print "-1" (without quotes) if Polycarp can't make his way from point x=0x=0 to point x=ax=a . Otherwise print one integer — the minimum total fatigue after reaching x=ax=a , if Polycarp picks up and throws away umbrellas optimally.

Examples

Input

Copy

10 2 4
3 7
8 10
0 10
3 4
8 1
1 2

Output

Copy

14

Input

Copy

10 1 1
0 9
0 5

Output

Copy

45

Input

Copy

10 1 1
0 9
1 5

Output

Copy

-1

Note

In the first example the only possible strategy is to take the fourth umbrella at the point x=1x=1 , keep it till the point x=7x=7 (the total fatigue at x=7x=7 will be equal to 1212 ), throw it away, move on from x=7x=7 to x=8x=8 without an umbrella, take the third umbrella at x=8x=8 and keep it till the end (the total fatigue at x=10x=10 will be equal to 1414 ).

In the second example the only possible strategy is to take the first umbrella, move with it till the point x=9x=9 , throw it away and proceed without an umbrella till the end.

-------------------------------------------------------------------------

动态规划,dp【i】记录到i点的最少体力,dp【a】就是答案。

如果不下雨,dp【i】=dp【i-1】;

下雨的话(i之前的j点有伞),dp【i】=dp【j】+(i-j)*weight;把i点前所有的伞(j点)都要扫一遍。记录dp【j】值;

---------------------------------------------------------------------------

#include<deque>
#include<queue>  
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<stack>
const int inf = 0x3f3f3f3f;
using namespace std;
int umb[2005],dp[2005];
bool rain[2005];
int main()
{
	int a,n,m;
	scanf("%d%d%d",&a,&n,&m);
	for(int i=0;i<=2004;i++)
	{
		umb[i]=inf;
		dp[i]=inf;
		rain[i]=false;
	}
	dp[0]=0;
	for(int i=1;i<=n;i++)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		for(int j=l;j<r;j++)
			rain[j]=true;
	}
	for(int i=1;i<=m;i++)
	{
		int t,w;
		scanf("%d",&t);
		scanf("%d",&w);
		umb[t]=min(umb[t],w);
	}
	for(int i=1;i<=a;i++)
	{
		if(!rain[i-1])
			dp[i]=dp[i-1];
		else
		{
			for(int j=i-1;j>=0;j--)
			{
				if(umb[j]!=inf)
					dp[i]=min(dp[i],dp[j]+(i-j)*umb[j]);
			}
		}
	}
	if(dp[a]>=inf)
		printf("-1\n");
	else
		printf("%d\n",dp[a]);
	
    return 0;
}

 

### Lua 中实现借伞换伞功能的逻辑 在 Lua 编程语言中,可以通过定义对象和方法来模拟借伞和还伞的功能。这种设计通常涉及状态管理和资源分配的概念。以下是基于面向对象编程的思想实现的一个简单示例。 #### 定义 Umbrella 类 通过表(table)结构创建一个 `Umbrella` 对象类,用于表示每把雨伞的状态以及其归属情况: ```lua -- 创建 Umbrella 类型的对象 local function createUmbrella(id) return { id = id, -- 雨伞唯一标识符 isBorrowed = false -- 是否已被借用 } end ``` #### 定义 BorrowSystem 类 为了管理多把雨伞并提供借伞和还伞的操作接口,可以进一步封装成一个管理系统类: ```lua -- 初始化借伞系统 local function initBorrowSystem(umbrellasCount) local system = {} system.umbrellas = {} -- 创建指定数量的雨伞实例 for i = 1, umbrellasCount do table.insert(system.umbrellas, createUmbrella(i)) end -- 借伞函数 function system.borrow() for _, umbrella in ipairs(system.umbrellas) do if not umbrella.isBorrowed then umbrella.isBorrowed = true print("成功借出雨伞 ID:", umbrella.id) return umbrella.id end end print("当前无可用雨伞") return nil end -- 还伞函数 function system.returnUmbrella(umbrellaId) for _, umbrella in ipairs(system.umbrellas) do if umbrella.id == umbrellaId and umbrella.isBorrowed then umbrella.isBorrowed = false print("成功归还雨伞 ID:", umbrella.id) return true elseif umbrella.id == umbrellaId and not umbrella.isBorrowed then print("该雨伞未被借用或已归还") return false end end print("找不到对应雨伞 ID:", umbrellaId) return false end return system end ``` #### 测试代码 下面是一个简单的测试场景,展示如何使用上述逻辑完成借伞和还伞操作: ```lua -- 初始化有 3 把雨伞的借伞系统 local borrowSystem = initBorrowSystem(3) -- 尝试借伞三次 borrowSystem.borrow() -- 成功借出雨伞 ID: 1 borrowSystem.borrow() -- 成功借出雨伞 ID: 2 borrowSystem.borrow() -- 成功借出雨伞 ID: 3 borrowSystem.borrow() -- 当前无可用雨伞 -- 归还第二把雨伞 borrowSystem.returnUmbrella(2) -- 成功归还雨伞 ID: 2 -- 再次尝试借伞 borrowSystem.borrow() -- 成功借出雨伞 ID: 2 ``` 此代码实现了基本的借伞和还伞功能,并能够处理异常情况,例如当所有雨伞都被借走时提示用户无法继续借伞[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值