uva 607 Scheduling Lectures
解决方案:dp思路:dp[i]代表上了前面i个topic用的课时最少数,D[i]代表上了前面i个topic的最低不满意度;
dp状态转移:dp[i]=min(dp[i],dp[j-1]+1),j>=1&&j<=i;D[i]则根据dp的状态来处理,若dp[i]==dp[j-1]+1时,D[i]=min(D[i],D[j-1]+dissatisfaction );
code:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1005;
const int inf=0x3f3f3f3f;
int T,L,C,n;
int dp[maxn];
int DI[maxn];
int sum[maxn];
inline int getDI(int t)
{
if(t==0) return 0;
else if(t>=1&&t<=10) return -1*C;
else
{
return (t-10)*(t-10);
}
}
int main()
{
int index=1;
while(scanf("%d",&n))
{
if(n&&index>1) printf("\n");
if(n==0) break;
scanf("%d%d",&L,&C);
sum[0]=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&T);
sum[i]=sum[i-1]+T;
}
dp[0]=0;
DI[0]=0;
for(int i=1; i<=n; i++)
{
dp[i]=inf;
DI[i]=inf;
for(int j=1; j<=i; j++)
{
if(sum[i]-sum[j-1]<=L)
{
int D=getDI(L-(sum[i]-sum[j-1]));
if(dp[i]>dp[j-1]+1)
{
dp[i]=dp[j-1]+1;
DI[i]=DI[j-1]+D;
}
else if(dp[i]==dp[j-1]+1)
{
DI[i]=min(DI[i],DI[j-1]+D);
}
}
}
}
printf("Case %d:\n",index++);
printf("Minimum number of lectures: %d\n",dp[n]);
printf("Total dissatisfaction index: %d\n",DI[n]);
}
return 0;
}