题意:商品总价按四舍五入计算,n个物品最多可分\(d+1\)段,求最小代价
\(dp[i][j]\):\(j\)个物品分\(i\)段
注意一个技巧是只在需要分出新的段时才四舍五入(旧段结算),这样就避免了不知道分段具体位置无法\(dp\)的情况
数据量比较小就不使用滚动数组了
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar(‘\n‘)
#define blank putchar(‘ ‘)
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e5+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
ll dp[22][maxn],p[maxn];
int n,d;
inline ll cal(ll x){
return (x+5)/10*10;
}
int main(){
while(cin>>n>>d){
memset(dp,0x3f,sizeof dp);
rep(i,1,n) p[i]=read();
dp[1][1]=p[1];
rep(i,2,n) dp[1][i]=dp[1][i-1]+p[i];
rep(i,2,d+1){
rep(j,i,n){
dp[i][j]=min(cal(dp[i-1][j-1])+p[j],dp[i][j-1]+p[j]);
}
}
ll ans=oo;
rep(i,1,d+1) ans=min(ans,cal(dp[i][n]));
println(ans);
}
return 0;
}
原文地址:https://www.cnblogs.com/caturra/p/8527450.html
时间: 2024-10-09 07:24:34