hdu 6047 Maximum Sequence

来源:互联网 发布:手机淘宝直通车在哪里 编辑:程序博客网 时间:2024/06/06 14:22

Maximum Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


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+1a2n. Just like always, there are some restrictions on an+1a2n: 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
48 11 8 53 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;
题意:根据b数组确定a数组的后n位,其实刚开始做的时候我们认为b数组是不可重复的,如果这样的话b数组压根没用,只需要把a数组预处理
a[i]=a[i]-i,然后开始确定a数组的后n位,比如a[n+1]就是找a[1]~a[n]之间的最大值,a[n+2]就是a[2]~a[n+1]之间的最大值,这样的话就用一下线段树
维护区间最大值+单点更新就可以了,但是交了wa,b数组是可以重复的,那么只需要把b数组排个序然后根据b数组确定查找区间的范围就好了,
每次确定一个a[i]就要把a[i]-i;插入到线段树中
ac代码:
#include<iostream>#include<cstdio>#include <algorithm>#define INF 99999999const int inf=-1000000000;const int mod=1e9+7;using namespace std;struct node{    int val;    int l,r;} tree[4*500005];int arr[500005];int b[250005];void build(int root,int l,int r){    int mid;    tree[root].l=l;    tree[root].r=r;    if (l==r) tree[root].val=arr[l];    else    {        mid=(l+r)/2;        build(root*2,l,mid);        build(root*2+1,mid+1,r);        tree[root].val=max(tree[root*2].val,tree[root*2+1].val);//存储左右子树的和    }}void add(int root,int id,int addval)  //单点更新{    int mid;    if (tree[root].l==tree[root].r)    {        tree[root].val=addval;        return;    }    else    {        mid=(tree[root].l+tree[root].r)/2;        if (id<=mid) add(root*2,id,addval);        else add(root*2+1,id,addval);        tree[root].val=max(tree[root*2].val,tree[root*2+1].val);    }}int ask(int root,int l,int r){    int mid;    if (tree[root].l==l&&tree[root].r==r)        return tree[root].val;    else    {        mid=(tree[root].l+tree[root].r)/2;        if (mid>=r)            return ask(root*2,l,r);        else if (mid<l)            return ask(root*2+1,l,r);        else return max(ask(root*2,l,mid),ask(root*2+1,mid+1,r));    }}int main(){    int n;    while(cin>>n)    {        for (int i=1; i<=n; i++)        {            scanf("%lld",&arr[i]);            arr[i]=arr[i]-i;        }        for(int i=n+1;i<=2*n;i++)        arr[i]=inf;        build(1,1,2*n);        int a;        for(int i=0;i<n;i++)        scanf("%d",&b[i]);        sort(b,b+n);        long long sum=0;        int k=0;        for(int i=n+1;i<=2*n;i++)        {            int p=ask(1,b[k++],i-1);            sum=(sum+p)%mod;            add(1,i,p-i);        }        cout<<sum<<endl;    }}