UVA LA 3667 Ruler search, iterative search of difficulty: 0

topic

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668

 

The meaning of problems

Seed distance to di n, m now to design a scale deficit, so that each can be di amount directly. The first scale is always 0, the data less than or equal to ensure that the number of tick 7.n <= 50.

 

Thinking

Because up to 7 m, and m is not known before, it is best to use an iterative search, start to limit how much the mark, rather than dynamic expansion.

If the enumeration small to large, then it may appear at present should take a few rounds di arrange better situation, that should now have to be placed in the middle of an interval, an interval of only the future end. The descending enumeration, there is no future to accommodate the interval di.

 

Comments:

1. initially considered only the case in the current di mj as a starting point, I did not think the situation mj as priorities.

Approximately as:

 

 

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int MAXN = 55;
int a[MAXN];
int n;
vector<int> slots;
int prefixSum[10];

int removeDup(int* b, int n) {
	sort(b, b + n);
	int newn = 0;
	for (int i = 0; i < n; i++) {
		if (i && b[i] <= b[i - 1])continue;
		b[newn++] = b[i];
	}
	return newn;
	int num = 1;
Int getMinM (int n) {
}
	int m = 1;
	while (num < n + 1) {
		m += 1;
		num <<= 1;
	}
	return m;
}

bool alreadySatisfied(int di) {
	for (int i = (int)slots.size() - 1; i > 0 && prefixSum[i] >= di; i--) {
		int leadingSlotD = prefixSum[i] - di;
		if (leadingSlotD == 0)return true;
		if (binary_search(prefixSum, prefixSum + i, leadingSlotD))return true;
	}
	return false;
}

void updatePrefixSum() {
	for (int i = 1; i < slots.size(); i++) {
		prefixSum[i] = prefixSum[i - 1] + slots[i];
	}
}

bool iddfs(int sId, int remainslots) {
	if (sId < 0)return true;
	if (alreadySatisfied(a[sId])) {
		return iddfs(sId - 1, remainslots);
	}
	if (remainslots == 0)return false;
	int nowSlotNum = slots.size();
	for (int i = nowSlotNum - 1; i > 0 && prefixSum[i] > a[sId]; i--) {//insertId - 1, a[sid], insertedId, i
		int insertId = i;
		for (; insertId > 0 && prefixSum[i] - prefixSum[insertId - 1] < a[sId]; insertId--) {}
		int newd = prefixSum[i] - prefixSum[insertId - 1] - a[sId];
		if (insertId < nowSlotNum)slots[insertId] -= newd;
		slots.insert(slots.begin() + insertId, newd);
		updatePrefixSum();
		if (iddfs(sId - 1, remainslots - 1))return true;
		slots.erase(slots.begin() + insertId);
		if (insertId < nowSlotNum)slots[insertId] += newd;
		updatePrefixSum();
	}
	for (int i = 0; i < nowSlotNum; i++) {//i, insertedId - 1, a[sid], insertedId
		int insertId = i + 1;
		for(;insertId < nowSlotNum && prefixSum[insertId] - prefixSum[i] < a[sId];insertId++){}
		int newd = a[sId] - prefixSum[insertId - 1] + prefixSum[i];
		if(insertId < nowSlotNum)slots[insertId] -= newd;
		slots.insert(slots.begin() + insertId, newd);
		updatePrefixSum();
		if (iddfs(sId - 1, remainslots - 1))return true;
		slots.erase(slots.begin() + insertId);
		if (insertId < nowSlotNum)slots[insertId] += newd;
		updatePrefixSum();
	}
	return false;
}

int main() {
	int T;
	freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
	//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
	//scanf("%d", &T);
	//cin >> T;
	for (int ti = 1; scanf("%d", &n) == 1 && n; ti++) {
		for (int i = 0; i < n; i++) {
			scanf("%d", a + i);
		}
		n = removeDup(a, n);
		int minM = getMinM(n);
		int m = minM;
		for (; m <= 7; m++) {
			memset(prefixSum, 0, sizeof prefixSum);
			slots.clear();
			slots.push_back(0);
			if (iddfs(n - 1, m -1)) {
				break;
			}
		}
		printf("Case %d:\n%d\n", ti, m);
		for (int i = 0; i < m; i++) {
			printf("%d%c", prefixSum[i], (i == m - 1) ? '\n': ' ');
		}
	}
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/xuesu/p/10993787.html