题目链接:http://codeforces.com/contest/988/problem/F

题目大意:

有三个整数a,n,m,a是终点坐标,给出n个范围(l,r)表示这块区域下雨,m把伞(p,w)在点p有重量为w的伞。

小明可以携带任意数量的伞,经过下雨处时必须要撑伞,小明每走一个单位长度消耗的体力与他所携带伞的重量相同,

求小明从0~a所需消耗的最少体力,若无解则输出-1。

解题思路:

第一种解法:

设dp[i]表示到达i点所需花费的最少体力,rain[i]表示第i段是否下雨(注意是段不是点),ub[j]表示j点放置的伞的重量。

则当rain[i-1]=false时,dp[i]=dp[i]-1

rain[i-1]=true是,dp[i]=min{dp[j]+(i-j)*ub[j]},(ub[j]!=1e18且j<=i-1)

复杂度O(n^2)

第二种解法:

设dp数组,

dp[i][0]表示到达第i段不拿伞最小花费
dp[i][1]表示到达第i段拿伞最小化费
dp[i][2]表示到达第i段拿最小重量的伞的最小化费

然后不想说了,各种递推就是了。。。

复杂度O(n)

代码:

解法一:

 #include<bits/stdc++.h>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=2e3+;
const int INF=0x3f3f3f3f;
const double eps=1e-; LL dp[N],ub[N];
bool rain[N]; int main(){
FAST_IO;
int a,n,m;
cin>>a>>n>>m;
for(int i=;i<N;i++){
dp[i]=ub[i]=1e18;
}
for(int i=;i<=n;i++){
int l,r;
cin>>l>>r;
if(l>r) swap(l,r);
for(int j=l;j<=r-;j++){
rain[j]=true;
}
}
for(int i=;i<=m;i++){
LL p,w;
cin>>p>>w;
ub[p]=min(ub[p],w);
}
dp[]=;
for(int i=;i<=a;i++){
if(!rain[i-]){
dp[i]=dp[i-];
}
else{
for(int j=i-;j>=;j--){
if(ub[j]!=1e18)
dp[i]=min(dp[i],dp[j]+(i-j)*ub[j]);
}
}
}
if(dp[a]==1e18)
cout<<-<<endl;
else
cout<<dp[a]<<endl;
return ;
}

解法二:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
#include<cctype>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=2e3+;
const int INF=0x3f3f3f3f;
const double eps=1e-; bool rain[N],flag[N];
LL dp[N][],ub[N];
//dp[i][0]表示到达第i段不拿伞最小花费
//dp[i][1]表示到达第i段拿伞最小化费
//dp[i][2]表示到达第i段拿最小重量的伞的最小化费 int main(){
FAST_IO;
int a,n,m;
cin>>a>>n>>m;
for(int i=;i<N;i++){
ub[i]=dp[i][]=dp[i][]=dp[i][]=1e18;
}
for(int i=;i<=n;i++){
int l,r;
if(l>r)
swap(l,r);
cin>>l>>r;
for(int j=l;j<=r-;j++){
rain[j]=true;
}
}
for(int i=;i<=m;i++){
LL p,w;
cin>>p>>w;
ub[p]=min(ub[p],w);
} if(!rain[])
dp[][]=;
dp[][]=dp[][]=ub[];
LL mmin=ub[],now=ub[];
for(int i=;i<=a-;i++){
if(ub[i]){
mmin=min(ub[i],mmin);
now=min(ub[i],now);
}
LL t=min(dp[i-][]+now,dp[i-][]+mmin);
dp[i][]=t;
if(t==dp[i-][]+mmin)
now=mmin;
dp[i][]=dp[i-][]+mmin; //下雨
if(rain[i]){
//有伞
if(ub[i]){
dp[i][]=min(dp[i-][]+ub[i],dp[i][]);
if(dp[i][]==dp[i-][]+ub[i])
now=ub[i];
if(mmin==ub[i]){
dp[i][]=min(dp[i-][]+mmin,dp[i][]);
}
}
}
//不下雨
else{
dp[i][]=min(dp[i-][],dp[i-][]);
//有伞
if(ub[i]){
dp[i][]=min(dp[i-][]+ub[i],dp[i][]);
if(dp[i][]==dp[i-][]+ub[i])
now=ub[i];
if(mmin==ub[i]){
dp[i][]=min(dp[i-][]+mmin,dp[i][]);
}
}
}
}
LL ans=min(dp[a-][],dp[a-][]);
if(ans!=1e18)
cout<<ans<<endl;
else
cout<<-<<endl;
return ;
}

