NC220698. C:Statues
描述
输入描述
The first line of the input contains two space-separated integers: N the number of street lights and Kthe number of statues. The K following lines each contains two space-separated integers, the i + 1-thline containing the integers Pi and Si describing the i-th statue. Pi gives the number of the street lightunder which the statue is and Si gives its size.
输出描述
The output should contain a single line containing a single integer: the minimum amount of timeneeded to move statues such that each statue is under a different street light and such that the size ofstatues grows with the street light numbers under which they are.
示例1
输入:
3 3 1 3 2 2 3 1
输出:
8
说明:
Because there are as many street lights as there are statues we needto position the statue of size 1 at street light 1 (which takes units of times), leave the statue of size 2 atstreet light 2, and move the statue of size 3 to the street light 3(which takes units of times). In total this takes units of time.示例2
输入:
4 3 2 2 3 2 4 1
输出:
3
说明:
C++(clang++11) 解法, 执行用时: 27ms, 内存消耗: 544K, 提交时间: 2021-04-02 22:36:03
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 5e3 + 10; pair<ll,ll> p[N]; ll dp[N],li[N],n,k,i,j; int main() { scanf("%lld%lld",&n,&k); for(i=1;i<=k;i++)scanf("%lld%lld",&p[i].second,&p[i].first); sort(p+1,p+1+k); for(i=1;i<=k;i++){ for(j=i;j<=n;j++)dp[j]=li[j-1]+p[i].first*abs(p[i].second-j); for(j=i;j<=n;j++)li[j]=i!=j?min(li[j-1],dp[j]):dp[j]; }printf("%lld",li[n]); }