All pastes #624370 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-07-18 06:32 UTC
rendered paste body
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;

class BrokenButtons
{
public:
	vector<int> v;
	string itos(int i) {
		char buf[100];
		sprintf(buf, "%d", i);
		return string(buf);
	}
	
	bool fine(int x) {
		if (x < 0) return false;
		string s = itos(x);
		for (int i=0; i<s.size(); ++i) {
			if (find(v.begin(), v.end(), s[i]-'0') != v.end())
				return false;
		}
		return true;
	}
	
	int mem[1000001];
	int minPresses(int x, vector<int> v1) {
		int best1 = 10000000;
		v = v1;
		for (int i=0; i<500001; ++i) {
			if (fine(x-i)) {
				best1 = itos(x-i).size()+i; break;
			} else if (fine(x+i)) {
				best1 = itos(x+i).size()+i; break;
			}
		}
		int best2 = 10000000;
		for (int i=0; ; ++i) {
			if (x==100+i || x==100-i) {
				best2 = i; break;
			}
		}
		return min(best1, best2);
	}
};