UVA 607 Scheduling Lectures(DP)

来源:互联网 发布:苹果cms如何绑定分类 编辑:程序博客网 时间:2024/05/06 18:40

Description

Download as PDF

You are teaching a course and must cover n ( $1 \le n \le 1000$) topics. The length of each lecture is L ( $1 \le L \le 500$) minutes. The topics require $t_1, t_2, \dots, t_n$ ( $1 \le t_i \le L$) 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 $1 \le i < n$. 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: 

\begin{displaymath}DI = \cases{0 & if $t=0$, \cr-C & if $1 \le t \le 10$, \cr(t-10)^2 & otherwise}\end{displaymath}

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 byn integers $t_1, t_2, \dots, t_n$.

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 

630 1510101010101010120 1080801050302040301201000

Sample Output 

Case 1:Minimum number of lectures: 2Total dissatisfaction index: 0Case 2:Minimum number of lectures: 6Total dissatisfaction index: 2700



状态比较容易去想:我的做法先贪心,得到最小节数,然后DP,得到最小代价。复杂度有点高。勉强过去。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;int a[1100];int dp[1010][1010];int len[1010];int n,L,C;int getdi(int t){    if(t==0)        return 0;    else if(t>=1&&t<=10)        return -C;    else        return (t-10)*(t-10);}int main(){    int cas=1;    while(~scanf("%d",&n)&&n)    {        if(cas>1) puts("");        scanf("%d%d",&L,&C);        len[0]=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            len[i]=len[i-1]+a[i];        }        int sum=0,ans=0;        for(int i=1;i<=n;i++)        {            if(sum+a[i]<=L)              sum+=a[i];            else            {                sum=a[i];                ans++;            }        }        if(sum) ans++;        CLEAR(dp,INF);        dp[0][0]=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=min(i,ans);j++)            {                if(len[i]>j*L) continue;                for(int p=i-1;p>=j-1;p--)                {                    if(dp[p][j-1]==INF) continue;                    if(len[i]-len[p]>L) break;                    if(len[i]-len[p]<=L)                        dp[i][j]=min(dp[i][j],dp[p][j-1]+getdi(L-(len[i]-len[p])));                }            }        }        printf("Case %d:\n",cas++);        printf("Minimum number of lectures: %d\n",ans);        printf("Total dissatisfaction index: %d\n",dp[n][ans]);    }    return 0;}


0 0
原创粉丝点击