• Xenia and Weights(Codeforces Round #197 (Div. 2)+DP)


    题目链接

    传送门

    思路

    (dp[i][j][k])表示第(i)次操作放(j)后与另一堆的重量差为(k)是否存在。

    代码实现如下

    #include <set>
    #include <map>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <ctime>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
     
    typedef long long LL;
    typedef pair<LL, LL> pLL;
    typedef pair<LL, int> pLi;
    typedef pair<int, LL> piL;;
    typedef pair<int, int> pii;
    typedef unsigned long long uLL;
     
    #define lson rt<<1
    #define rson rt<<1|1
    #define lowbit(x) x&(-x)
    #define name2str(name) (#name)
    #define bug printf("*********
    ")
    #define debug(x) cout<<#x"=["<<x<<"]" <<endl
    #define FIN freopen("in","r",stdin)
    #define IO ios::sync_with_stdio(false),cin.tie(0)
     
    const double eps = 1e-8;
    const int mod = 1e9 + 7;
    const int maxn = 1e5 + 7;
    const double pi = acos(-1);
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3fLL;
     
    int m, vis[15];
    bool dp[1007][15][105];
    char s[15];
    vector<int> vec;
     
    int main() {
        scanf("%s%d", s, &m);
        for(int i = 0; i < 10; ++i) {
            if(s[i] == '1') vis[i+1] = 1;
        }
        for(int i = 1; i <= 10; ++i) {
            if(vis[i]) dp[1][i][i] = 1;
        }
        for(int i = 2; i <= m; ++i) {
            for(int j = 1; j <= 10; ++j) {
                if(!vis[j]) continue;
                for(int k = 1; k <= 10; ++k) {
                    if(!vis[k] || j == k) continue;
                    for(int t = 1; t <= k; ++t) {
                        if(!dp[i-1][k][t]) continue;
                        if(j >= t) dp[i][j][j-t] = 1;
                    }
                }
            }
        }
        int flag = 0;
        for(int i = 1; i <= 10; ++i) {
            for(int j = 1; j <= 10; ++j) {
                if(dp[m][i][j]) {
                    flag = 1;
                    break;
                }
            }
            if(flag) break;
        }
        if(!flag) puts("NO");
        else {
            puts("YES");
            int num1 = 0, num2 = 0;
            for(int i = 1; i <= 10; ++i) {
                for(int j = 1; j <= 10; ++j) {
                    if(dp[m][i][j]) {
                        num1 = i, num2 = j;
                        break;
                    }
                }
                if(num1) break;
            }
            vec.push_back(num1);
            for(int i = m - 1; i >= 1; --i) {
                for(int j = 1; j <= 10; ++j) {
                    if(dp[i][j][num1-num2] && j != num1) {
                        vec.push_back(j);
                        num2 = num1 - num2;
                        num1 = j;
                        break;
                    }
                }
            }
            for(int i = (int)vec.size() - 1; i >= 0; --i) {
                printf("%d%c", vec[i], i == 0 ? '
    ' : ' ');
            }
        }
        return 0;
    }
    
  • 相关阅读:
    C# WinForm 关于窗体最大化时的是否全屏效果与是否遮盖任务栏
    C# winform 无边框 窗体的拖动
    lenovo 联想笔记本ideapad 320c-15改装win7问题
    解决WIN7第一次开机冷启动QQ未响应的办法
    WIN10X64_LTSB2016极限精简版by双心
    RAMOS和SSD对比
    联想IDEAPAD 320C-15笔记本显卡驱动问题
    WIN7以上系统安装VB6的解决办法,附上个批处理。
    吐槽下银联1分钱乘公交
    QQ聊天框变成方框口口口口的解决办法
  • 原文地址:https://www.cnblogs.com/Dillonh/p/11162592.html
Copyright © 2020-2023  润新知