2017ACM暑期多校联合训练 - Team 2 1003 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+1…a2n. Just like always, there are some restrictions on an+1…a2n: 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:
- Choose 2 from {bi}, then a_2…a_4 are available for a_5, and you can let a_5=a_2-2=9;
- Choose 1 from {bi}, then a_1…a_5 are available for a_6, and you can let a_6=a_2-2=9;
分析:
每次需要从已有的a数组(a数组保存的就是本来的值减去下标)里面找到一个最大值,然后将这个值加入到a数组后面,接着在重复上面的过程。每次在找到一个值之后,线段树都要更新。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#define lchild left,mid,root<<1
#define rchild mid+1,right,root<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 600000;
const int mod = 1e9+7;
int Max[maxn<<2];
int a[maxn];
int b[maxn];
void push_up(int root)
{
Max[root] = max(Max[root<<1],Max[root<<1|1]);
}
///构建线段树
void build(int left,int right,int root)
{
if(left == right)
{
Max[root] = a[left];
return;
}
int mid = (left+right)>>1;
build(lchild);
build(rchild);
push_up(root);
}
int query(int L,int R,int left,int right,int root)///[L,R]是需要查找的区间
{
if(L<=left && right<=R)
return Max[root];
int mid = (left+right)>>1;
int ans = -inf;
if(L<=mid) ans = max(ans,query(L,R,lchild));
if(R>mid) ans = max(ans,query(L,R,rchild));
return ans;
}
void Insert(int pos,int left,int right,int root)
{
if(left == right)
{
Max[root] = a[pos]-pos;
return;
}
int mid = (left+right)>>1;
if(pos<=mid) Insert(pos,lchild);
else Insert(pos,rchild);
push_up(root);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
a[i] = a[i]-i;///a数组中直接保存为它本身减去下标的值,从而每次从里面取得最大值
}
for(int i = 1; i <= n; i++)
{
scanf("%d",&b[i]);
a[i+n] = -inf;
}
sort(b,b+n);
build(1,2*n,1);///因为在从后面取的时候要从整个数组里面取包括后面的部分
int ans = 0;
int range = n;
for(int i = 1; i <= n; i++)
{
int num = query(b[i],range,1,2*n,1);
range++;
a[i+n] = num;
Insert(n+i,1,2*n,1);
ans = (ans+num)%mod;
}
printf("%d\n",ans);
}
return 0;
}
2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 6 1003 HDU 6098 Inversion (模拟)
题目链接 Problem Description Give an array A, the index starts from 1. Now we want to know Bi=maxi∤jAj , ...
- 2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)
题目链接 Problem Description In mathematics, the function d(n) denotes the number of divisors of positiv ...
- 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)
题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
随机推荐
- windows操作系统下载tomcat,并与eclipse进行整合
进入Tomcat官网之后,在左边我们看到,Tomcat的有6,7,8这三个最流行的版本,我们可以点击进去下载想要的版本. 进入里面之后,可以看见有64位的和32位的,就看自己的电脑是多少位的了,如果电 ...
- 【移动端debug-2】Flexbox在移动端的兼容实践
最近在项目中用到了flexbox,总结一下使用心得. 一.什么是flexbox,干嘛使的? 曾几何时,我们特别希望能像word一样,在排版时有个分散对齐选项(平均分配子元素宽度)这样我就可以任意在父元 ...
- 51nod 1574 排列转换(贪心+鸽巢原理)
题意:有两个长度为n的排列p和s.要求通过交换使得p变成s.交换 pi 和 pj 的代价是|i-j|.要求使用最少的代价让p变成s. 考虑两个数字pi和pj,假如交换他们能使得pi到目标的距离减少,p ...
- shell的uniq命令
uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用. uniq 可检查文本文件中重复出现的行列. 命令语法: uniq [-c/d/D/u/i] [-f Fields ...
- 获取和验证Windows AD域的用户信息
1.获取windows AD域用户信息,首先需要有一个ad域管理员权限的账号,用这个账号连接ad域,获取所有域用户信息 用LdapContext,它继承自DirContext public Objec ...
- Oracle 多表关联并且批量修改
描述:A表有 id,or_id 字段,B表有 id,code 字段 A表有 or_id 与B表的 id 关联,现要将A.or_id 替换成 B.code 数据 UPDATE AS ...
- 【刷题】BZOJ 2260 商店购物
Description Grant是一个个体户老板,他经营的小店因为其丰富的优惠方案深受附近居民的青睐,生意红火.小店的优惠方案十分简单有趣.Grant规定:在一次消费过程中,如果您在本店购买了精制油 ...
- nginx服务器去掉url中的index.php 和 配置path_info
隐藏index.php server { listen 80; server_name yourdomain.com; root /home/yourdomain/www/; index index. ...
- acid(数据库事务正确执行的四个基本要素的缩写)
ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...
- 【DP】【CF9D】 How many trees?
传送门 Description 给你两个正整数\(n,h\),求由\(n\)个点组成的高度大于等于\(h\)的二叉树有多少个 Input 一行两个整数\(n,h\) Output 一个整数代表答案. ...