hdu 6047

Problem Description
Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one day Klay comes up with a problem and ask him about it.

Given two integer sequences {ai} and {bi} with the same length n, you are to find the next n numbers of {ai}: an+1…a2n. Just like always, there are some restrictions on an+1…a2n: for each number ai, you must choose a number bk from {bi}, and it must satisfy ai≤max{aj-j│bk≤j

题解:

经典的滑动窗口。看了好久题目才理解什么意思。
学习了优先队列自定义排序的方法,cin一直超时,scanf水过。
优先队列维护一下即可。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 250000+100;
const int MOD = 1e9+7;
int  n;
int a[maxn],b[maxn];

struct node
{
    int id;
    int data;
};

struct cmp{
    bool operator ()(const node &a,const node &b){
          return a.data<b.data;
    }
};

priority_queue<node,vector<node>,cmp> pq;

int main()
{
    while(~scanf("%d",&n))
    {

       for(int i=1;i<=n;i++)
          scanf("%d",a+i);
       for(int i=1;i<=n;i++)
          scanf("%d",b+i);

       sort(b+1,b+1+n);

       LL sum = 0;
       while(!pq.empty()) pq.pop();
       node tmp;

       for(int i=1;i<=n;i++)
       {
            tmp.data=a[i]-i;
            tmp.id=i;
            pq.push(tmp);
       }

       for(int i=1;i<=n;i++)
       {
           while(pq.top().id<b[i]) pq.pop();
           node now = pq.top();
           sum += now.data;
           sum %= MOD;
           now.data = now.data-(i+n);
           now.id = i+n;
           pq.push(now);
       }

       cout<<sum<<endl;

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值