2017 多校赛 第二场

1003.Maximum Sequence

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

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

思路:贪心,用一个数组记下从i到n的a[i]-i的最大值pre[i],可以得到pre[i]=max(pre[i+1],a[i]-i),然后容易得到b[k]从小往大取时,后n项之和最大,所以a[n+1]=pre[b[1]](后n项会呈单调不递增趋势,a[n+1]-n-1即为之后取最大值的比较对象)。

代码:

 1 #include "cstdio"
 2 #include "stdlib.h"
 3 #include "iostream"
 4 #include "algorithm"
 5 #include "string"
 6 #include "cstring"
 7 #include "queue"
 8 #include "cmath"
 9 #include "vector"
10 #include "map"
11 #include "set"
12 #define db double
13 #define ll long long
14 #define inf 0x3f3f3f
15 using namespace std;
16 const int N=3e5+5;
17 const int mod=1e9+7;
18 #define rep(i,x,y) for(int i=x;i<=y;i++)
19 //char s[N],t[N];
20 db  pi=3.14;
21 //int s[N],w[N];
22 int a[N],b[N];
23 int pre[N];
24 int main()
25 {
26     int n;
27     while(scanf("%d",&n)==1){
28         for(int i=1;i<=n;i++){
29             scanf("%d",a+i);
30             a[i]-=i;
31         }
32         memset(pre,0, sizeof(pre));
33         for(int i=n;i>=1;i--) pre[i]=max(a[i],pre[i+1]);
34         for(int i=1;i<=n;i++) scanf("%d",&b[i]);
35         sort(b+1,b+n+1);
36         int ma=0;
37         ll ans=pre[b[1]];
38         for(int i=2;i<=n;i++){
39             ma=max(pre[b[1]]-n-1,pre[b[i]]);
40             ans=(ma+ans)%mod;
41 //            printf("%d\n",ma);
42         }
43         printf("%lld\n",ans);
44     }
45
46 }
时间: 2024-08-06 15:59:58

2017 多校赛 第二场的相关文章

2015 多校赛 第二场 1006 (hdu 5305)

Problem Description There are n people and m pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline friends (mostly using face-to-face communication). However, everyo

2015 多校赛 第二场 1002 (hdu 5301)

Description Your current task is to make a ground plan for a residential building located in HZXJHS. So you must determine a way to split the floor building with walls to make apartments in the shape of a rectangle. Each built wall must be paralled t

2015 多校赛 第二场 1004 hdu(5303)

Problem Description There are n apple trees planted along a cyclic road, which is L metres long. Your storehouse is built at position 0 on that cyclic road.The ith tree is planted at position xi, clockwise from position 0. There are ai delicious appl

2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型

题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你可以获得的最大权值是多少? 题解: 对每个士兵建立一个点x ,点x 向源点s 连一条边,向汇点t 连一条边, 分别表示选择两种职业,然后就可以先加上所有的贡献,通过两点关系用 最小割建模,如下图所示 设一条边的三种贡献为A, B, C,可以得到以下方程: 如果x,y都是法师,你可以获得C的权值,但是

HDU 5742 It&#39;s All In The Mind (贪心) 2016杭电多校联合第二场

题目:传送门. 题意:求题目中的公式的最大值,且满足题目中的三个条件. 题解:前两个数越大越好. #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int gcd(int a,int b) { if(!b) return a; return gcd(b,a%b); } int main() { int t; ci

Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)

D 10248 修建高楼 D 传送门 题干 题目描述 C 市有一条东西走向的"市河".C 市的市长打算在"市河"的其中一条岸边自东往西的 n 个位置(可以将这 n 个位置看成在一条直线上,且位置不会重叠)依次建造高楼. C 市的设计部门设计了 T 个方案供市长挑选(方案编号为 1 到 T).每个方案都提供了建造的每幢高楼的高度,自东向西依次为 h1,h2,h3,-,hn-1,hn.每幢楼房的高度在 1 到 n 之间(包括 1 和 n),且各不相同. 市长在挑选设计方

2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. 解法:枚举要为这两段分配的颜色数目分别为 i,j ,则在第一段总共有 C(m,i) 种选取方案,在第二段总共有 C(m?i,j) 种选取方案.而在每段内部,我们设 F(n,x) 为长度为 n 的格子使用 x 种颜色(等于 x )染色的方案数.则根据容斥原理 F(n,x)=x^n?C(x,1)*(x

2017多校第7场 HDU 6129 Just do it 找规律

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6129 题意:求a序列后m次xor前缀和 解法: 手动对1位置对每个位置的贡献打表发现 第一次 贡献为 1 1 1 1 1 1 1 1 1 1 1 第二次 贡献为 1 0 1 0 1 0 1 0 1 0 1 0 第四次 贡献为 1 3个0 1 3个0 1 3个0 1 3个0 第八次 贡献为 1 7个0 1 7个0 1 7个0 1 7个0 ... 这是比赛之后才知道的,看着比赛的时候通过了200+人,被

2017 Fzu校赛 [Cloned]

A - Salty Fish 海边躺着一排咸鱼,一些有梦想的咸鱼成功翻身(然而没有什么卵用),一些则是继续当咸鱼.一个善良的渔夫想要帮这些咸鱼翻身,但是渔夫比较懒,所以只会从某只咸鱼开始,往一个方向,一只只咸鱼翻过去,翻转若干只后就转身离去,深藏功与名.更准确地说,渔夫会选择一个区间[L,R],改变区间内所有咸鱼的状态,至少翻转一只咸鱼. 渔夫离开后想知道如果他采取最优策略,最多有多少只咸鱼成功翻身,但是咸鱼大概有十万条,所以这个问题就交给你了! Input 包含多组测试数据. 每组测试数据的第