[codeforces 339]C. Xenia and Weights

[codeforces 339]C. Xenia and Weights

试题描述

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.

输入

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).

输出

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.

输入示例

0000000101
3

输出示例

YES
8 10 8

数据规模及约定

见“输入

题解

一开始我想贪心,但强大的 cf 数据一巴掌把我打了回来。于是就开始想怎么 dp,发现可以设 f(i, j, k) 表示添加第 i 个砝码,使得砝码 i 所对应的天平中总质量比另一个天平总质量多 j,且当前砝码质量为 k,那么就可以转移了,枚举一下当前添加的砝码的质量,状态数 O(100m),转移 O(10),总时间复杂度相当于 O(m2)。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;

const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
    if(Head == Tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        Tail = (Head = buffer) + l;
    }
    return *Head++;
}
int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 15
#define maxm 1010
char has[maxn];
int m, f[maxm][maxn][maxn], ans[maxm], cnt;
struct Node {
	int i, j, k;
	Node() {}
	Node(int _1, int _2, int _3): i(_1), j(_2), k(_3) {}
} fa[maxm][maxn][maxn];

int main() {
	scanf("%s", has + 1);
	m = read();
	
	for(int j = 1; j <= 10; j++)
		if(has[j] == '1') f[1][j][j] = 1;
	for(int i = 1; i <= m; i++)
		for(int j = 1; j <= 10; j++)
			for(int k = 1; k <= 10; k++)
				fa[i][j][k] = Node(-1, -1, -1);
	for(int i = 2; i <= m; i++)
		for(int j = 1; j <= 9; j++)
			for(int k = 1; k <= 10; k++)
				for(int x = 1; x <= 10; x++)
					if(has[x] == '1' && x != k && x > j && f[i-1][x-j][k])
						f[i][j][x] = 1, fa[i][j][x] = Node(i-1, x-j, k);
	
	bool ok = 0;
	for(int j = 1; j <= 10; j++) {
		for(int k = 1; k <= 10; k++)
			if(f[m][j][k]) {
				ok = 1;
				Node u = Node(m, j, k);
				while(u.i >= 0) {
					ans[++cnt] = u.k;
					u = fa[u.i][u.j][u.k];
				}
				break;
			}
		if(ok) break;
	}
	
	if(!ok) return puts("NO"), 0;
	puts("YES");
	for(int i = cnt; i > 1; i--) printf("%d ", ans[i]); printf("%d\n", ans[1]);
	
	return 0;
}
/*
1111000011
16
1111000011
1
*/

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5833826.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值