Codeforces 38E.Let's Go Rolling! (dp)

本文介绍了一道关于珠子在数轴上移动的问题,通过动态规划的方法找到最小花费来固定所有珠子的解决方案。文章详细解释了状态定义、状态转移方程,并附带实现代码。

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
inputCopy
3
2 3
3 4
1 2
output
5
inputCopy
4
1 7
3 1
5 10
6 1
output
11
题目大意是有一堆滚珠,有各自的在数轴上的位置,对于数轴的每个点可以用一定的钱去固定==滚珠会一直向左边移动,直到碰上某个被固定的点为止,滚珠滚动花费和滚动距离相等的钱,问让所有滚珠全部停止最少需要花费多少钱==
这题一开始就从第n颗滚珠贪心搞了几发,凉凉,于是想要dp一下,没想到dp[i][j]的含义也想错了,一开始以为代表第i颗在第j个位置停下来,然后发现根本写不出转移方程==

其实dp[i][j]代表了前i颗珠子最后一个被固定的点为j,这样表示状态转移方程就可以写了,然而还是一开始写错了==难道是太久没有写dp的缘故么。。。还是太菜
转移方程分两种情况 1.若i==j,则我们需要在前i-1颗中找到在哪个点花费最少,然后加上第i个点的花费。

2.i!=j,那么第i颗在第j个位置停下,由于i不小于j,所以前i-1颗也必定在j位置停下,这样就是dp[i-1][j]+i-j的距离,前缀和处理一波就好。
其实感觉有点像区间dp==

另外还有一个坑点,这题由于答案会特别大,差不多接近long long了,至少是远超int,平时习惯把inf定为0x3f3f3f3f,然后又wa了好几次==
代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define maxn 3010
#define ll long long
#define angel 0x3f3f3f3f
struct stick
{
    ll c;
    ll s;
}num[maxn];
int n;
bool cmp(stick x,stick y)
{
    return x.c<y.c;
}
ll sum[maxn];
ll dp[maxn][maxn];
ll Min(ll x,ll y)
{
    if(x>y)
        return y;
    return x;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld%lld",&num[i].c,&num[i].s);
    sort(num+1,num+1+n,cmp);
    sum[1]=0;
    for(int i=2;i<=n;i++)
        sum[i]=sum[i-1]+(num[i].c-num[i-1].c);//前缀和处理一波距离
    dp[1][1]=num[1].s;//第一颗珠子肯定在第一个点停下来
    for(int i=2;i<=n;i++)
    {
        ll tmp=1e18;//寻找dp[i-1][j]最小值
        for(int j=1;j<=i;j++)
        {
            if(i==j)
                dp[i][j]=tmp+num[j].s;
            else
            {
                tmp=Min(tmp,dp[i-1][j]);
                dp[i][j]=dp[i-1][j]+sum[i]-sum[j];
            }
        }
    }
    ll ans=1e19;//寻找最终结果,不要开小==
    for(int i=1;i<=n;i++)
        ans=Min(ans,dp[n][i]);
    printf("%lld\n",ans);
    return 0;
}
Java是一种具备卓越性能与广泛平台适应性的高级程序设计语言,最初由Sun Microsystems(现属Oracle公司)的James Gosling及其团队于1995年正式发布。该语言在设计上追求简洁性、稳定性、可移植性以及并发处理能力,同时具备动态执行特性。其核心特征与显著优点可归纳如下: **平台无关性**:遵循“一次编写,随处运行”的理念,Java编写的程序能够在多种操作系统与硬件环境中执行,无需针对不同平台进行修改。这一特性主要依赖于Java虚拟机(JVM)的实现,JVM作为程序与底层系统之间的中间层,负责解释并执行编译后的字节码。 **面向对象范式**:Java全面贯彻面向对象的设计原则,提供对封装、继承、多态等机制的完整支持。这种设计方式有助于构建结构清晰、模块独立的代码,提升软件的可维护性与扩展性。 **并发编程支持**:语言层面集成了多线程处理能力,允许开发者构建能够同时执行多项任务的应用程序。这一特性尤其适用于需要高并发处理的场景,例如服务器端软件、网络服务及大规模分布式系统。 **自动内存管理**:通过内置的垃圾回收机制,Java运行时环境能够自动识别并释放不再使用的对象所占用的内存空间。这不仅降低了开发者在内存管理方面的工作负担,也有效减少了因手动管理内存可能引发的内存泄漏问题。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值