CodeForces - 38E Let's Go Rolling!

來源:互聯網
上載者:User

標籤:des   style   blog   os   io   for   2014   art   

Description

On a number axis directed from the left rightwards, n marbles with coordinatesx1,?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 numberi is equal to ci, numberci 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 nextn lines contain the descri_ptions of the marbles in pairs of integersxi,ci (?-?109?≤?xi,?ci?≤?109). The numbers are space-separated. Each descri_ption 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.

Sample Input

Input
32 33 41 2
Output
5
Input
41 73 15 106 1
Output
11

題意:給你每個求的位置以及在這個點修卡點的費用,每個球都會向左滾,求讓所有球停下來的最小費用

思路: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 = 3005;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 = 1; i <= n; i++)scanf("%d%d", &node[i].x, &node[i].c);sort(node+1, node+1+n);sum[0] = 0;memset(dp, 0, sizeof(dp));for (int i = 1; i <= n; i++)sum[i] = sum[i-1] + node[i].x - node[1].x;for (int i = 1; i <= n; i++) {dp[i] = inf;for (int j = 1; j <= i; j++)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);}printf("%lld\n", dp[n]);return 0;}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.