hdu 多校联赛 Maximum Sequence

来源:互联网 发布:淘宝 信用卡 积分 浦发 编辑:程序博客网 时间:2024/05/22 09:04

Maximum Sequence

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


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;
做的时候没有看懂题意 后来补题的时候连百度带请教别人才把样例解释清楚,而且代码也有很多地方不是很理解的地方。

百度上的思路:

题意是给了一个a数组,长度是n,然后要求从a[n+1]~a[2*n]的和

它的计算方法是这样的; 
从a[n+1]开始,a[j]的值等于你在b数组中选一个数字k,那么a[j]的值就是a[j]-j(j<k<i)的最大值

以第一个样例来说明

编号1234a81185b3142

我们第一次从b数组选出来的数字是2,那么 
从a[2]开始算起 
a[2]-2=9 
a[3]-3=5
a[4]-4=1 
a[5]的值就是他们中间最大的,也就是a[5]=9

到了计算a[6]的情况,这次我们在b数组里面选择1 
a[1]-1=7 
a[2]-2=9 
a[3]-3=5 
a[4]-4=1 
a[5]-5=4

所以a[6]的值是9,以此类推 
a[7]=5,a[8]=4

他们的和=9+9+5+4=27

我们的做法是直接在a[i]中存储a[i]-i的值,然后定义数组ma[i],代表以i为起点,到后面区间所能得到最大值

然后后开一个优先队列,让值比较大的先出队,把b数组中的每一个ma[b[i]]值都放到优先队列中,每次取队首,那么队首就是我们所要求的那个最大值,用tmp来存储n+1以后a[i]-i的最大值,每次更新a[i],最后取模就是答案。

ac代码

#include <bits/stdc++.h>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define N 250010#define ll long longusing namespace std;const int mod=1e9+7;int a[N*2],b[N],ma[N];int main(){  int n;  while(scanf("%d",&n)!=EOF)  {      mem(ma,0);mem(a,0);//每次输入都清空数组数组 不是难点却容易粗心漏掉      int x;      for(int i=1; i<=n; i++)      {          scanf("%d",&x);//输入a【i】数据          a[i]=x-i;//用a【i】-【i】来存储数据      }      for(int i=n; i>=1; i--)          ma[i]=max(a[i],ma[i+1]);//计算从i为开始,所能达到的最大值      for(int i=1; i<=n; i++)          scanf("%d",&b[i]);//输入b【i】      priority_queue<int>q;//创建一个优先队列      for(int i=1; i<=n; i++)          q.push(ma[b[i]]);//将处理完的b【i】值对应的ma数组压入队列      int ans=0,tmp=0;      for(int i=n+1; i<=2*n; i++)      {          int s=q.top();//每次循环取栈的第一个值          if(tmp>s)              a[i]=tmp-i;//如果之前的最大值大于这次栈顶的值 则确定这个位置的a【i】值          else          {              a[i]=s-i;//如果小于 就用这次的栈顶的值确定a【i】的值              q.pop();//将这个已被用过的数踢出栈          }          ans=(ans+a[i]+i)%mod;//每一项累和时都取模 防爆数          tmp=max(tmp,a[i]);//每次都找到tmp的最大值      }      printf("%d\n",ans%mod);  }  return 0;}

原创粉丝点击