Gym 101482C Cent Savings( dp )

Problem C
Cent Savings
Time limit: 5 seconds
Picture by Tijmen Stam via Wikimedia Commons, cc by-sa
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 consists of:
• one line with two integers n (1 ≤ n ≤ 2 000) and d (1 ≤ d ≤ 20), the number of items
and the number of available dividers;
• one line with n integers p1, . . . pn (1 ≤ pi ≤ 10 000 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 1 Sample Output 1
5 1
13 21 55 60 42
190
Sample Input 2 Sample Output 2
5 2
1 1 1 1 1
0
题意:
n个物品,可以分成 k+1 组,每组的花费和四舍五入(14=10),求最小花费。
思路:

  1. 一开始都错题,以为可以打乱顺序,自闭了几个小时(比赛的时候这题过穿了。。。而我不会)。
  2. 其实是一个 dp dp[i][j],表示前 i 个物品分 j 组,然后枚举j组和j-1组的分界点转移就好了。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
int dp[maxn][30],a[maxn],pre[maxn];
int f(int x){
    int v=x%10;
    if(v>=5) return x+10-v;
    return x-v;
}
int main() {
    std::ios::sync_with_stdio(false);
    int n,k,sum=0,x,v,ans=0x3f3f3f3f;
    memset(dp,0x3f3f3f3f,sizeof(dp));
    cin>>n>>k;k++;
    for(int i=1;i<=n;i++){
       cin>>a[i];
       pre[i]=pre[i-1]+a[i];
       dp[i][1]=f(pre[i]);
    }
    for(int i=2;i<=n;i++){
        for(int j=2;j<=min(i,k);j++){
            for(int h=j-1;h<=i-1;h++){
                dp[i][j]=min(dp[i][j],dp[h][j-1]+f(pre[i]-pre[h]));
            }
        }
    }
    for(int i=1;i<=k;i++){
        ans=min(ans,dp[n][i]);
    }
    printf ("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值