题意:求a[n+1]~a[2*n]的和,这些值得计算方法是,每次在b数组取值,a[i] = MAX(a[k]-k) (bj ≤ k ≤ i-1)
思路:我们用优先队列存下a[i]-i 和他们的位置,每次取最大的值的位置,没有了就向前取,这样就保证每次取到的是最大的,放在a[i]最优。
#include<bits/stdc++.h>
using namespace std;
const int MAX=250000+10;
const int MOD = 1e9+7;
int a[MAX],b[MAX],n;
map<int,int> ma;
map<int,int>::iterator iter;
struct data
{
int x,index;
bool friend operator<(const struct data &a,const struct data &b)
{
if(a.x < b.x)
return 1;
else if(a.x == b.x)
{
if(a.index < b.index)
return 1;
else
return 0;
}
else
return 0;
}
};
int main()
{
while(scanf("%d",&n)!=EOF)
{
long long ans = 0;
ma.clear();
ma[0] = 1;
ma[n+1] = 1;
priority_queue<struct data>p;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),a[i]-=i,p.push((data){a[i],i});
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
ma[k]++;
}
for(int i=n+1;i<=2*n;i++)
{
loop:;
data now = p.top();
int wei = now.index;
if(ma[wei] >= 1)
{
ma[wei]--;
if(ma[wei] == 0)
ma.erase(wei);
ans += now.x;ans %= MOD;
p.push((data){now.x-i,i});
}
else
{
ma[wei] = 1;
iter = ma.find(wei);
iter--;
ma.erase(wei);
if( iter->first != 0)
{
wei = iter->first;
ma[wei]--;
if(ma[wei] == 0)
ma.erase(wei);
ans += now.x;ans %= MOD;
p.push((data){now.x-i,i});
}
else
{
p.pop();
goto loop;
}
}
}
printf("%lld\n",ans);
}
return 0;
}