(2017多校训练第二场)HDU - 6047 Maximum Sequence 贪心 + 单调队列

比赛的时候是用线段树写的,赛后看了题解发现还有单调队列这种操作。

官方题解:


想学习单调队列可以看下这篇博客:点击打开链接

代码如下:

#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
const int MAX_N = 250005;
const int MOD = 1e9 + 7;
struct Node
{
    int pos;
    int minval;
};
deque<Node> que;

int main()
{
    //freopen("test.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin.sync_with_stdio(false);
    int n;
    int a[MAX_N];
    int b[MAX_N];
    while (cin >> n)
    {
        que.clear();
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            a[i] -= i;
            while (!que.empty() && a[i] > que.back().minval)
                que.pop_back();
            que.push_back({i, a[i]});
        }
        for (int i = 1; i <= n; i++)
            cin >> b[i];
        sort(b + 1, b + n + 1);
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            while (!que.empty() && b[i] > que.front().pos)
                que.pop_front();
            int tmp = que.front().minval;
            ans = (ans + tmp) % MOD;
            tmp -= i + n;
            while (!que.empty() && tmp > que.back().minval)
                que.pop_back();
            que.push_back({i + n, tmp});
        }
        cout << ans << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值