天天看點

[HDU 6047]Maximum Sequence

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}: a[n+1]…a[2n]. Just like always, there are some restrictions on a[n+1]…a[2n] : for each number a[i], you must choose a number bk from {bi}, and it must satisfy a[i]≤max{a[j] - j│b[k] ≤ 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{ ∑(i = n+1 -> 2n) a[i] } modulo 10^9+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{ ∑(i = n+1 -> 2n) a[i] } modulo 10^9+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;

題目大意:

有2個數組,一個是a,一個是b,長度都為n。現在要把a數組的長度變成2n,有一個往a數組中加元素的規則,對于每一個加入i 位置的元素x,必須滿足x<=max{a[j] - j│b[k] ≤ j < i},求問增加的所有元素之和最大值是多少(答案對10^9+7取模)

題解:我們可以很容易知道,越小的b[k]要越先用,因為這樣目前得到的a[i]值才能盡量大。這個貪心是很顯(蛋)然(疼)的。本來要用個單調隊列,結果被我用線段樹水過去了233

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<cmath>
#include<map>
#define ll long long
#define pa pair<long long , int >
#define LiangJiaJun main
#define MOD 1000000007
using namespace std;
ll tr[],res;
int n,up=;
int b[];
ll query(int l,int r){
   ll ans=-;
   for(l=l+up-,r=r+up+;r-l>;l>>=,r>>=){
       if(~l&)ans=max(ans,tr[l+]);
       if(r&)ans=max(ans,tr[r-]);
   }
   return ans;
}
void change(int pos,ll x){
     pos += up;
     tr[pos]=x;
     pos>>=;
     while(pos>=){
        tr[pos]=max(tr[pos<<],tr[pos<<|]);
        pos>>=;
     }
}
int gn;
int w33ha(){
    gn = (n<<);
    res = ;
    while(up<gn+)up<<=;
    for(int i=;i<=n;i++){
        scanf("%I64d",&tr[i+up]);
        tr[i+up]-=i;
    }
    for(int i=n+;i<=n+n;i++)tr[i+up]=;
    for(int i=up;i>=;i--)tr[i]=max(tr[i<<],tr[i<<|]);
    for(int i=;i<=n;i++)scanf("%d",&b[i]);
    sort(b+,b+n+);
    for(int k=;k<=n;k++){
        int r = n + k ;
        ll cc = query(b[k],r-);
        res = (res + cc)%MOD;
        change(r,cc-r);
    }
    cout<<res<<endl;
}

int LiangJiaJun (){
    while(scanf("%d",&n)!=EOF){
        memset(tr,,sizeof(tr));
        w33ha();
    }
    return ;
}