这道题题意不想说了,跑了640ms,感觉水过去了,应该能通过单调队列优化,很长时间没碰已经不知道怎么写了,就说说现在的写法吧。

状态定义很关键:dp[i][j]把前j个topic放在前i堂课.

因为这道题中的topic不能跳,必须按顺序,那么我们可以用贪心先求出最少的课程数,凭感觉证明这个贪心的做法是准确的,且找不到反例。

然后根据dp前后状态递推方程:

dp[i][j]=max(dp[i][j],dp[i-1][k]+solve(k+1,j));(sum[k+1][j]<=L)

我们显然写个二重循环,里面再找k的时候写个循环,最差情况是O(n^3),前两个循环顺序随便写,其中一种写法快200ms,但感觉能用单调队列来把那个找k的直接优化成O(1)的做法,以后想到再补。

这道题格式很恶心,注意每个分块之间也要有空行。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
int cas,cass;
int l,c,n;
int t[1005];
int sum[1005];
int dp[1005][1005];
//dp[i][j]把前j个topic放在前i堂课
//dp[i][j]=max(dp[i][j],dp[i-1][k]+solve(k+1,j));(sum[k+1][j]<=L) int solve(int x)
{
if(x==0)
return 0;
else if(x>0&&x<=10)
return -c;
else
return (x-10)*(x-10);
}
int main()
{
// freopen("input.txt","r",stdin); scanf("%d",&cass);
while(cass--)
{
int cas=1;
while(scanf("%d",&n)==1&&n)
{
memset(sum,0,sizeof(sum));
if(cas>=2)
printf("\n");
printf("Case %d:\n\n",cas++);
scanf("%d%d",&l,&c);
for(int i=1;i<=n;i++)
{
scanf("%d",&t[i]);
if(i>1)
sum[i]=sum[i-1];
sum[i]+=t[i];
}
//贪心
int ans=1;
int cnt=0;
for(int i=1;i<=n;i++)
{
cnt+=t[i];
if(cnt>l)
{
ans++;
cnt=t[i];
}
}
//
printf("Minimum number of lectures: %d\n",ans);
memset(dp,0X7f,sizeof(dp));
dp[0][0]=0;
for(int i=1;i<=ans;i++)
for(int j=1;j<=n;j++)
{
for(int k=j-1;k>=0&&sum[j]-sum[k]<=l;k--)
dp[i][j]=min(dp[i][j],dp[i-1][k]+solve(l-(sum[j]-sum[k])));
}
printf("Total dissatisfaction index: %d\n",dp[ans][n]);
}
if(cass!=0)
printf("\n");
} }

zoj1183 Scheduling Lectures的更多相关文章

  1. UVA 607 二十二 Scheduling Lectures

    Scheduling Lectures Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  2. 递推DP UVA 607 Scheduling Lectures

    题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...

  3. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  4. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  7. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  8. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  9. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

随机推荐

  1. Sharepoint2010 通过 WebFeature 修改web.config

    using System;using System.Runtime.InteropServices;using System.Security.Permissions;using Microsoft. ...

  2. Java 基础之 static 静态

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  3. uva 123 Searching Quickly

     Searching Quickly  Background Searching and sorting are part of the theory and practice of computer ...

  4. 关于web标准的理解(转)

    从开始接触所谓web标准化,差不多也有两年多的时候了吧.从最初的疑惑和彷徨一直到现在,经历了每一个阶段.这段过程是痛苦的也是开心的,痛苦的是这个过程中没有人可以帮你答疑解惑,所有的问题都要你自己去解决 ...

  5. css3变形讲解

    Transform变形:可以实现文字或者图片旋转.缩放.倾斜和移动,并且该元素下的所有子元素都随着父元素一样. 既然接触到transform,我们就可以做好多3d的效果啦 旋转:transform:r ...

  6. netbeans ide怎么添加yii代码提示

    菜单栏选择工具->选项->php界面里的全局包含路径中选择framework这个文件夹就可以了

  7. Financial Management--hdu1064

    Financial Management Problem Description Larry graduated this year and finally has a job. He’s makin ...

  8. ctype.h库函数

    头文件ctype.h声明了一组用于分类和转换单个字符的函数.所有的函数都接收一个int型的参数,并返回一个int——返回的int可能代表一个字符,也可能代表的是bool值(0为假,非0为真). 你可能 ...

  9. ADO.NET帮助类DBHelper

    一. DBHelper帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  10. 杭电oj 3361

    Tips:字符在计算机中都是以ASCII码形式保存,直接以char形式输出ASCII码即可. #include<stdio.h> int main() { int T; scanf(&qu ...