这道题题意不想说了,跑了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. js 创建html元素 渲染html元素

    var p1 = document.getElementById('p1'); //添加的元素类型及属性var newNode = document.createElement("input ...

  2. Node.js实践第一天

    实践案例一 表单提交 demo.html <!doctype html> <html> <head> <title>表单提交</title> ...

  3. js 进阶笔记

    JS中substr和substring的用法和区别 substr和substring都是JS截取字符串函数,两者用法很相近, substr方法 返回一个从指定位置开始的指定长度的子字符串. strin ...

  4. 【.NetRemoting-3】2015.09.18

    [Remoting程序的基本实现] [一]服务程序集 [1]服务对象必须要求继承[MBR,MarshalByRefObject] [二]服务端应用程序 [1]注册通道 [两种类型的通道] [A]发送请 ...

  5. USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)

    这题可真是又让我找到了八数码的感觉...哈哈. 首先,第一次见题,没有思路,第二次看题,感觉是搜索,就这样写下来了. 这题我几乎是一个点一个点改对的(至于为什么是这样,后面给你看一个神奇的东西),让我 ...

  6. Redis 作为缓存服务器的配置

    随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...

  7. 创建view

    IF EXISTS(SELECT 1 FROM sys.views WHERE name='V_PARENT_CLIENT') DROP VIEW V_PARENT_CLIENT GO create ...

  8. ado.net实现一个通知公告功能

    一.建立相应的数据库,此处就不多说了,相信大家都非常了解

  9. undefined reference to `_sbrk', `_write', `_lseek', `_read'

    现象: 在用GCC编译嵌入式MCU程序时,由于使用了第三方的库,出现了类似undefined reference to `_sbrk', `_write', `_lseek', `_read'的连接错 ...

  10. 【测试技术】ant在测试中的使用@文件以及目录的读写删和复制

    ant其实就是一个java的打包工具,存在的时间已经很久了,很多同行在使用中可能就是用,对为什么要用它,能够怎么用没有更多的了解: ---------------------------------- ...