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…a2nan+1…a2n. Just like always, there are some restrictions on an+1…a2nan+1…a2n: for each number aiai, you must choose a number bkbk from {bi}, and it must satisfy aiai≤max{ajaj-j│bkbk≤j<i}, and any bkbk can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{∑2nn+1ai∑n+12nai} modulo 109109+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∑n+12nai} modulo 109109+7。
 
Sample

Sample Input

Sample Output
 
题意 :
  这个题的题意是真不好理解啊,大体就是已知两个数列a, b,已经给出了a, b的前n项,求数列a的n+1到2*n项,使得这些项的和最大

  数列要满足aj <= max{ai - i},其中bk <= j < i,bk是数列b中的一项,且每个bk最多仅能取一次

  看到这里肯定还看不明白。

  具体说一下第一个样例:

    a数组是8 11 8 5   b数组是3 1 4 2  

    现在要扩充a数组,然后数组的扩充方法是,a数组中每个数等于本身减去他的下标,然后从b数组中选一个数,然后a数组的这个数的位置开始到最后选一个最大的(只能解释到这样了)。

    实现:a数组相当于 (8-1)(11-2)(8-3)(5-4)

    从b数组中选择1,代表从a[1]到a[4]中选择一个最大的,选择9。然后将9添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)

    然后第二次从b数组中选择2代表从a[2]到a[5]中选择一个最大的,选择9。然后将9添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)(9-6)

    然后第三次从b数组中选择3代表从a[3]到a[6]中选择一个最大的,选择5。然后将5添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)(9-6)(5-7)

    然后第四次从b数组中选择4代表从a[4]到a[7]中选择一个最大的,选择4。然后将9添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)(9-6)(5-7)(4-8)

  然后更新完成了。最后数列a的n+1到2*n项的值为9+9+5+4=27

思路:

要想使数列a的n+1到2*a项最大,又每项都要减去i

所以应该尽量使bk最小,从最前面开始(可以理解为贪心)

那么肯定要先把最大的放进来

将b排序,计算ai - i的值

开一个max数组记录从i 到最后一项的最大ai-i的值(可以从后往前找)

每次加入点的时候,从后往前比较,如果小于这个数,就更新为这个数。

用一个sum记录总和。

记得每次加的结果要%mod。

注意数组要开为数据量的两倍,因为a数组要扩展!

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
int mod=1e9+;
using namespace std;
int a[],b[],maxx[];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(maxx,,sizeof(maxx));
for(int i=; i<=n; i++)
{
scanf("%d",a+i);
a[i]-=i;
}
for(int i=; i<=n; i++)
{
scanf("%d",b+i);
}
sort(b+,b+n+);
maxx[n]=a[n];
for(int i=n; i>=; i--)//更新max数组
{
maxx[i-]=max(a[i-],maxx[i]);
}
int flag=n+;
int sum=;
for(int i=; i<=n; i++)
{
sum+=maxx[b[i]];//选择最大的数
a[flag]=maxx[b[i]]-(flag);
maxx[flag]=a[flag];
for(int j=flag-; j>=; j--)//加入新的点之后,要更新对应的max
{
if(maxx[j]<maxx[flag])
maxx[j]=maxx[flag];
else break;
}
flag++;
sum=sum%mod;
}
printf("%d\n",sum);
}
}

hdu 6047 Maximum Sequence 贪心的更多相关文章

  1. HDU 6047 Maximum Sequence(贪心+线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  2. HDU 6047 Maximum Sequence (贪心+单调队列)

    题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由 ...

  3. HDU 6047 - Maximum Sequence | 2017 Multi-University Training Contest 2

    /* HDU 6047 - Maximum Sequence [ 单调队列 ] 题意: 起初给出n个元素的数列 A[N], B[N] 对于 A[]的第N+K个元素,从B[N]中找出一个元素B[i],在 ...

  4. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  5. HDU 6047 Maximum Sequence

    Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 2017 Multi-University Training Contest - Team 2&&hdu 6047 Maximum Sequence

    Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 6047 Maximum Sequence(贪心)

    Description Steph is extremely obsessed with "sequence problems" that are usually seen on ...

  8. hdu 6047: Maximum Sequence (2017 多校第二场 1003)【贪心】

    题目链接 可以贪心写,先把b数组按从小到大的顺序排个序,根据b[i]的值来产生a[n+i] 借助一个c数组,c[i]记录,j从i到n,a[j]-j的最大值,再加上一个实时更新的变量ma,记录从n+1到 ...

  9. 【多校训练2】HDU 6047 Maximum Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=6047 [题意] 给定两个长度为n的序列a和b,现在要通过一定的规则找到可行的a_n+1.....a_2n,求su ...

随机推荐

  1. 【NOIP】提高组2005 过河

    [算法]状态压缩型DP [题解] Q=tx+(t-1)y 对于Q≥t(t-1),x,y一定有解. 所以当两石子间距离long>t(t-1)时,令long=t(t-1),重新构造数组即可. [注意 ...

  2. Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]

    题目链接:http://codeforces.com/contest/984 A. Game time limit per test:2 seconds memory limit per test:5 ...

  3. Ribbon自带负载均衡策略比较

    Ribbon自带负载均衡策略比较 策略名 策略声明 策略描述 实现说明 BestAvailableRule public class BestAvailableRule extends ClientC ...

  4. Perl6 Bailador框架(8):自定义400/500

    第一种方法, 直接写在源码中: use Bailador; get '/' => sub { '<h1>hello, Bailador</h1>'; } get '/te ...

  5. pam会话函数详解

    pam会话函数详解 http://www.xuebuyuan.com/2223069.html http://blog.itpub.net/15480802/viewspace-1406088/ ht ...

  6. Mysql 数据库学习笔记03 存储过程

    一.存储过程:如下           通过 out .inout 将结果输出,可以输出多个值. * 调用存储过程: call 存储名称(参数1,参数2,...); 如指定参数不符合要求,返回 Emp ...

  7. 关于移动端audio自动播放问题

    本人小白全栈一枚,给公司写了一个监控中心,要求严重报警的时候需要触发音频播放,于是就有了以下的折腾. 刚开始一切都很顺利,自然而然的写了以下代码. <audio id="myaudio ...

  8. linux命令(23):cp命令

    实例一:复制单个文件到目标目录 cp 1.log /home 说明: 1.在没有带-a参数时,两个文件的时间是不一样的.在带了-a参数时,两个文件的时间是一致的. 2.当目标文件已存在,会询问是否覆盖 ...

  9. Codeforces 873B - Balanced Substring(思维)

    题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...

  10. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...