int get_dist(const II &st, const II &ed) {
		if (st == ed) {
			return -1;
		}
		int d[64][64];
		memset(d, -1, sizeof(d));
		II dirs[4] = {II(-1,0),II(1,0),II(0,-1),II(0,1)};
		IIVec q;
		d[st.second][st.first] = 0;
		q.push_back(st);
		while (q.size() > 0) {
			IIVec nq;
			int i, j;
			for (i = 0; i < (int)q.size(); ++i) {
				const II &current = q[i];
				if (current == ed) {
					return d[current.second][current.first];
				}
				for (j = 0; j < 4; ++j) {
					int x = current.first + dirs[j].first;
					int y = current.second + dirs[j].second;
					if (x >= 0 && x < W && y >= 0 && y < H &&
							d[y][x] < 0 && fld[y][x] != '#') {
						nq.push_back(II(x, y));
						d[y][x] = d[current.second][current.first] + 1;
					}
				}
			}
			q = nq;
		}
		return -1;
	}
Example #2
0
	int solve(void) {
		IIVec q;
		q.push_back(II(sx, sy));
		for (int t = 1; q.size() > 0; ++t) {
			IIVec next;
			for (int i = 0; i < (int)q.size(); ++i) {
				const int dx[4] = { -1, 0, 1, 0 };
				const int dy[4] = { 0, -1, 0, 1 };
				for (int d = 0; d < 4; ++d) {
					int x = q[i].first + dx[d], y = q[i].second + dy[d];
					if (x >= 0 && y >= 0 && x < W && y < H && ((1 << (t % 4)) & z[y][x]) == 0) {
						if (x == gx && y == gy) {
							return t;
						}
						if (!vis[t % 4][y][x] && maze[y][x] == '.') {
							vis[t % 4][y][x] = 1;
							next.push_back(II(x, y));
						}
					}
				}
			}
			q = next;
		}
		return -1;
	}
Example #3
0
	int maxSongs(vector <int> duration, vector <int> tone, int T) {
		IIVec v;
		int N = (int)tone.size();
		int i, j, k;
		for (i = 0; i < N; ++i) {
			v.push_back(II(tone[i], duration[i]));
		}
		sort(v.begin(), v.end());

#define INF 0x3f3f3f3f
		int dp[64][64] = {};
		memset(dp, 0x3f, sizeof(dp));
		for (i = 0; i < N; ++i) {
			for (j = 0; j < i; ++j) {
				for (k = 1; k <= i; ++k) {
					dp[i][k+1] = min(dp[i][k+1], dp[j][k] + v[i].first-v[j].first + v[i].second);
				}
			}
			dp[i][1] = v[i].second;
		}

		int ans = 0;
		for (k = 1; k <= N; ++k) {
			for (i = 0; i < N; ++i) {
				if (dp[i][k] <= T) {
					ans = k;
				}
			}
		}
		return ans;
	}
Example #4
0
	int maxSongs(vector <int> duration, vector <int> tone, int T) {
		int ans = 0;

		IIVec v;
		for (int i = 0; i < (int)tone.size(); ++i) {
			v.push_back(II(tone[i], duration[i]));
		}
		sort(v.begin(), v.end());

		int dp[64][64];
		memset(dp, 0x3f, sizeof(dp));
		for (int i = 0; i < (int)v.size(); ++i) {
			for (int j = 0; j < i; ++j) {
				for (int k = 1; k <= i; ++k) {
					dp[i][k + 1] = min(dp[i][k + 1], dp[j][k] + abs(v[i].first - v[j].first) + v[i].second);
					if (dp[i][k + 1] <= T) {
						ans = max(ans, k + 1);
					}
				}
			}
			dp[i][1] = v[i].second;
			if (v[i].second <= T) {
				ans = max(ans, 1);
			}
		}
		return ans;
	}
Example #5
0
	double maxExpectation(int M, vector <int> day, vector <int> win, vector <int> gain) {
		Max = M;
		IIVec Days;
		int i;
		for (i = 0; i < (int)day.size(); ++i) {
			if (win[i] > 0 && gain[i] > 0) {
				Days.push_back(II(day[i], i));
			}
		}
		sort(Days.begin(), Days.end());

		_day.clear();
		_win.clear();
		_gain.clear();
		memset(memo, 0, sizeof(memo));
		for (i = 0; i < (int)Days.size(); ++i) {
			_day.push_back(Days[i].first);
			_win.push_back(win[Days[i].second] * 0.01);
			_gain.push_back(gain[Days[i].second]);
		}

		if (Days.size() <= 0 || M <= _day[0]) {
			return M;
		}
		return rec(0, M - _day[0]);
	}
	vector <int> minimalDifference(vector <int> h) {
		int N = (int)h.size();
		IIVec v;
		for (int i = 0; i < N; ++i) {
			v.push_back(II(h[i], i));
		}
		sort(v.begin(), v.end());
		vector <int> ans(N);
		int l = 0, r = N - 1;
		for (int i = 0; i < N; ++i) {
			if (i % 2) {
				ans[r--] = v[i].second;
			} else {
				ans[l++] = v[i].second;
			}
		}
		return ans;
	}
Example #7
0
int main(int argc, char *argv[])
{
	IIVec v;
	int N;
	cin >> N;
	for (int i = 0; i < N; ++i) {
		int a;
		cin >> a;
		v.push_back(II(a, i));
	}
	sort(v.begin(), v.end());
	int ans = 0;
	for (int i = 0; i < N; ++i) {
		ans += (i % 2) != (v[i].second % 2);
	}
	cout << ans / 2 << endl;
	return 0;
}
Example #8
0
	vector <int> construct(vector <int> x) {
		IIVec v;
		for (int i = 0; i != x.size(); ++i) {
			v.push_back(II(x[i], i));
		}
		sort(v.begin(), v.end());
		vector <int> ans;
		for (int i = 0; i != v.size(); ++i) {
			if (v[i].first > (i + 1)) {
				ans.resize(1);
				ans[0] = -1;
				break;
			}
			for (int j = 0; j < (v[i].first - 1); ++j) {
				ans.push_back(v[i].second);
				ans.push_back(v[j].second);
			}
		}
		return ans;
	}