HDU 6047 17多校 Maximum Sequence(优先队列)

时间:2021-05-27 17:32:03
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<i}, and any bk can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{∑2nn+1ai} modulo 109+7 .

Now Steph finds it too hard to solve the problem, please help him.

 
Input
The input contains no more than 20 test cases.
For each test case, the first line consists of one integer n. The next line consists of n integers representing {ai}. And the third line consists of n integers representing {bi}.
1≤n≤250000, n≤a_i≤1500000, 1≤b_i≤n.
 
Output
For each test case, print the answer on one line: max{∑2nn+1ai} modulo 109+7。
 
Sample Input
4
8 11 8 5
3 1 4 2
 
Sample Output
27
Hint

For the first sample: 1. Choose 2 from {bi}, then a_2…a_4 are available for a_5, and you can let a_5=a_2-2=9;

2. Choose 1 from {bi}, then a_1…a_5 are available for a_6, and you can let a_6=a_2-2=9;
 
 
题意:给一个长度为n的a数组和b数组,然后给a在后面再填充n个数字,要求每次填充都是在b中选择一个数,设挑选的数为bx,设j为我目前要填充的数的序号,那么你填充的数就是a中序号从(bx)~(j-1)中最大的那个a[i]-i,最终使填充数的和最大。
题解:
1.要使最终结果最大的话,每次填充的数都尽可能的大,因为最先填的数减的数最小,所以对b来说先从小到大排序,能保证先填充的数最大。
2.为了维持每次挑选的是最大的数,可以用一个优先队列来维护,按照它的a[i]-i的值来进行排序。
3.最精华的地方,因为b已经排过序了,然后每次加的是最大的数,所以我只要保证队列的top的id要大于等于目前的b[i]即可。
 
每次用优先队列来维护最大最小值的题目我用sort来做超时一发的时候都觉得自己简直是太傻逼了///
为自己续一秒。。。
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
using namespace std; #define MOD 1000000000+7 struct node
{
long long num,ip,dis;
}; long long b[]; struct cmp
{
bool operator()(node q,node p)
{
return q.dis<p.dis;
}
}; int main()
{
long long n;
while(~scanf("%lld",&n))
{
priority_queue<node,vector<node>,cmp>Q;
node a;
for(int i=;i<=n;i++)
{
scanf("%lld",&a.num);
a.ip=i;
a.dis=a.num-a.ip;
Q.push(a);
}
for(int i=;i<=n;i++)
scanf("%lld",&b[i]);
sort(b+,b++n);
long long res=;
for(int i=;i<=n;i++)
{
while(Q.top().ip<b[i])
Q.pop();
node tmp=Q.top();
res+=tmp.dis;
res%=MOD;
tmp.ip=n+i;
tmp.num=tmp.dis;
tmp.dis-=tmp.ip;
Q.push(tmp);
}
printf("%lld\n",res);
}
return ;
}