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.

 
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

分析可知 ,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=;
const int mod=1e9+;
int a[N],b[N],Max[N];
int main(){
   int n;
   while(scanf("%d",&n)!=EOF){
    for(int i=;i<=n;++i) scanf("%d",&a[i]),a[i]-=i;
    for(int i=;i<=n;++i) scanf("%d",&b[i]);
    Max[n]=a[n];
    for(int i=n-;i>=;--i) Max[i]=max(Max[i+],a[i]);
    sort(b+,b+n+);
    int ans1=Max[b[]]-n-,ans=Max[b[]];
    for(int i=;i<=n;++i)  ans=(ans+max(Max[b[i]],ans1))%mod;
    printf("%d\n",ans);
   }
}
 

最新文章

  1. python 3.5 成功安装 scrapy 的步骤
  2. android 7.0 学习笔记(一)
  3. js高级群的一些整理6月
  4. 用友ERP-U8最新破解(再次更新版本,附安装过程中的解决办法)
  5. hdu 5596 GTW likes gt
  6. WCF 编程实验室
  7. IT职业思考 谈谈IT外包公司
  8. 【循序渐进学Python】14.数据库的支持
  9. 装饰模式/decorator模式/结构型模式
  10. JSON认识
  11. ACM题目————Subsequence
  12. PAT 1021
  13. Window 点击“X”关闭之后无法show
  14. HDU 3903 Trigonometric Function
  15. CSS3知识点整理(五)----响应式设计及其他属性
  16. 你好 JSONP !!!!
  17. PERL学习笔记---正则表达式
  18. 解决使用Spring Boot、Multipartfile实现上传提示无法找到文件的问题
  19. spring+springmvc+hibernate 整合
  20. 【scapy】读取pcap

热门文章

  1. 【shell】shell脚本入门
  2. 初入React源码(一)
  3. 《树莓派学习指南(基于Linux)》——本章小结
  4. TEC-004-php文件下载任意文件读取漏洞修复
  5. Linux从入门到精通系列之NFS
  6. c/c++头文件的摘抄
  7. [NOI 2020 Online] 入门组T1 文具采购(洛谷 P6188)题解
  8. Spring IOC的核心机制:实例化与注入
  9. OSG程序设计之Hello World 2.0
  10. 经典卷积神经网络算法(2):AlexNet