第十三届四川省赛 J - Ants

8 篇文章 0 订阅
2 篇文章 0 订阅

题目链接
思路:
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;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值