-----hdu 6047-Maximum Sequence


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
4 8 11 8 5 3 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{i}和一个长度为n的序列b{i},要求构建一个新的序列an+1-a2n,并且使得sum=(an+1)+(an+2)+....+(a2n-1)+(a2n)的和最大; 用于构建新的序列的ai是有一定限制条件的:从b序列里面选一个数bk, ai≤max{aj-j│bk≤j<i};

解题思路:

要想使得构建出来的新的序列的所有项之和最大的话 ai=max{aj-j│bk≤j<i},因为只有每一个ai都取最大值时,才会使得所有项之和最大; ai 的选择范围是从bk到a序列的结束,所以只有当bk越小的时候,ai能选择的范围才越大,这样更有可能取到最大的值,先给b序列从小到大排序,v[i]存a[j]-j的值;然后更新v[i]数组,从i=n开始,v[i]=max(v[i],v[i+1]); 这样更新的目的在于,把一个数前面小于它的数,全部更新为本身,也就是更新为一个大的数; 举个例子:比如一开始的时候v[i]数组为:6 7 9 4 5 4 1 ,经过更新之后每个数前面比它小的数就会变成它本身,数组变成:9 9 9 5 5 4 1;

当我们从bk开始到a序列的结束,找一个最大的数的时候,是从前往后找的,也就是说,如果第一个数就正好是bk到a序列的结束中) 最大的数时,那很容易,但是如果不是的话就还要往后找,所以就干脆直接把大数前面小于它的数全都更新为大数,找的时候会大大减少时间复杂度; 更新了a[i]之后,同时更新a[j]-j;每次a[i]的取值就应该是a[i]=max( a[b[i]],maxx),同时maxx=max(maxx,a[b[i]-i-n)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int mod=1e9+7;
long long a[250100],b[250100],v[250100];
int n;

int main()
{
    while(~scanf("%d",&n))
    {
        memset(v,0,sizeof(v));
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            v[i]=a[i]-i;
        }
        for(int i=1;i<=n;i++)
            scanf("%lld",&b[i]);
        sort(b+1,b+n+1);
        for(int i=n;i>=1;i--)
            v[i]=max(v[i],v[i+1]);
        long long ans=0,maxx=0;
        for(int i=1;i<=n;i++)
        {
            v[b[i]]=max(maxx, v[b[i]]);
            ans=(ans+v[b[i]])%mod;
            maxx=max(maxx,v[b[i]]-i-n);
        }
        printf("%lld\n",ans);
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值