hdu 6047Maximum Sequence(贪心)

来源:互联网 发布:windows日志看不懂 编辑:程序博客网 时间:2024/06/02 03:52

Maximum Sequence

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


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;


题意:
在确定ai(n<i<2*n)时在B中选一个数作为左闭区间,i作为右开区间  j属于[bk,i)中ai为aj-j的中最大值

解析:
首先让a序列的数都减去他的坐标,之后的操作都对这些数来操作
 很容易想到是尽可能让大的数排在前面,因为(n<i<2n)中后面的数在计算前面区间的最大值时可以包括前面的数(n<j<i)

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define MAXN 250010#define INF 999999999#define MOD 1000000007using namespace std;typedef struct node{    int left;    int mmax;   //在[left,n]中a中的最大值}node;int n;int stu[MAXN];node B[MAXN];int cmp(node x,node y){    if(x.mmax==y.mmax)        return x.left>y.left;    return x.mmax>y.mmax;}int cmp1(node x,node y){    return x.left>y.left;}int main(){    int sum;    while(scanf("%d",&n)!=EOF)    {        sum=0;        memset(stu,0,sizeof(stu));        for(int i=1;i<=n;i++)        {            scanf("%d",&stu[i]);            stu[i]-=i;        }        for(int i=1;i<=n;i++)        {            scanf("%d",&B[i].left);        }        sort(B+1,B+n+1,cmp1);        int right=n;        int mm=0;        for(int i=1;i<=n;i++)   //预处理,将每一个B中的数都算出他对应[left,n]中的最大值        {            for(int j=right;j>=B[i].left;j--)  //这里只是在找看[left,right]中有没有更大的值大于之前的最大值            {                if(stu[j]>mm) mm=stu[j];            }            right=B[i].left-1;               B[i].mmax=mm;        }        sort(B+1,B+n+1,cmp);   //对B根据其对应区间的最大值进行排序        sum=(sum+B[1].mmax)%MOD;   //这里先将a[n+1]的值算出来,这里注意,其实后面(n,2n)中最大值只能是a[n+1]=最大值(已经减去下标)        B[1].mmax=B[1].mmax-n-1;   //所以只要将a[n+1]-n-1加入到B中就行了        B[1].left=n+1;        sort(B+1,B+n+1,cmp);  //再对B进行相同的排序        int w=1,flag;        flag=0;        for(int i=n+2;i<=2*n;i++)        {            sum=(sum+B[w].mmax)%MOD;            if(B[w].left>n)flag=1;  //如果现在的最大值是a[n+1]-n-1的话那么之后a[i..2n]就都等于a[n+1]-n-1了            if(flag==0)            w++;        }        printf("%d\n",sum%MOD);    }}