uva 607 记忆化搜索

题意:

有n个topic,每个topic的时间长度是ti。定义一个Lecture的时间长度为L。

问将这些topic放进Lecture中,最少需要几个Lecture,注意一个topic必须在一个Lecture内,不能断,且topic要按顺序。

然后又定义了一个学生不满意度的函数,用每个Lecture剩下的时间作为自变量,要求在Lecture数相同的情况下,不满意度最小。


解析:

最少Lecture很简单,扫一遍。

不满意度用记忆化搜索处理。

状态转移方程:

dp[ i ] [ j ] = min ( dp[ i ] [ j ] ,  dp [ i - 1 ] [ j - 1 ] + getscore(ti - sum) )

dp[i][j] 表示 i 个lecture上 j 个topic的最小不满意度。

对于当前lecture,当前topic选不选,选,则为dp [ i - 1 ] [ j - 1 ] + getscore(ti - sum);不选则为dp[i][j]。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int maxn = 1000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

int n, ti, c;
int topic[maxn];
int dp[maxn][maxn];
bool vis[maxn][maxn];//dp maybe negative

int getscore(int x)
{
    if (x == 0)
        return 0;
    else if (x <= 10)
        return -c;
    else
        return (x - 10) * (x - 10);
}

int dfs(int i, int j)
{
    int& res = dp[i][j];
    if (vis[i][j])
        return res;

    if (i == 0)
        return j ? inf : 0;

    vis[i][j] = true;
    res = inf;
    int sum = 0;
    for (int k = j; k > 0; k--)
    {
        sum += topic[k];
        if (ti < sum)
            break;
        res = min(res, dfs(i - 1, k - 1) + getscore(ti - sum));
    }
    return res;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int ncase = 0;
    while (~scanf("%d", &n) && n)
    {
        scanf("%d%d", &ti, &c);
        memset(dp, 0, sizeof(dp));
        memset(vis, false, sizeof(vis));
        int ans = 1;
        int sum = 0;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &topic[i]);
            sum += topic[i];
            if (ti < sum)
            {
                sum = topic[i];
                ans++;
            }
        }
        if (ncase)
            printf("\n");
        printf("Case %d:\n", ++ncase);
        printf("Minimum number of lectures: %d\n", ans);
        printf("Total dissatisfaction index: %d\n", dfs(ans, n));
    }
    return 0;
}


### 关于记忆化搜索的练习题 在洛谷平台上有许多涉及记忆化搜索的经典题目,这些题目可以帮助学习者更好地理解动态规划与深度优先搜索相结合的方法。以下是几个推荐的记忆化搜索练习题及其背景介绍: #### 题目一:P4170 [CQOI2007] 涂色 此题属于经典的 DP 类型问题之一,主要考察如何通过记忆化的方式优化暴力枚举的过程[^1]。 该题的核心在于利用状态压缩技术存储已经处理过的子结构,并结合 DFS 的方式逐步扩展解空间。 #### 题目二:P1464 Function 这是一个典型的递归加记忆化的入门级题目[^2]。它强调了在搜索过程中记录中间结果的重要性,从而避免必要的重复计算。具体实现上可以通过定义一个辅助数组 `f[x][y]` 来保存已访问的状态值。 #### 题目三:UVA - 1629 Cake Slicing 这篇文章提到的一道经典例题展示了记搜的优势所在[^3]。通过对同切割方案的结果进行缓存操作,可以显著降低时间复杂度并提高效率。 #### 题目四:滑雪 (P1434) 作为一道非常受欢迎的记忆化搜索练习题,“滑雪”模拟了一个二维网格上的最长下降路径求解过程[^5]。给定一张地图表示各个位置的高度分布情况后,我们需要找到从任意起点出发能够达到的最大步数长度。 ```python from functools import lru_cache @lru_cache(None) def dfs(x, y): res = 1 directions = [(0,-1),(-1,0),(0,+1),(+1,0)] for dx, dy in directions: nx, ny = x + dx, y + dy if 0<=nx<R and 0<=ny<C and grid[nx][ny]<grid[x][y]: res = max(res, dfs(nx, ny)+1 ) return res R,C=map(int,input().split()) grid=[list(map(int,input().strip().split()))for _ in range(R)] ans=0 for i in range(R): for j in range(C): ans=max(ans ,dfs(i,j)) print(ans) ``` 上述代码片段实现了基于 Python 的解决方案,其中运用装饰器 @lru_cache 实现自动化的函数调用结果缓存功能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值