hdu 6047 Maximum Sequence

来源:互联网 发布:origin软件吧 编辑:程序博客网 时间:2024/06/01 21:26

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;
 

题意: 给你两个长度为n的序列,A,B; 你需要在An+1~A2n上填上数(0<=Bi<=n), An+1~A2n数的限制条件:填An+1时,从B1~Bn中任选一位数Bk,是使得对任意j,Bk<=j<n+1,An+1<=max(Aj-j),以此类推,很容易能够想到把B数组升序排列,从第一位开始操作即可,直觉(证明略!!),这样用线段树查找最大值或者用个数组维护最大值都可以,但是用线段树要注意很多细节,一时不一定能想到,下面是线段树代码(注意线段树的空间,小了会wa):

#include <iostream>#include <algorithm>using namespace std;typedef long long lll;const long long mod=1e9+7;const int maxnn=500010;const int minn=-0x3f3f3f3f;int m[maxnn];int m1[maxnn];lll a,b;int MAX;int n;struct node{    int l,r,maxn;lll sum;}tree[4*maxnn+10];void build(lll k,lll ll,lll rr) {if (k>8*n)return;    tree[k].l=ll,tree[k].r=rr;    if(tree[k].l==tree[k].r)    {    if (ll>n)    {       tree[k].sum=0;   tree[k].maxn=0;}         else         {           tree[k].sum=m[ll];           tree[k].maxn=m[ll]-ll;          }        return;    }    lll m=(ll+rr)/2;    build(k*2,ll,m);    build(k*2+1,m+1,rr);    tree[k].maxn=max(tree[k*2].maxn,tree[k*2+1].maxn);    tree[k].sum=(tree[k*2].sum+tree[k*2+1].sum)%mod;}void ask_interval(lll k) {    if(tree[k].l>=a&&tree[k].r<=b)     {        MAX=max(MAX,tree[k].maxn);        return;    }    lll mid=(tree[k].l+tree[k].r)/2;    if(a<=mid)  ask_interval(k*2);    if(b>mid)  ask_interval(k*2+1);}void update(lll x,lll v,lll k){ if (tree[k].l==tree[k].r) {  tree[k].sum=x; tree[k].maxn=x-v; return ; } lll mid=(tree[k].l+tree[k].r)/2; if (v<=mid) update(x,v,k*2); else update(x,v,k*2+1); tree[k].sum=(tree[k*2].sum+tree[k*2+1].sum)%mod; tree[k].maxn=max(tree[k*2].maxn,tree[k*2+1].maxn);}int main(){while (scanf("%d",&n)!=EOF){for (int i=1;i<=n;i++)scanf("%d",&m[i]);for (int i=1;i<=n;i++)scanf("%d",&m1[i]);sort(m1+1,m1+1+n);build(1,1,2*n);for (int i=1;i<=n;i++){  MAX=minn;  a=m1[i];  b=i+n-1;  ask_interval(1);//在An+1~A2n上加数;  m[i+n]=MAX;  update(m[i+n],i+n,1); //更新最大值与和     }     printf("%lld\n",tree[3].sum); //tree[3].sum,存的是An+1到A2n的和;}return 0;}