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;
}