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("*********\n")
#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 ? '\n' : ' ');
        }
    }
    return 0;
}
posted @ 2019-07-10 11:06  Dillonh  阅读(168)  评论(0编辑  收藏  举报