uva 812 - Trade on Verweggistan(暴力)

题目连接:uva 812 - Trade on Verweggistan


题目大意:给出n个序列,每个序列给出k,表示有k个元素,每行只能从头取,取i(i≤k),现在要在每个序列前取走c[i]个,保证说sum = ∑| 10 - num| (num为所选数的集合)最大,并且输出可以选择的个数,超过10个的话只要输出最小的10个。


解题思路:对于每个序列遍历一次,找出最大值和可以组成最大值的个数,注意可以取0个,然后递归求解出所有的组成情况。


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

const int tmp = 10;
const int N = 60;
const int M = 1005;
const int INF = 0x3f3f3f3f;

int n, ans, cnt[M];
vector<int> v[N];

void init() {
	ans = 0;
	memset(cnt, 0, sizeof(cnt));

	int m, num;
	for (int i = 0; i < n; i++) {
		scanf("%d", &m); v[i].clear();
		int sum = 0, Max = 0;
		v[i].push_back(0);

		for (int j = 0; j < m; j++) {
			scanf("%d", &num);
			sum += (tmp - num);
			if (sum > Max) {
				v[i].clear();
				v[i].push_back(j+1);
				Max = sum;
			} else if (sum == Max) {
				v[i].push_back(j+1);
			}
		}
		ans += Max;
	}
}

void dfs(int d, int s) {
	if (d >= n) {
		cnt[s] = 1;
		return ;
	}

	int top = v[d].size();
	for (int i = 0; i < top; i++) {
		dfs(d + 1, s + v[d][i]);
	}
}

void solve() {
	dfs(0, 0);
	
	int c = 0;

	for (int i = 0; i < M; i++) {
		if (c >= tmp) break;
		if (cnt[i]) {
			printf(" %d", i);
			c++;
		}
	}
	printf("\n");
}

int main() {
	int cas = 0;
	while (scanf("%d", &n) == 1 && n) {
		init();
		if (cas) printf("\n");
		printf("Workyards %d\n", ++cas);
		printf("Maximum profit is %d.\n", ans);
		printf("Number of pruls to buy:");
		solve();
	}
	return 0;
}


你可能感兴趣的:(uva 812 - Trade on Verweggistan(暴力))