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;
	}
Beispiel #2
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;
	}
	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;
	}
Beispiel #4
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;
}
Beispiel #5
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;
	}
Beispiel #6
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;
	}
	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;
	}
Beispiel #8
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]);
	}
Beispiel #9
0
bool solve(int N, int M, IIVec &walls, int &flavors, IntVec &pillars) {
	IntSet V[2000];		// vertexes in a room
	IntSet F[2000];		// flavors in a room
	IntVec C[2001];		// vertex connection

	int i, j;
	for (i = 0; i < N; ++i) {
		V[0].insert(i);
	}
	int rooms = 1;

	for (i = 0; i < N; ++i) {
		C[i].push_back((i-1+N)%N);
		C[i].push_back((i+1)%N);
	}

	for (i = 0; i < (int)walls.size(); ++i) {
		int s = min(walls[i].first, walls[i].second) - 1;
		int e = max(walls[i].first, walls[i].second) - 1;
		for (j = 0; j < rooms; ++j) {
			if (V[j].count(s) > 0 && V[j].count(e) > 0) {
				break;
			}
		}
		if (j >= rooms) {
			// error
			return false;
		}
		IntSet a, b;
		a.insert(s);
		a.insert(e);
		b.insert(s);
		b.insert(e);
		bool f = false;
		IntSet::const_iterator it;
		for (it = V[j].begin(); it != V[j].end(); ++it) {
			if (!f) {
				a.insert(*it);
			} else {
				b.insert(*it);
			}
			if (*it == s || *it == e) {
				f = !f;
			}
		}
		V[j] = a;
		V[rooms++] = b;
		C[s].push_back(e);
		C[e].push_back(s);
	}

	flavors = N;
	for (i = 0; i < rooms; ++i) {
		flavors = min((int)V[i].size(), flavors);
	}

	// fill the first room
	{
		int flavor = 1;
		IntSet::const_iterator it;
		for (it = V[0].begin(); it != V[0].end(); ++it) {
			int pillar = *it;
			if (flavor <= flavors) {
				set_flavor(rooms, V, F, pillars, pillar, flavor);
			} else {
				// fill with different color
				IntSet s;
				for (i = 0; i < (int)C[pillar].size(); ++i) {
					s.insert(pillars[C[pillar][i]]);
				}
				for (i = 1; ; ++i) {
					if (s.count(i) <= 0) {
						set_flavor(rooms, V, F, pillars, pillar, i);
						break;
					}
				}
			}
			++flavor;
		}
	}

	int filled_rooms, room;
	for (filled_rooms = 1; filled_rooms < N; ++filled_rooms) {
		bool found = false;
		for (room = 0; room < rooms; ++room) {
			int c = 0;
			IntSet::const_iterator it;
			for (it = V[room].begin(); it != V[room].end(); ++it) {
				if (pillars[*it] > 0) {
					++c;
				}
			}
			if (c >= 2 && c < (int)V[room].size()) {
				found = true;
				break;
			}
		}
		if (!found) {
			break;
		}
		fill_room(rooms, flavors, V, F, C, pillars, room);
	}

	for (i = 0; i < rooms; ++i) {
		if ((int)F[i].size() < flavors) {
			return false;
		}
	}
	for (i = 0; i < N; ++i) {
		if (pillars[i] <= 0 || pillars[i] > flavors) {
			return false;
		}
	}

	return true;
}