ZOJ-1183-Scheduling Lectures
可以用贪心求最小讲课次数,贪心策略也很好想,就是对于任意主题,能早讲就早讲。这种方案的讲课次数一定是最少的,但是不满意指标不一定是最小,然后再利用动态规划求在最少讲课次数前提下的最小不满意指标。
- 方法一(自己想到的)
Accepted 1183 C++11 1020 4240 #include "bits/stdc++.h"
using namespace std;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
// arr[i]表示第i个主题所需时间,dp[u][j]表示第j个主题在第u节课讲的情况下前u节课的最小不满意指标
int arr[MAXN], dp[MAXN][MAXN];
int t, n, l, c;
// 计算不满意指标
int getDI(int k) {
if (k == ) {
return ;
}
if (k <= ) {
return -c;
}
return (k - ) * (k - );
}
// 动态规划的实现,计算在最少讲课次数前提下的最小不满意指标
int getTDI(int m) {
memset(dp, INF, sizeof(dp));
dp[][] = ;
for (int i = ; i <= n; i++) {
int k = ;
for (int j = i; j; j--) {
k += arr[j];
if (k <= l) {
for (int u = ; u <= m; u++) {
dp[u][i] = min(dp[u][i], dp[u - ][j - ] + getDI(l - k));
}
}
}
}
return dp[m][n];
}
int main() {
scanf("%d", &t);
while (t--) {
int ca = ;
while (scanf("%d", &n) && n) {
int minLect = , k = ;
scanf("%d%d", &l, &c);
for (int i = ; i <= n; i++) {
scanf("%d", &arr[i]);
// 采用贪心求最小讲课次数
k += arr[i];
if (k > l) {
minLect++;
k = arr[i];
}
}
if (ca != ) {
puts("");
}
printf("Case %d:\n\n", ++ca);
printf("Minimum number of lectures: %d\n", minLect);
printf("Total dissatisfaction index: %d\n", getTDI(minLect));
}
if (t != ) {
puts("");
}
}
return ;
}自定义函数getTDI里面的那句if (k <= l)应该改成if (k > l) {break;}的,脑抽了。
- 方法二(看了参考书之后改进的)
Accepted 1183 C++11 10 300 #include "bits/stdc++.h"
using namespace std;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
// 每个主题需要的时间
int arr[MAXN];
// 主题1 ~ i (1 <= i < n)的最少讲课次数
int minLec[MAXN];
// 与主题相应的最小不满意指标
int minDis[MAXN];
int t, n, l, c;
// 计算不满意指标
int getDI(int k) {
if (k == ) {
return ;
}
if (k <= ) {
return -c;
}
return (k - ) * (k - );
}
// 动态规划的实现,计算在最少讲课次数及相应的最小不满意指标
int dp() {
memset(minLec, INF, sizeof(minLec));
memset(minDis, INF, sizeof(minDis));
minLec[] = minDis[] = ;
for (int i = ; i <= n; i++) {
int sum = ;
for(int j = i; j; j--) {
sum += arr[j];
if (sum > l) {
break;
}
if (minLec[j - ] + < minLec[i]) {
minLec[i] = minLec[j - ] + ;
minDis[i] = minDis[j - ] + getDI(l - sum);
}
minDis[i] = min(minDis[i], minDis[j - ] + getDI(l - sum));
}
}
}
int main() {
scanf("%d", &t);
while (t--) {
int ca = ;
while (scanf("%d", &n) && n) {
scanf("%d%d", &l, &c);
for (int i = ; i <= n; i++) {
scanf("%d", &arr[i]);
}
if (ca != ) {
puts("");
}
printf("Case %d:\n\n", ++ca);
dp();
printf("Minimum number of lectures: %d\n", minLec[n]);
printf("Total dissatisfaction index: %d\n", minDis[n]);
}
if (t != ) {
puts("");
}
}
return ;
}从二维优化到一维极大的降低了时间复杂度和空间复杂度。果然还是很高明的。
ZOJ-1183-Scheduling Lectures的更多相关文章
- UVA 607 二十二 Scheduling Lectures
Scheduling Lectures Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- zoj1183 Scheduling Lectures
这道题题意不想说了,跑了640ms,感觉水过去了,应该能通过单调队列优化,很长时间没碰已经不知道怎么写了,就说说现在的写法吧. 状态定义很关键:dp[i][j]把前j个topic放在前i堂课. 因为这 ...
- 递推DP UVA 607 Scheduling Lectures
题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...
- 别人整理的DP大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
- dp题目列表
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- [转] POJ DP问题
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- POJ动态规划题目列表
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
随机推荐
- 美团:WSDM Cup 2019自然语言推理任务获奖解题思路
WSDM(Web Search and Data Mining,读音为Wisdom)是业界公认的高质量学术会议,注重前沿技术在工业界的落地应用,与SIGIR一起被称为信息检索领域的Top2. 刚刚在墨 ...
- python 网络请求、下载
#获取qq群中的群成员qq号import requestsmembers = []url = 'https://qun.qq.com/cgi-bin/qun_mgr/search_group_memb ...
- @EnableWebMvc WebMvcConfigurer
Spring注解@EnableWebMvc使用坑点解析 https://blog.csdn.net/zxc123e/article/details/84636521 @EnableWebMvc,Web ...
- malloc 底层实现及原理
摘要:偶尔看到面试题会问到 malloc 的底层原理,今天就来记录一下,毕竟学习要“知其所以然”,这样才会胸有成竹. 注:下面分析均是基于 linux 环境下的 malloc 实现.步骤是:先总结结论 ...
- JavaSE--压缩
package util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java ...
- JS导出、导入EXCEL(案例)
插件下载地址:http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js 1.导出excel <!DOCTYPE html> <html> ...
- c++语法(1)
#include<iostream> #include<windows.h> using namespace std; class Parents { public: ; // ...
- 使用pythonnet调用halcon脚本
最近的项目中遇到了使用python程序结合不同部分,其中包括使用halcon处理拍摄到的图像. halcon本身提供了c++与.NET的开发库,但无python库,网上有pyhalcon之类的库,但功 ...
- 控制台输出<迷你DVD管理>
使用顺序.选择.循环.跳转语句 数组 功能实现菜单显示和切换 输入的数字不符合要求直接退出程序 用户可以选择新增.查看. 删除.借出.归还.退出 思路分析 使用switch语句实现菜单选择 使用do- ...
- TPO6-1Powering the Industrial Revolution
The source had long been known but not exploited. Early in the eighteenth century, a pump had come i ...