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. 12.24笔记(关于//UIDynamic演练//多对象的附加行为//UIDynamic简单演练//UIDynamic//(CoreText框架)NSAttributedString)

          12.24笔记1.UIDynamic注意点:演示代码:上面中设置视图旋转的时候,需要注意设置M_PI_4时,视图两边保持平衡状态,达不到仿真效果.需要偏移下角度.2.吸附行为3.推动行为初 ...

  2. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...

  3. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  4. 使用EL表达式需要注意的

    1.表达式与Java无关,表达式都是以${开始,以}结束. 2.EL访问java变量不能直接访问,因为得不到数据,要访问数据,需要将其放到作用域中.而使用<%=java变量%>可以访问ja ...

  5. Java多线程学习(四)等待/通知(wait/notify)机制

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  6. Spark实现销量统计

    package com.mengyao.examples.spark.core; import java.io.Serializable; import org.apache.hadoop.io.Nu ...

  7. Ubuntu安装pip

    首先打开终端 在终端输入:sudo apt-get install python-pip python-dev build-essential [+] 如果需要在Python3下安装pip,那么在py ...

  8. linux pthread【转】

    转自:http://www.cnblogs.com/alanhu/articles/4748943.html Posix线程编程指南(1) 内容:  一. 线程创建  二.线程取消 关于作者  线程创 ...

  9. python爬虫模块之HTML解析模块

    这个就比较简单了没有什么好强调的,如果返回的json 就是直接按照键值取,如果是网页就是用lxml模块的html进行xpath解析. from lxml import html import json ...

  10. An In-Depth Look at the HBase Architecture

    https://www.mapr.com/blog/in-depth-look-hbase-architecture An In-Depth Look at the HBase Architectur ...