码迷,mamicode.com
首页 > 其他好文 > 详细

HNU Cent Savings (DP)

时间:2015-08-02 21:42:05      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:算法   dp   

Cent Savings
Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:65536KB
Total submit users: 54, Accepted users: 36
Problem 13345 : No special judgement
Problem description

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, book- ing 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 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

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
#include<stdio.h>
#include<string.h>
const int N = 2005;
int main()
{
    int dp[N][25],n,d,sum[N];
    while(scanf("%d%d",&n,&d)>0)
    {
        for(int i=0; i<=n; i++)
            for(int j=1; j<=d; j++)
             dp[i][j]=1<<29;
        for(int j=0; j<=d; j++)
             dp[0][j]=0;
        sum[0]=0;
        for(int i=1; i<=n; i++){
            scanf("%d",&sum[i]);
            sum[i]+=sum[i-1];
        }
        for(int i=1; i<=n; i++)
            if(sum[i]%10>4)
                dp[i][0]=sum[i]-sum[i]%10+10;
            else
                dp[i][0]=sum[i]-sum[i]%10;

        for(int i=1; i<=n; i++)
        for(int j=1; j<=d; j++)
        for(int ti=i-1; ti>=0; ti--)
        {
            int ad=sum[i]-sum[ti];
            if(ad%10>4){
                ad=ad-ad%10+10;
            }
            else ad=ad-ad%10;
            if(dp[i][j]>dp[ti][j-1]+ad)
                dp[i][j]=dp[ti][j-1]+ad;
        }
        printf("%d\n",dp[n][d]);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HNU Cent Savings (DP)

标签:算法   dp   

原文地址:http://blog.csdn.net/u010372095/article/details/47210551

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!