Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1797 Accepted Submission(s): 842
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.
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.
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
分析可知 ,a_n+1-------a_2*n必定是一个非严格递减序列,由此可知对a_n+1-a_2*n有贡献的只可能是a_1-a_n+1(因为a_n+1-a_2*n是一个非严格递减序列)
所以首先预处理出来从每个A[i]起的贡献Max[i],然后加一起就可以了。(注意Max数组处理时没有管道a_n+1,所以要和a_n+1进行比较一下大小)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=250008; const int mod=1e9+7; int a[N],b[N],Max[N]; int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;++i) scanf("%d",&a[i]),a[i]-=i; for(int i=1;i<=n;++i) scanf("%d",&b[i]); Max[n]=a[n]; for(int i=n-1;i>=1;--i) Max[i]=max(Max[i+1],a[i]); sort(b+1,b+n+1); int ans1=Max[b[1]]-n-1,ans=Max[b[1]]; for(int i=2;i<=n;++i) ans=(ans+max(Max[b[i]],ans1))%mod; printf("%d ",ans); } }