atcoder B - Frog 2 (DP)
B - Frog 2
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 100100 points
Problem Statement
There are NN stones, numbered 1,2,…,N1,2,…,N. For each ii (1≤i≤N1≤i≤N), the height of Stone ii is hihi.
There is a frog who is initially on Stone 11. He will repeat the following action some number of times to reach Stone NN:
- If the frog is currently on Stone ii, jump to one of the following: Stone i+1,i+2,…,i+Ki+1,i+2,…,i+K. Here, a cost of |hi−hj||hi−hj| is incurred, where jj is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone NN.
Constraints
- All values in input are integers.
- 2≤N≤1052≤N≤105
- 1≤K≤1001≤K≤100
- 1≤hi≤1041≤hi≤104
Input
Input is given from Standard Input in the following format:
NN KK
h1h1 h2h2 …… hNhN
Output
Print the minimum possible total cost incurred.
Sample Input 1 Copy
5 3
10 30 40 50 20
Sample Output 1 Copy
30
If we follow the path 11 → 22 → 55, the total cost incurred would be |10−30|+|30−20|=30|10−30|+|30−20|=30.
Sample Input 2 Copy
3 1
10 20 10
Sample Output 2 Copy
20
If we follow the path 11 → 22 → 33, the total cost incurred would be |10−20|+|20−10|=20|10−20|+|20−10|=20.
Sample Input 3 Copy
2 100
10 10
Sample Output 3 Copy
0
If we follow the path 11 → 22, the total cost incurred would be |10−10|=0|10−10|=0.
Sample Input 4 Copy
10 4
40 10 20 70 80 10 20 70 80 60
Sample Output 4 Copy
40
If we follow the path 11 → 44 → 88 → 1010, the total cost incurred would be |40−70|+|70−70|+|70−60|=40|40−70|+|70−70|+|70−60|=40.
题目链接:https://atcoder.jp/contests/dp/tasks/dp_b
题意:这一篇的进阶版。
把每次只能跳1~2步改成1~k步。
做法只需要其他的不变,把转移方程那里循环1~k次就行了。
附上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
ll dp[maxn];
ll a[maxn];
ll k;
int main()
{
gbtb;
cin>>n>>k;
repd(i,,n)
{
cin>>a[i];
}
dp[]=;
dp[]=;
repd(i,,n)
{
dp[i]=inf;
}
repd(i,,n)
{
for(int j=i-;j>=max(1ll,i-k);j--)
{
dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j]));
}
}
cout<<dp[n];
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
atcoder B - Frog 2 (DP)的更多相关文章
- atcoder A - Frog 1(DP)
A - Frog 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...
- Atcoder E - RGB Sequence(dp)
题目链接:http://arc074.contest.atcoder.jp/tasks/arc074_c 题意:一共有3种颜色,红色,绿色,蓝色.给出m个要求l,r,x表示在区间[l,r]内要有x种不 ...
- Atcoder Beginner Contest 155E(DP)
#definde HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; int main(){ ios: ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- 【CF625E】Frog Fights(模拟)
[CF625E]Frog Fights(模拟) 题面 CF 洛谷 翻译: 有\(n\)只青蛙在一个被分为了\(m\)等分的圆上,对于每份顺时针依次标号. 初始时每只青蛙所在的位置是\(p_i\),速度 ...
随机推荐
- IO流_文件切割与合并(带配置信息)
在切割文件的时候应该生成一个记录文件信息的文件,以便在以后合并文件的时候知道这个文件原来的文件名和记录文件切割完后生成了多少个切割文件 import java.io.File; import java ...
- 2018-2019-2 网络对抗技术 20165318 Exp1 PC平台逆向破解
实验模块 (一)直接修改程序机器指令,改变程序执行流程: (二)通过构造输入参数,造成BOF攻击,改变程序执行流: (三)注入Shellcode并执行: 实验准备 设置共享文件夹(这一步我已经在之前安 ...
- 【转】这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- rac添加新节点的步骤与方法(官方步骤与自我测试)
Extending the Oracle Grid Infrastructure Home to the New NodeNow that the new node has been configur ...
- checkbox选中并通过ajax传数组到后台接收
这次讲的是通过复选框,选中,点击提交,并将值chuan到后台controller.这个应用是比较广的,什么批量修改,批量添加,授权管理等等,都用到这个.其实这个还是挺简单的,并没有想象中的那么难,但我 ...
- 【转】Pandas的Apply函数——Pandas中最好用的函数
转自:https://blog.csdn.net/qq_19528953/article/details/79348929 import pandas as pd import datetime #用 ...
- mysql innodb引擎 一次线上死锁分析排查步骤
我们的线上erp系统一天使用人员反映部分数据死活保存不上而且页面操作很慢.开始以为操作数据量大的原因, 后来查看了我们线上的glowroot系统,发现slowtrace中有超长时间的访问,点开查看详情 ...
- OpenStack keystone节点搭建(官方2018年4月份文档)
参考文档:https://docs.openstack.org/install-guide/common/conventions.html https://docs.openstack.org/mit ...
- 【Codeforces 331D3】Escaping on Beaveractor
题意:给\(b\times b\)的网格,其中有\(n\)个不交叉的箭头. 现在有\(q\)个询问,每个询问包含一个点\((x,y)\),以及一个方向\(dir\).时间\(t\). 要求从\((x, ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...