http://acm.hdu.edu.cn/showproblem.php?pid=6047
题意,给定一个数组 {an} 和一个数组 {bn} ,按照一定规则生成 {a2n} ,规则是: ai≤max{aj−j|bk≤j<i} ,每一个 bk 只能选择一次。
例如样例:
4
8 11 8 5
3 1 4 2
选择
a5
的时候,前四个为7 9 5 1,选择
b2
,
a5=9
选择
a6
的时候,前五个为7 9 5 1 4,选择
b4
,
a6=9
选择
a7
的时候,前五个为7 9 5 1 4 3,选择
b1
,
a7=5
选择
a8
的时候,前五个为7 9 5 1 4 3 -2,选择
b3
,
a8=4
则 ∑2ni=n+1ai 最大为27。
考虑用一个优先队列维护 ai−i 以及其下标,然后按照 {bn} 升序排序,对于队列中下标大于 bi 的选择出队,然后选择队首作为 ai+n 的值。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
int a[300000];
int b[300000];
struct node{
int a,id;
};
struct cmp{
bool operator ()(const node &a,const node &b){
return a.a<b.a;
}
};
priority_queue< node ,vector<node > ,cmp> Q;
int main(){
int n;
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);
for(int i=1;i<=n;i++)
a[i]-=i;
int tmx = a[n];
while(!Q.empty()) Q.pop();
sort(b+1,b+1+n);
ll ans = 0;
for(int i=1;i<=n;i++){
Q.push(node{a[i],i});
}
int t=0;
for(int i=n+1;i<=n+n;i++){
while(Q.top().id<b[i-n]) Q.pop();
node now = Q.top();
ans += now.a;
ans %= MOD;
Q.push(node{now.a-i,i});
}
cout<<ans<<endl;
}
return 0;
}