Scheduling Lectures
You are teaching a course and must cover n ( ) topics. The length of each lecture is L ( ) minutes. The topics require () minutes each. For each topic, you must decide in which lecture it should be covered. There are two scheduling restrictions:
-
1.
- Each topic must be covered in a single lecture. It cannot be divided into two lectures. This reduces discontinuity between lectures. 2.
- Topic i must be covered before topic i + 1 for all . Otherwise, students may not have the prerequisites to understand topic i + 1.
With the above restrictions, it is sometimes necessary to have free time at the end of a lecture. If the amount of free time is at most 10 minutes, the students will be happy to leave early. However, if the amount of free time is more, they would feel that their tuition fees are wasted. Therefore, we will model the dissatisfaction index (DI) of a lecture by the formula:
where C is a positive integer, and t is the amount of free time at the end of a lecture. The total dissatisfaction index is the sum of the DI for each lecture.
For this problem, you must find the minimum number of lectures that is needed to satisfy the above constraints. If there are multiple lecture schedules with the minimum number of lectures, also minimize the total dissatisfaction index.
Input
The input consists of a number of cases. The first line of each case contains the integer n, or 0 if there are no more cases. The next line contains the integers L and C. These are followed by n integers .
Output
For each case, print the case number, the minimum number of lectures used, and the total dissatisfaction index for the corresponding lecture schedule on three separate lines. Output a blank line between cases.
Sample Input
6 30 15 10 10 10 10 10 10 10 120 10 80 80 10 50 30 20 40 30 120 100 0
Sample Output
Case 1: Minimum number of lectures: 2 Total dissatisfaction index: 0 Case 2: Minimum number of lectures: 6 Total dissatisfaction index: 2700
dp[ i ][ j ]表示前i节课,覆盖j个知识点的最小不满意度。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf=99999999;
const int maxn=1010;
int sum[maxn],dp[maxn][maxn],a[maxn],n,L,C,cnt,co;
void initial()
{
for(int i=0; i<maxn; i++)
{
dp[i][0]=0;
for(int j=1; j<maxn; j++) dp[i][j]=inf;
}
cnt=0;
}
void input()
{
sum[0]=0;
scanf("%d %d",&L,&C);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
}
void solve1()
{
int w=0;
for(int i=1; i<=n; i++)
{
w+=a[i];
if(w>=L)
{
cnt++;
if(w==L) w=0;
else w=a[i];
}
}
if(w) cnt++;
}
void solve2()
{
for(int i=1; i<=cnt; i++)
for(int j=1; j<=n; j++)
{
if(sum[j]>i*L) break;
for(int k=j-1;k>=0 && sum[j]-sum[k]<=L;k--)
{
int t=L-(sum[j]-sum[k]),val;
if(t==0) val=0;
else if(t>=1 && t<=10) val=-C;
else val=(t-10)*(t-10);
dp[i][j]=min(dp[i][j],dp[i-1][k]+val);
}
}
if(co) printf("\n");
printf("Case %d:\n",++co);
printf("Minimum number of lectures: %d\n",cnt);
printf("Total dissatisfaction index: %d\n",dp[cnt][n]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
initial();
input();
solve1();
solve2();
}
return 0;
}