E. Let's Go Rolling!
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

On a number axis directed from the left rightwards, n marbles with coordinates x1, x2, ..., xn are situated. Let's assume that the sizes of the marbles are infinitely small, that is in this task each of them is assumed to be a material point. You can stick pins in some of them and the cost of sticking in the marble number i is equal to ci, number ci may be negative. After you choose and stick the pins you need, the marbles will start to roll left according to the rule: if a marble has a pin stuck in it, then the marble doesn't move, otherwise the marble rolls all the way up to the next marble which has a pin stuck in it and stops moving there. If there is no pinned marble on the left to the given unpinned one, it is concluded that the marble rolls to the left to infinity and you will pay an infinitely large fine for it. If no marble rolled infinitely to the left, then the fine will consist of two summands:

  • the sum of the costs of stuck pins;
  • the sum of the lengths of the paths of each of the marbles, that is the sum of absolute values of differences between their initial and final positions.

Your task is to choose and pin some marbles in the way that will make the fine for you to pay as little as possible.

Input

The first input line contains an integer n (1 ≤ n ≤ 3000) which is the number of marbles. The next n lines contain the descriptions of the marbles in pairs of integers xi, ci ( - 109 ≤ xi, ci ≤ 109). The numbers are space-separated. Each description is given on a separate line. No two marbles have identical initial positions.

Output

Output the single number — the least fine you will have to pay.

Examples
input

Copy
3
2 3
3 4
1 2
output
5
input

Copy
4
1 7
3 1
5 10
6 1
output
11

题意:

有n个小球,所有小球都在一条线上,以该线为x轴,每个球在x轴上都有各自的位置,所有小球都有各自固定的费用。

初始状态每个小球都未固定,为固定的小球会向x轴的负半轴滚动,直到碰到一个固定的小球。

小球滚动的距离也算是费用。选择哪些小球固定或不固定。

求滚动费用与固定费用之和最小为多少。

我们先将每个点按照坐标升序排序

dp[i][j]中一维表示进行到第几个点,二维表示最后一个被固定的点

则递推到i点时分两种情况:

1: i点固定(加上需要固定的费用,i点后的小球不会往前滚),那么有dp[i][i]=min(dp[i][i],dp[i-1][j]+p[i].c);

2: i点不固定(则需要加上从j点向前滚到i点的费用),那么有dp[i][j]=min(dp[i][j],dp[i-1][j]+p[i].x-p[j].x);

#include<bits/stdc++.h>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int x;
int c;
}p[];
LL dp[][];
bool cmp(node one,node two)
{
return one.x<two.x;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&p[i].x,&p[i].c);
}
sort(p+,p++n,cmp);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
dp[i][j]=1e18;
}
dp[][]=p[].c;
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
dp[i][j]=min(dp[i][j],dp[i-][j]+p[i].x-p[j].x);
dp[i][i]=min(dp[i][i],dp[i-][j]+p[i].c);
}
}
LL ans=1e18;
for(int i=;i<=n;i++)
ans=min(ans,dp[n][i]);
printf("%ld\n",ans);
}

一维:

  1. 题解:
  2. dp1[i]表示前i个球的最小花费,dp2[i]表示固定第i个球的最小花费。
  3. 则dp2[i]=dp1[i-1]+cost[i]。
  4. dp1[i]=dp2[j]+ans;
  5. ans表示j~i之间的球滚到了j位置的花费
 #include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
struct node
{
ll id,cost;
}c[];
bool cmp(node a,node b)
{
return a.id<b.id;
}
ll dp1[],dp2[];
ll min1(ll a,ll b)
{
if(a>b)return b;
return a;
}
int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld%lld",&c[i].id,&c[i].cost);
sort(c+,c+n+,cmp);
dp1[]=dp2[]=c[].cost;
for(ll i=;i<=n;i++)
{
ll ans=;
dp1[i]=dp2[i]=dp1[i-]+c[i].cost;
for(ll j=i-;j>=;j--)
{
ans=ans+(c[j+].id-c[j].id)*(i-j);
dp1[i]=min1(dp1[i],dp2[j]+ans);
}
}
printf("%lld\n",dp1[n]);
return ;
}

题意:给你每个求的位置以及在这个点修卡点的费用,每个球都会向左滚,求让所有球停下来的最小费用

