zoukankan      html  css  js  c++  java
  • [P1441]砝码称重 (搜索+DP)

    对于我这种蒟蒻,是很不错的一题了。

    dfs搜索当前状态

    满足时DP

    比较坑的地方就是起始的地方

    我一开始从1开始,搜索写的是从0开始。

    后来就统一用0开始的了。

    #include<bits/stdc++.h>
    #define max(a,b) (a>b?a:b)
    using namespace std;
    int n, m;
    int a[25],ans;
    bool f[2005];
    bool vis[25];
    void dp()//背包 滚存优化是看一个大佬的题解的
    {
        memset(f, 0, sizeof f);
        f[0] = true;
        int ret = 0, tot = 0;
        for (int i = 0; i < n; i++)
        {
            if (vis[i]) continue;
            for (int j = tot; j >= 0; j--)if (f[j] && !f[j + a[i]]) f[j + a[i]] = true, ret++;
            tot += a[i];
        }
        ans = max(ans, ret);
    }
    void dfs(int x,int y)//搜索
    {
        if (y > m) return;
        if (x > n) return;
        if (x == n && y==m) {
            dp();//满足的时候DP
            return;
        }
        dfs(x + 1, y);
        vis[x] = true;
        dfs(x+1, y + 1);
        vis[x] = false;
        return;
    }
    int main()
    {
        memset(vis, 0, sizeof(vis));
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
        }
        dfs(0,0);
        printf("%d", ans);
        return 0;
    }
  • 相关阅读:
    SRM482
    SRM481
    SRM480
    SRM479
    SRM478
    vue-cli 3 is not a modual err
    .vimrc
    css3 导入字体
    class []的用法
    RK61 Keyboard Use
  • 原文地址:https://www.cnblogs.com/lincold/p/9821964.html
Copyright © 2011-2022 走看看