题意
礼堂共有 排,每排 个座位(左 个 A、B、C,右 个 D、E、F,过道在 C 与 D 之间)。
初始每个座位被一名考生占用。有 个考生将依次提前交卷离开。第 个离开的考生位于 。
离开时,他必须前往最前面或最后面的保密室,途中经过的还在座位上的考生数记为 ,进入保密室时室内已有的人数记为 。
不满度的计算方式是 (若未离开则不满度为 )。
所有考生协作,求最小的不满度之和。
思路
1. 经过人数的计算
考生从座位走到过道时,会经过同排的考生(在 C/D 侧),然后沿过道走到第 排(前保密室)或第 排(后保密室)。
过道上的考生只计算还在座位上的,已经离开的座位不计数。
- 设当前第 个离开的考生座位为 ,同排同一侧需要经过的考生数取决于该侧内更靠近过道的座位是否还在。
- 过道上的考生数:前保密室需要统计 排的过道考生(每排两个过道座位 C 和 D),后保密室统计 排的过道考生。
- 经过人数 = 同排沿途人数 + 对侧过道人数(若要对侧过道需经过该座位的对侧过道座位) + 过道前缀/后缀人数。
2. 保密室等待人数
第 个离开的考生进入保密室时,室内已有 人,即之前选择同一个保密室的人数。
设选择前保密室的人数为 ,则后保密室人数为 。 的累加贡献为
3. 转化为选前/后决策
对于第 个考生,设他去前保密室的不满意度为 (不含 部分),去后保密室为 。
令 ,则选择前保密室时贡献 ,选择后保密室时贡献 。
记所有考生默认选后保密室的总和为 ,设选择前保密室的考生集合为 ,,则总不满度为
因此,只需将 从小到大排序,枚举前 个最小的 作为选择前保密室的考生,计算取最小值。
4. 维护过道人数
需要动态维护每排过道座位(C 和 D)的占用情况。因为考生依次离开,过道座位会逐渐变空。
使用树状数组维护前缀和,支持单点减和区间查询。
实现步骤
- 读入 ,记录每个座位离开的时间顺序(没有离开的标记为 )。
- 预处理每个考生若去前/后保密室时的 值(不含 部分):
- 同排沿途人数:根据 col 和同侧更靠近过道的座位是否仍在。
- 对侧过道人数:若 col 在左侧(A、B、C),则需经过对侧过道 D(如果还在);若在右侧则经过 C。
- 过道前缀/后缀人数:用 BIT 维护当前过道座位数。
- 计算出每个考生的 和 ,进而得到 ,同时累加 。
- 将 排序,计算前缀和。
- 枚举前保密室人数 ,计算总不满度,取最小值。
- 输出结果(结果可能超过 64 位,需用 128 位整数)。
代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
using ll = long long;
using i128 = __int128_t;
int n, m, A, B;
int leave_time[100005][6];
struct BIT {
int n;
vector<int> tree;
BIT(int n_) : n(n_), tree(n_ + 2, 0) {}
void add(int idx, int delta) {
for (; idx <= n; idx += idx & -idx) tree[idx] += delta;
}
int sum(int idx) {
int res = 0;
for (; idx > 0; idx -= idx & -idx) res += tree[idx];
return res;
}
};
void print128(i128 x) {
if (x == 0) {
cout << 0;
return;
}
string s;
if (x < 0) {
cout << '-';
x = -x;
}
while (x) {
s.push_back('0' + (x % 10));
x /= 10;
}
reverse(s.begin(), s.end());
cout << s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> A >> B;
for (int i = 1; i <= n; ++i)
for (int j = 0; j < 6; ++j)
leave_time[i][j] = m + 1;
vector<pair<int, int>> students(m);
for (int i = 1; i <= m; ++i) {
int r; char c;
cin >> r >> c;
int col;
switch (c) {
case 'A': col = 0; break;
case 'B': col = 1; break;
case 'C': col = 2; break;
case 'D': col = 3; break;
case 'E': col = 4; break;
case 'F': col = 5; break;
}
students[i-1] = {r, col};
leave_time[r][col] = i;
}
BIT bit(n);
i128 total_aisle = 2LL * n;
for (int i = 1; i <= n; ++i) bit.add(i, 2);
vector<ll> front_cost(m), back_cost(m);
i128 sum_back = 0;
for (int i = 1; i <= m; ++i) {
int r = students[i-1].first;
int col = students[i-1].second;
int same = 0;
if (col <= 2) {
if (col == 0) {
if (leave_time[r][1] > i-1) ++same;
if (leave_time[r][2] > i-1) ++same;
} else if (col == 1) {
if (leave_time[r][2] > i-1) ++same;
}
} else {
if (col == 5) {
if (leave_time[r][4] > i-1) ++same;
if (leave_time[r][3] > i-1) ++same;
} else if (col == 4) {
if (leave_time[r][3] > i-1) ++same;
}
}
int opp = (col <= 2) ? 3 : 2;
int opp_cnt = (leave_time[r][opp] > i-1) ? 1 : 0;
int front_aisle = bit.sum(r-1);
int back_aisle = total_aisle - bit.sum(r);
ll cf = 1LL * A * (same + opp_cnt + front_aisle);
ll cb = 1LL * A * (same + opp_cnt + back_aisle);
front_cost[i-1] = cf;
back_cost[i-1] = cb;
sum_back += cb;
if (col == 2 || col == 3) {
bit.add(r, -1);
total_aisle--;
}
}
vector<ll> d(m);
for (int i = 0; i < m; ++i) d[i] = front_cost[i] - back_cost[i];
sort(d.begin(), d.end());
vector<i128> pref(m+1, 0);
for (int i = 1; i <= m; ++i) pref[i] = pref[i-1] + d[i-1];
i128 ans = sum_back + pref[0] + (i128)B * (0 + (i128)m * (m-1) / 2);
for (int L = 1; L <= m; ++L) {
i128 cur = sum_back + pref[L] + (i128)B * ( (i128)L*(L-1)/2 + (i128)(m-L)*(m-L-1)/2 );
if (cur < ans) ans = cur;
}
print128(ans);
cout << '\n';
return 0;
}复杂度
- 预处理:。
- 树状数组维护过道人数:。
- 排序:。
- 总时间复杂度:,可通过 。