uvalive6952 - Cent Sa dp

6952 Cent Savings
To host a regional contest like NWERC a lot of preparation
is necessary: organizing rooms and computers,
making a good problem set, inviting contestants, designing
T-shirts, booking hotel rooms and so on. I am
responsible for going shopping in the supermarket.
When I get to the cash register, I put all my n
items on the conveyor belt and wait until all the other
customers in the queue in front of me are served.
While waiting, I realize that this supermarket recently
started to round the total price of a purchase
to the nearest multiple of 10 cents (with 5 cents being
rounded upwards). For example, 94 cents are
rounded to 90 cents, while 95 are rounded to 100.
It is possible to divide my purchase into groups
and to pay for the parts separately. I managed to find d dividers to divide my purchase in up to d + 1
groups. I wonder where to place the dividers to minimize the total cost of my purchase. As I am
running out of time, I do not want to rearrange items on the belt.
Input
The input file contains several test cases, each of them as described below.
The input consists of:
• one line with two integers n (1 ≤ n ≤ 2000) and d (1 ≤ d ≤ 20), the number of items and the
number of available dividers;
• one line with n integers p1, …, pn (1 ≤ pi ≤ 10000 for 1 ≤ i ≤ n), the prices of the items in cents.
The prices are given in the same order as the items appear on the belt.
Output
For each test case, output the minimum amount of money needed to buy all the items, using up to d
dividers.
Sample Input
5 1
13 21 55 60 42
5 2
1 1 1 1 1
Sample Output
190
0**
題意:

用最多d個劃分d+1個組使各組之和最小,和會四捨五入,如若和爲15則付20,和爲114則付110

思路:
dp[i][k+1],即在i點劃分爲第k+1組的最小和,就會發現dp[i][k+1]=dp[j][k]+(sum[j]-sum[i])/10*10或(sum[j]-sum[j])/10*10+10,(判斷條件就是如果(sum[j]-sum[i])%10<5就是前者,否則是後者)
j爲前i-1位,dp[j][k]即j位劃分爲第k組的最小和

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf=99999999;
int p[2005];
int sum[2005];
int dp[2005][25];
int main()
{
    int n,d;
    while(scanf("%d%d",&n,&d)!=-1)
    {
        memset(sum,0,sizeof sum);
        for(int i=1;i<=n;i++){
            scanf("%d",&p[i]);
            sum[i]=sum[i-1]+p[i];
        }
        memset(dp,0,sizeof dp);
        for(int i=0;i<=n;i++)
            for(int j=0;j<=d+1;j++)
            dp[i][j]=inf;
        for(int i=1;i<=n;i++){
            int temp=sum[i]%10;
            if(temp>=5)
           dp[i][1]=sum[i]/10*10+10;
            else
                dp[i][1]=sum[i]/10*10;
        }
        for(int k=1;k<=d;k++)
        for(int i=k;i<=n;i++){
           for(int j=0;j<i;j++){
               int res=(sum[i]-sum[j]);
               if(res%10>=5) res=res/10*10+10;
               else res=res/10*10;
               //printf("res=%d\n",res);
               dp[i][k+1]=min(dp[j][k]+res,dp[i][k+1]);
               /*if(dp[i][k]>=inf)
                   puts("1-------");
               else
               printf("%d %d %d dp[j][k]=%d dp[i][k+1]=%d\n",k,i,j,dp[j][k],dp[i][k+1]);*/
           }
        }
        int ans=dp[n][d+1];
       for(int i=1;i<=d+1;i++){
            ans=min(dp[n][i],ans);
        }
        printf("%d\n",ans);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章