• Codeforces Problem


    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[3005];  
    LL dp[3005][3005];  
    bool cmp(node one,node two)  
    {  
        return one.x<two.x;  
    }  
    int main()  
    {  
        int n;  
        scanf("%d",&n);  
        for(int i=1;i<=n;i++)  
        {  
            scanf("%d%d",&p[i].x,&p[i].c);  
        }  
        sort(p+1,p+1+n,cmp);  
        for(int i=1;i<=n;i++)  
        {  
            for(int j=1;j<=n;j++)  
                dp[i][j]=1e18;  
        }  
        dp[1][1]=p[1].c;  
        for(int i=2;i<=n;i++)  
        {  
            for(int j=1;j<i;j++)  
            {  
                dp[i][j]=min(dp[i][j],dp[i-1][j]+p[i].x-p[j].x);  
                dp[i][i]=min(dp[i][i],dp[i-1][j]+p[i].c);  
            }  
        }  
        LL ans=1e18;  
        for(int i=1;i<=n;i++)  
            ans=min(ans,dp[n][i]);  
        printf("%ld
    ",ans);  
    }  
    View Code

    一维:

    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位置的花费  
     1 #include<cstdio>  
     2 #include<algorithm>  
     3 using namespace std;  
     4 #define ll long long   
     5 struct node  
     6 {  
     7     ll id,cost;  
     8 }c[3003];  
     9 bool cmp(node a,node b)  
    10 {  
    11     return a.id<b.id;  
    12 }  
    13 ll dp1[3003],dp2[3003];  
    14 ll min1(ll a,ll b)  
    15 {  
    16     if(a>b)return b;  
    17     return a;  
    18 }  
    19 int main()  
    20 {  
    21     int n;scanf("%d",&n);  
    22     for(int i=1;i<=n;i++)  
    23         scanf("%lld%lld",&c[i].id,&c[i].cost);  
    24     sort(c+1,c+n+1,cmp);  
    25     dp1[1]=dp2[1]=c[1].cost;  
    26     for(ll i=2;i<=n;i++)  
    27     {  
    28         ll ans=0;  
    29         dp1[i]=dp2[i]=dp1[i-1]+c[i].cost;  
    30         for(ll j=i-1;j>=1;j--)  
    31         {  
    32             ans=ans+(c[j+1].id-c[j].id)*(i-j);  
    33             dp1[i]=min1(dp1[i],dp2[j]+ans);  
    34         }  
    35     }  
    36     printf("%lld
    ",dp1[n]);  
    37     return 0;  
    38 }  

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

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

     1 #include <iostream>  
     2 #include <cstring>  
     3 #include <algorithm>  
     4 #include <cstdio>  
     5 using namespace std;  
     6 typedef long long ll;  
     7 const ll inf = 1e15;  
     8 const int maxn = 3005;  
     9   
    10 struct Node {  
    11     int x, c;  
    12     bool operator <(const Node &a) const {  
    13         return x < a.x;  
    14     }  
    15 } node[maxn];  
    16   
    17 ll sum[maxn], dp[maxn];  
    18   
    19 int main() {  
    20     int n;  
    21     scanf("%d", &n);  
    22     for (int i = 1; i <= n; i++)  
    23         scanf("%d%d", &node[i].x, &node[i].c);  
    24     sort(node+1, node+1+n);  
    25     sum[0] = 0;  
    26     memset(dp, 0, sizeof(dp));  
    27     for (int i = 1; i <= n; i++)   
    28         sum[i] = sum[i-1] + node[i].x - node[1].x;  
    29     for (int i = 1; i <= n; i++) {  
    30         dp[i] = inf;  
    31         for (int j = 1; j <= i; j++)  
    32             dp[i] = min(dp[i], dp[j-1]+sum[i]-sum[j-1]-(ll)(node[j].x-node[1].x)*(i-j+1)+node[j].c);  
    33     }     
    34     printf("%lld
    ", dp[n]);  
    35     return 0;  
    36 }  
  • 相关阅读:
    冲刺第四天
    冲刺第三天
    构建之法阅读笔记03
    冲刺第二天
    第八周学习进度条
    冲刺第一天
    第七周学习进度条
    软件工程课堂练习:返回一个一维整数数组中最大子数组的和
    团队项目成员和题目
    调用TerminateProcess是无法触发DLL_PROCESS_DETACH的
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8847151.html
Copyright © 2020-2023  润新知