Codeforces 339C Xenia and Weights

C. Xenia and Weights
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.

Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.

You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done.

Input

The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).

Output

In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can putm weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.

If there are multiple solutions, you can print any of them.

Sample test(s)
input
0000000101
3
output
YES
8 10 8
input
1000000000
2
output
NO

解題思路:首先我們枚舉第一步選取哪個數,然後在第一個數確定的情況下采用最優的策略選取接下來的數,這裏的最優策略是選取的數加起來比另一堆多,且多的數目越少越好,這樣能保證我們的方案是最優的。最後就是對於特殊情況判斷一下即可,總的複雜度爲O(10*1000*10).
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
const int inf = 0x3f3f3f3f;
bool mark[20];
int ans[1010];
int pos;


int main() {


    //freopen("aa.in", "r", stdin);


    int m;
    string s;
    int cnt = 0;
    memset(mark, false, sizeof(mark));
    cin >> s >> m;
    for(int i = 0; i < 10; ++i) {
        if(s[i] == '1') {
            cnt++;
            mark[i+1] = true;
        }
    }
    if(cnt == 0) {
        printf("NO\n");
    } else if(cnt == 1) {
        if(m == 1) {
            printf("YES\n");
            for(int i = 1; i <= 10; ++i) {
                if(mark[i]) {
                    printf("%d\n", i);
                    break;
                }
            }
        } else {
            printf("NO\n");
        }
    } else {
        bool ok;
        for(int k = 1; k <= 10; ++k) {
            if(!mark[k]) continue;
            int tot[2];
            int p = -1, id = 1;
            int tp;
            tot[0] = k;
            tot[1] = 0;
            int tmp = inf;
            pos = 0;
            ans[pos++] = k;
            for(int i = 1; i < m; ++i) {
                ok = false;
                tmp = inf;
                for(int j = 1; j <= 10; ++j) {
                    if(!mark[j]) continue;
                    if(j == p) continue;
                    if(tot[id] + j > tot[id^1]) {
                        if(tmp > tot[id] + j) {
                            tmp = tot[id] + j;
                            tp = j;
                            ok = true;
                        }
                    }
                }
                tot[id] = tmp;
                ans[pos++] = tp;
                p = tp;
                id ^= 1;
                if(!ok) break;
            }
            if(ok) {
                printf("YES\n");
                for(int i = 0; i < pos; ++i) {
                    printf("%d ", ans[i]);
                }
                printf("\n");
                break;
            }
        }
        if(!ok) {
            printf("NO\n");
        }
    }
    return 0;
}
發佈了230 篇原創文章 · 獲贊 2 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章