题意:题意是给了一个a数组,长度是n,然后要求从a[n+1]~a[2*n]的和
它的计算方法是这样的;
从a[n+1]开始,a[j]的值等于你在b数组中选一个数字k,那么a[j]的值就是a[j]-j(j<k<i)的最大值
思路:这个题目一开始看到就想到了贪心来做,把b数组从小到大来排序进行计算即可。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAXN 300010
#define INF 10000000
#define MOD 1000000007
#define LL long long
#define pi acos(-1.0)
using namespace std;
LL arr[MAXN * 2];
LL brr[MAXN];
struct Node {
LL num;
int id;
bool operator<(const Node &t) const {
if (num == t.num)
return id > t.id;
return num < t.num;
}
};
int main() {
int n;
while (~scanf("%d", &n)) {
priority_queue<Node> que;
for (int i = 1; i <= n; ++i) {
scanf("%lld", &arr[i]);
que.push(Node{arr[i] - i, i});
}
for (int i = 1; i <= n; ++i)
scanf("%lld", &brr[i]);
sort(brr + 1, brr + n + 1);
LL ans = 0;
int cnt = 1;
for (int i = 1; i <= n; ++i) {
while (!que.empty() && que.top().id < brr[i]) {
que.pop();
}
//cout << que.top().num << endl;
ans += que.top().num;
ans %= MOD;
que.push(Node{que.top().num - i - n, i + n});
}
ans %= MOD;
printf("%lld\n", ans);
}
return 0;
}