您的位置:首页 > 产品设计 > UI/UE

HDU6047 Maximum Sequence(贪心,暑期训练1003)

2017-07-27 19:19 260 查看


Maximum Sequence


Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 159    Accepted Submission(s): 79


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<
4000
span>,
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;

 


题意是给了一个a数组,长度是n,然后要求从a[n+1]~a[2*n]的和

它的计算方法是这样的; 

从a[n+1]开始,a[j]的值等于你在b数组中选一个数字k,那么a[j]的值就是a[j]-j(j<k<i)的最大值

以第一个样例来说明
编号1234
a81185
b3142
我们第一次从b数组选出来的数字是2,那么 

从a[2]开始算起 

a[2]-2=9 

a[3]-3=8 

a[4]-4=1 

a[5]的值就是他们中间最大的,也就是a[5]=9

到了计算a[6]的情况,这次我们在b数组里面选择1 

a[1]-1=7 

a[2]-2=9 

a[3]-3=8 

a[4]-4=1 

a[5]-5=4

所以a[6]的值是9,以此类推 

a[7]=5,a[8]=4

他们的和=9+9+5+4=27

我们的做法是直接在a[i]中存储a[i]-i的值

然后后开一个优先队列,让值比较大的先出队,把b数组中的每一个值都放到优先队列中,每次取队首,那么队首就是我们所要求的那个最大值

#include "stdio.h"
#include "string.h"
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX=250020;
const int mod=1000000007;
struct node
{
int v;
int i;
friend bool operator<(node n1,node n2)
{
return n1.v<n2.v;
}
}a[2*MAX],p;
int b[MAX];

int main()
{
int n;
while(~scanf("%d",&n))
{
priority_queue<node> q;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].v);
a[i].v=a[i].v-i;
a[i].i=i;
q.push(a[i]);
}
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
sort(b+1,b+1+n);
int sum=0;
for(int i=1;i<=n;i++)
{
p=q.top();
while(p.i<b[i])
{
q.pop();
p=q.top();
}
a[i+n].v=p.v;
a[i+n].i=i+n;
sum+=a[i+n].v;
sum%=mod;
a[i+n].v=a[i+n].v-(i+n);
q.push(a[i+n]);
}
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: