题目链接
思路:
1)2只蚂蚁碰面后视为穿过即可
2)n只蚂蚁每走了2*L路程后回到原来位置,那么先算出要先走多少个周期,再模拟最后一步即可
3)ql存往左走的蚂蚁的位置,qr存往右走的蚂蚁的位置
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, L = 1e9 + 1;
int bb[N], n, a, b, aa[N];
ll res, ans;
queue<ll> ql, qr;
void Pop() {//弹出操作,蚂蚁掉下去
if (!a && ql.size()) {
while (ql.size() > 1)ql.pop();
ans = max(ans, ql.front());
ql.pop();
}
if (!b && qr.size()) {
while (qr.size() > 1)qr.pop();
ans = max(ans, L - qr.front());
qr.pop();
}
}
int main() {
scanf("%d%d%d", &n, &a, &b);
for (int i = 1; i <= n; i++)scanf("%d", &aa[i]);
for (int i = 1; i <= n; i++)scanf("%d", &bb[i]);
ll x = min(a / n, b / n);// 算出要走多少个周期
res = 2ll * x * L;
a -= x * n, b -= x * n;
for (int i = 1; i <= n; i++)
if (!bb[i])ql.push(aa[i]);
for (int i = n; i >= 1; i--)
if (bb[i])qr.push(aa[i]);
Pop();
while (ql.size() || qr.size()) {
ll x, y;
if (ql.size())x = ql.front();
if (qr.size())y = qr.front();
if (qr.size() && ql.size() && x == L - y) {//如果左右2端的蚂蚁同时到端点
a--, b--;
qr.pop(), ql.pop();
qr.push(-x), ql.push(2 * L - y);//其他的蚂蚁位置都不动,存相对位置,并不是实际位置
} else if (qr.size() && (x > L - y || !ql.size())) {//往右边走的蚂蚁先到端点
qr.pop();
ql.push(2 * L - y);
b--;
} else if (ql.size()) {// 往左边走的蚂蚁先到端点
ql.pop();
qr.push(-x);
a--;
}
Pop();
}
cout << ans + res << endl;
return 0;
}