UVA - 607 Scheduling Lectures

本文介绍了一个使用动态规划算法解决讲座分配问题的例子。通过最小化总不满指数来确定最佳讲座分割方案,考虑了每段讲座长度对听众满意度的影响。

简单的dp

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAX 1010
using namespace std;

int s[MAX],dp[MAX][MAX];
int n,l,c,cases=1;

void init(){
	memset(dp,0x7f,sizeof(dp));
	dp[0][0]=0;
}

int main(){
	while(cin>>n&&n){
		cin>>l>>c;
		for(int i=1;i<=n;i++)
			cin>>s[i];
		init();
		for(int i=1;i<=n;i++){
			for(int j=1;j<=i;j++){
				int temp=0;
				for(int t=i;t>j-1;t--){
					temp+=s[t];
					if(temp>l)
						break;
					if(dp[t-1][j-1]==0x7f7f7f7f)//注意不一定所有的情况都能得到
						continue;
					if(temp==l)
						dp[i][j]=min(dp[i][j],dp[t-1][j-1]);
					else if(temp>=l-10)
						dp[i][j]=min(dp[i][j],dp[t-1][j-1]-c);
					else{
						dp[i][j]=min(dp[i][j],dp[t-1][j-1]+(l-temp-10)*(l-temp-10));
					}
				}
			}
		}
		/*for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++)
				cout<<dp[i][j]<<" ";
			cout<<endl;
		}*/
		if(cases!=1)
			cout<<endl;
		for(int i=1;i<=n;i++)
			if(dp[n][i]!=0x7f7f7f7f){
				printf("Case %d:\nMinimum number of lectures: %d\nTotal dissatisfaction index: %d\n",cases++,i,dp[n][i]);
				break;
			}
	}
	return 0;
}


### Spring Boot Starter Scheduling 使用与配置 Spring Boot 提供了一个强大的工具来简化定时任务的开发,即 `spring-boot-starter` 中内置的支持调度功能。以下是关于如何启用和配置调度功能的相关说明。 #### 启用调度支持 为了在应用程序中使用调度功能,需要确保项目依赖项中包含了 `spring-boot-starter` 的核心库。默认情况下,Spring Boot 已经提供了对调度的支持,只需引入以下 Maven 或 Gradle 依赖即可: 对于 Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> ``` 对于 Gradle: ```gradle implementation 'org.springframework.boot:spring-boot-starter' ``` #### 配置调度器 要在 Spring Boot 应用程序中启用调度功能,需在主类上添加 `@EnableScheduling` 注解。此注解会激活 Spring 的计划任务机制[^1]。 示例代码如下: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 创建定时任务 创建一个服务类并定义定时执行的方法。可以使用 `@Scheduled` 注解指定任务的触发条件。常见的触发方式包括固定延迟 (`fixedDelay`) 和 Cron 表达式 (`cron`)。 示例代码展示了一种基于固定时间间隔的任务调度: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("当前时间为:" + System.currentTimeMillis()); } @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次 public void fixedCronJob() { System.out.println("每分钟打印一次日志"); } } ``` 上述代码中的第一个方法每隔 5 秒钟运行一次,而第二个方法则按照设定的 Cron 时间表达式定期运行[^2]。 #### 自定义线程池 如果希望自定义用于调度任务的线程池,可以通过实现 `ThreadPoolTaskExecutor` 来完成这一目标。例如,可以在配置类中定义一个 Bean 并设置其属性以满足特定需求。 示例代码如下: ```java import org.springframework.context.annotation.Bean; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration public class TaskConfig { @Bean(name = {"applicationTaskExecutor", "taskExecutor"}) public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(10); executor.setQueueCapacity(25); executor.initialize(); return executor; } } ``` 以上代码片段展示了如何通过注入的方式定制化线程池大小和其他参数[^3]。 --- #### 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值