voidWork(int N, int G, int M, const vector<pair<int, bool>>& guests, vector<int>* ans){ set<int> clock_end, anti_clock_end; for (int i = 0; i < G; i++) { if (guests[i].second) { int end = (guests[i].first + M) % N; clock_end.insert(end); } else { int end = (guests[i].first - M) % N; if (end < 0) end += N; anti_clock_end.insert(end); } } vector<int> clock_last_time(N), anti_clock_last_time(N); for (int i = 0; i < N; i++) { clock_last_time[i] = -1; anti_clock_last_time[i] = -1; } for (auto it = clock_end.begin(); it != clock_end.end(); it++) { int last; if (it == clock_end.begin()) { last = *clock_end.rbegin(); } else { auto tmp = it; tmp--; last = *tmp; } int now = M; for (int pos = *it; pos != (last + 1) % N; pos = (pos + N - 1) % N) { clock_last_time[pos] = now; now--; if (now < 0) break; } if (now >= 0) clock_last_time[(last + 1) % N] = now; } for (auto it = anti_clock_end.begin(); it != anti_clock_end.end(); it++) { int next; if ((*it) == (*anti_clock_end.rbegin())) { next = *anti_clock_end.begin(); } else { auto tmp = it; tmp++; next = *tmp; } int now = M; for (int pos = *it; pos != (next - 1 + N) % N; pos = (pos + 1) % N) { anti_clock_last_time[pos] = now; now--; if (now < 0) break; } if (now >= 0) anti_clock_last_time[(next - 1 + N) % N] = now; } vector<int> clock_sum(N), anti_clock_sum(N); for (int i = 0; i < N; i++) { int res = max(clock_last_time[i], anti_clock_last_time[i]); if (res != -1 && res == clock_last_time[i]) clock_sum[i] = 1; if (res != -1 && res == anti_clock_last_time[i]) anti_clock_sum[i] = 1; if (i > 0) { clock_sum[i] += clock_sum[i - 1]; anti_clock_sum[i] += anti_clock_sum[i - 1]; } } ans->resize(G); for (int i = 0; i < G; i++) { if (guests[i].second) { int end = (guests[i].first + M) % N; auto it = clock_end.find(end); int last; if (it == clock_end.begin()) { last = *clock_end.rbegin(); } else { auto tmp = it; tmp--; last = *tmp; } int len = min(end == last ? N : (end - last + N) % N, M + 1); int start = (end - len + 1 + N) % N; (*ans)[i] = GetSum(clock_sum, start, end); } else { int end = (guests[i].first - M) % N; if (end < 0) end += N; auto it = anti_clock_end.find(end); int next; if ((*it) == (*anti_clock_end.rbegin())) { next = *anti_clock_end.begin(); } else { auto tmp = it; tmp++; next = *tmp; } int len = min(end == next ? N : (next - end + N) % N, M + 1); int to = (end + len - 1 + N) % N; (*ans)[i] = GetSum(anti_clock_sum, end, to); } } }
intmain(){ int T; cin >> T; for (int test = 1; test <= T; test++) { int N, G, M; cin >> N >> G >> M; vector<pair<int, bool>> guests(G); for (int i = 0; i < G; i++) { int x; cin >> x; guests[i].first = x - 1; char ch = getchar(); while (ch != 'C' && ch != 'A') ch = getchar(); guests[i].second = (ch == 'C'); } vector<int> ans; Work(N, G, M, guests, &ans); cout << "Case #" << test << ": "; for (int x : ans) cout << x << " "; cout << endl; } return0; }
longlongWork(int K, int N, const vector<int>& X, const vector<int>& C){ vector<pair<int, int>> spots(N); for (int i = 0; i < N; i++) { spots[i] = make_pair(X[i], C[i]); } sort(spots.begin(), spots.end()); int left_num = K / 2; int right_num = K - left_num; vector<longlong> left(N), right(N); multiset<int> now; longlong sum = 0; for (int i = 0; i < N; i++) { if (now.size() == left_num) left[i] = sum; else left[i] = inf64; sum += spots[i].second - spots[i].first; now.insert(spots[i].second - spots[i].first); if (now.size() > left_num) { auto it = --now.end(); sum -= *it; now.erase(it); } } now.clear(); sum = 0; for (int i = N - 1; i >= 0; i--) { if (now.size() == right_num) right[i] = sum; else right[i] = inf64; sum += spots[i].second + spots[i].first; now.insert(spots[i].second + spots[i].first); if (now.size() > right_num) { auto it = --now.end(); sum -= *it; now.erase(it); } } longlong res = inf64; for (int i = 0; i < N; i++) { longlong left_cost = left[i] + (longlong)left_num * spots[i].first; longlong right_cost = right[i] - (longlong)right_num * spots[i].first; res = min(res, left_cost + right_cost + spots[i].second); } return res; }
intmain(){ int T; cin >> T; for (int test = 1; test <= T; test++) { int K, N; cin >> K >> N; vector<int> X(N), C(N); for (int i = 0; i < N; i++) cin >> X[i]; for (int i = 0; i < N; i++) cin >> C[i]; auto ans = Work(K, N, X, C); cout << "Case #" << test << ": " << ans << endl; } return0; }