Codeforces 988F Rain and Umbrellas(DP)的更多相关文章

  1. Codeforces 988F. Rain and Umbrellas

    解题思路:动态规划 遍历点i,如果从前一个点i-1走到这个点i不需要伞,则疲劳值不变dp[i] = dp[i-1]. 如果前一个点i-1走到这一个点i需要伞,则从前面找一把伞. 即遍历前面的每个点j, ...

  2. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  3. CodeForces 988 F Rain and Umbrellas

    Rain and Umbrellas 题意:某同学从x=0的点走到x=a的点,路上有几段路程是下雨的, 如果他需要经过这几段下雨的路程, 需要手上有伞, 每一把伞有一个重量, 求走到重点重量×路程的最 ...

  4. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  5. Rain and Umbrellas(dp)

    题目链接 http://codeforces.com/problemset/problem/988/F 令dp[i][j]为走到目标为i处,手里拿着第j把伞,同时注意,在某处可能存在不止一把伞 #in ...

  6. Codeforces Round #486-F.Rain and Umbrellas题解

    一.题目链接:http://codeforces.com/contest/988/problem/F 二.题面 三.思路 很明显而且比较能想到的$dp$. 四.代码实现 #include<bit ...

  7. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  8. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  9. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. c/c++基本数据类型转换

    If either operand is of type long double, the other operand is converted to type long double. If the ...

  2. linux Git版本控制学习与Git服务器搭建

    来源地址 要随时掌握工作区的状态,使用git status命令. 如果git status告诉你有文件被修改过,用git diff可以查看修改内容. 初始化一个Git仓库,使用git init命令. ...

  3. BZOJ1053:反素数(数学)

    题目链接 对于任意的正整数\(x\),记其约数的个数为\(g(x)\).现在定义反素数:对于\(0<i<x\),都有\(g(x)>g(i)\),那么就称x为反素数. 现在给定一个数N ...

  4. 「Vue」实用组件

    一.时间格式 1.安装Moment模块 npm i moment -S2.main.js中设置全局过滤器 import moment from 'moment' Vue.filter('ctime', ...

  5. JVM体系结构和工作方式

            JVM能够跨计算机体系结构来执行Java字节码,主要是由于JVM屏蔽了与各个计算机平台相关的软件或者是硬件之间的差异,使得与平台相关的耦合统一由JVM提供者来实现.   何为JVM   ...

  6. Sql Server 优化技巧

    1.查看执行时间和cpu占用时间 set statistics time on select * from dbo.Product set statistics time off 打开你查询之后的消息 ...

  7. opencv 图像处理函数大全

    .cvLoadImage:将图像文件加载至内存: .cvNamedWindow:在屏幕上创建一个窗口: .cvShowImage:在一个已创建好的窗口中显示图像: .cvWaitKey:使程序暂停,等 ...

  8. (一)在Lingo中使用集合

    1.    在Lingo中使用集合 4.1 集合的基本用法和lingo模型的基本要素 Lingo虽然使用方便,但是如果要解决几万个,几十万个变量的优化问题时,我们总不能一个一个地列出x1,x2,…,x ...

  9. NGINX配置PHP解析

    <?php phpinfo(); ?> location ~ \.php$ { root html; fastcgi_pass ; fastcgi_index index.php; fas ...

  10. PHP基础知识之————匿名函数(Anonymous functions)

    匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应用的情况. ...