HDOJ 6047 Maximum Sequence(单调队列)

the description of the problem is really hard to understand . It’s means we should find a start point k in array B, than go back to array A, find the maximum of Ajj in range (K,i) .we can us priority queue to slove this.

#include <bits/stdc++.h>
#define INF 0x7f7f7f7f
#define ll long long
using namespace std;
const int maxn = 250005;
const int mod  = 1e9+7;
int a[maxn<<1],b[maxn];
struct node
{
    int id,x;
    bool operator < (const node &b) const
    {
        if(x != b.x) return x < b.x;
        else return id > b.id;
    }
};
int n;
int bucket[maxn];
void bucketsort(int vv){
    memset(bucket,0,sizeof(bucket));
    for(int i=0;i<n;i++){
        bucket[b[i]]++;
    }
    for(int i=0,j=0;i<=vv;i++){
        while(bucket[i]){
            b[j++]=i;
            bucket[i]--;
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        node temp;
        priority_queue<node> q;
        for(int i = 1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            temp.x = a[i]-i;
            temp.id = i;
            q.push(temp);
        }
        int vv = 0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&b[i]);
            //vv = max(vv,b[i]);
        }
        sort(b,b+n);
        //bucketsort(vv);
//        for(int i=0;i<n;i++)
//        {
//            cout<<b[i]<<" ";
//        }
//        cout<<endl;
        int ans = 0;
        for(int i= n+1;i<=2*n;i++)
        {
            temp = q.top();
            while(b[i-n-1] > temp.id)
            {
                 q.pop();
                temp = q.top();

            }
            a[i] = temp.x;
            temp.id = i;
            temp.x = a[i]-i;
            q.push(temp);
            //q.pop();
        }
//        for(int i=1;i<=2*n;i++)
//        {
//            cout<<a[i]<<" ";
//        }
//        cout<<endl;
        for(int i=n+1;i<=n+n;i++)
        {
            ans = (ans + a[i]) %mod;
        }

        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值