思路:DP, 先处理出当前点之前都跑到第一个点的距离和,然后用dp[i]表示到第i个点的最小费用,假设停在了第j个点,那么我们就要算上[j, i]所有点都跑到j的距离和,还有要算上修j的费用,最后减去[j, i]要跑到第1点的距离

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
const ll inf = 1e15;
const int maxn = ; struct Node {
int x, c;
bool operator <(const Node &a) const {
return x < a.x;
}
} node[maxn]; ll sum[maxn], dp[maxn]; int main() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d%d", &node[i].x, &node[i].c);
sort(node+, node++n);
sum[] = ;
memset(dp, , sizeof(dp));
for (int i = ; i <= n; i++)
sum[i] = sum[i-] + node[i].x - node[].x;
for (int i = ; i <= n; i++) {
dp[i] = inf;
for (int j = ; j <= i; j++)
dp[i] = min(dp[i], dp[j-]+sum[i]-sum[j-]-(ll)(node[j].x-node[].x)*(i-j+)+node[j].c);
}
printf("%lld\n", dp[n]);
return ;
}

Codeforces Problem - 38E - Let's Go Rolling!(DP)的更多相关文章

  1. 【CF】38E Let's Go Rolling! (dp)

    前言 这题还是有点意思的. 题意: 给你 \(n\) (\(n<=3000\)) 个弹珠,它们位于数轴上.给你弹珠的坐标 \(x_i\) 在弹珠 \(i\) 上面花费 \(C_i\) 的钱 可以 ...

  2. codeforces #260 DIV 2 C题Boredom(DP)

    题目地址:http://codeforces.com/contest/456/problem/C 脑残了. .DP仅仅DP到了n. . 应该DP到10w+的. . 代码例如以下: #include & ...

  3. Codeforces Round #260 (Div. 2)C. Boredom(dp)

    C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. Codeforces Round #658 (Div. 2) D. Unmerge(dp)

    题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...

  5. codeforces#FF DIV2C题DZY Loves Sequences(DP)

    题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...

  6. codeforces#1152D. Neko and Aki's Prank(dp)

    题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...

  7. Codeforces 766C:Mahmoud and a Message(DP)

    题目链接:http://codeforces.com/problemset/problem/766/C 题意 有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母 ...

  8. codeforces 811 C. Vladik and Memorable Trip(dp)

    题目链接:http://codeforces.com/contest/811/problem/C 题意:给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间 ...

  9. Educational Codeforces Round 16 E. Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

随机推荐

  1. HMM简单理解(来自quora&其他网上资料)

    转载自quora: 连接:https://www.quora.com/What-is-a-simple-explanation-of-the-Hidden-Markov-Model-algorithm ...

  2. Luogu-4248 [AHOI2013]差异

    \(\sum_{i<j}len(i)+len(j)\)比较简单,稍微想想就出来了,问题在于怎么求任意两个后缀的\(lcp\)长度之和 因为求\(lcp\)实际上就是一个对\(h\)数组求区间最小 ...

  3. java深入探究12-框架之Hibernate

    1.引入SSH框架 Struts框架,基于MVC 模式的应用层框架技术 Hibernate,基于持久层框架(数据访问层使用) Dao代码编写的几种方式: 1.原始jdbc操作,Connection/S ...

  4. MapReduce-多个输出(使用MultipleOutput,不指定reduce任务个数)

    多个输出 FileOutputFormat及其子类产生的文件放在输出目录下.每个reduce一个文件并且文件由分区号命名:part-r-00000,part-r-00001,等等.有时可能需要对输出的 ...

  5. java 读取src下的配置文件

    很多时候,我们都将配置文件放在eclipse的src目录下,这个位置,相当于,当导出可执行jar包后,配置文件放在和jar同级的目录中,比如jar包放在/opt目录下,则配置文件放在/opt下,则ja ...

  6. unity调用C++ dll文件

    首先建立Plugins文件夹,把dll文件放在里面 一一对应,我踩的坑是文件名加了后缀.dll,虽然不知道网上为什么都加了我这加了就报找不到dll文件错误,反正解决啦

  7. delphi 数据连接规范

    建议大家采用另外一种编码风格,不要在程序中到处都有这种LZ程序生成的代码: begin with qryMain do begin try Close; SQL.Clear; SQL.Add('Del ...

  8. 终端命令对字符串进行sha1、md5、base64、urlencode/urldecode

    sha1.md5.base64 mac $ echo -n foo|shasum 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 - $ 2c26b46b68ffc6 ...

  9. Maven下载 || 配置本地仓库 || IntelliJ IDEA配置Maven教程

    本文章主要介绍1.Maven下载   2.配置本地仓库Repository   3.IDEA配置Maven 三点. 相关博客: Eclipse配置Maven https://www.cnblogs.c ...

  10. Pdf 解密后复制文字乱码

    1.安装cajviewer 这个工具 2.用CAJviewer打开pdf文档 3.选择图像4.点文字识别,这时候就弹窗一个框,里面是可复制的文本,而且准确率比较高