Maximum Sequence

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…a2nan+1…a2n. Just like always, there are some restrictions on an+1…a2nan+1…a2n: for each number aiai, you must choose a number bkbk from {bi}, and it must satisfy aiai≤max{ajaj-j│bkbk≤j<i}, and any bkbk can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{∑2nn+1ai∑n+12nai} modulo 109109+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∑n+12nai} modulo 109109+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;

这题的意思是给出一串a1,a2,......an,让你推出an+1+an+2+.......+a2*n的最大值

ak=ai-i(b[i]<=i<k)(n+1<=k<=2*n)

这题的解题思路是实时刷新模拟即可

#include <cstdio>
using namespace std;
int a[250005*2],b[250005*2];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            a[i]=a[i]-i;
        }
        int t;
        for(int i=1;i<=n;i++){
            scanf("%d",&t);
            b[t]++;
        }
        t=n;
        int v=a[t];
        for(int i=n-1;i>=1;i--){
            if(v>a[i]){
                b[t]+=b[i];
                b[i]=0;
            }
            else{
                t=i;
                v=a[i];
            }
        }
        int j=1;
        long long ans=0;
        for(int i=n+1;i<=2*n;i++){
            while(b[j]>=0){
                if(b[j]==0)
                    j++;
                else{
                    b[j]--;
                    break;
                }
            }
            ans+=a[j];
            a[i]=a[j]-i;
            t=i;
            v=a[t];
            for(int k=i-1;k>=j;k--){
                if(v>a[k]){
                    b[t]+=b[k];
                    b[k]=0;
                }
                else
                    break;
            }
        }
        printf("%lld\n",ans%1000000007);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/aaakirito/article/details/81070380