HDU 6047 17多校 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: 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数组和b数组,然后给a在后面再填充n个数字,要求每次填充都是在b中选择一个数,设挑选的数为bx,设j为我目前要填充的数的序号,那么你填充的数就是a中序号从(bx)~(j-1)中最大的那个a[i]-i,最终使填充数的和最大。

题解:

1.要使最终结果最大的话,每次填充的数都尽可能的大,因为最先填的数减的数最小,所以对b来说先从小到大排序,能保证先填充的数最大。

2.为了维持每次挑选的是最大的数,可以用一个优先队列来维护,按照它的a[i]-i的值来进行排序。

3.最精华的地方,因为b已经排过序了,然后每次加的是最大的数,所以我只要保证队列的top的id要大于等于目前的b[i]即可。

每次用优先队列来维护最大最小值的题目我用sort来做超时一发的时候都觉得自己简直是太傻逼了///

为自己续一秒。。。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<queue>
 7 #include<vector>
 8 using namespace std;
 9
10 #define MOD 1000000000+7
11
12 struct node
13 {
14     long long num,ip,dis;
15 };
16
17 long long b[250005];
18
19 struct cmp
20 {
21     bool operator()(node q,node p)
22    {
23          return q.dis<p.dis;
24     }
25  };
26
27 int main()
28 {
29     long long n;
30     while(~scanf("%lld",&n))
31     {
32         priority_queue<node,vector<node>,cmp>Q;
33         node a;
34         for(int i=1;i<=n;i++)
35         {
36             scanf("%lld",&a.num);
37             a.ip=i;
38             a.dis=a.num-a.ip;
39             Q.push(a);
40         }
41         for(int i=1;i<=n;i++)
42             scanf("%lld",&b[i]);
43         sort(b+1,b+1+n);
44         long long res=0;
45         for(int i=1;i<=n;i++)
46         {
47             while(Q.top().ip<b[i])
48                 Q.pop();
49             node tmp=Q.top();
50             res+=tmp.dis;
51             res%=MOD;
52             tmp.ip=n+i;
53             tmp.num=tmp.dis;
54             tmp.dis-=tmp.ip;
55             Q.push(tmp);
56         }
57         printf("%lld\n",res);
58     }
59     return 0;
60 }
时间: 2024-11-06 16:29:59

HDU 6047 17多校 Maximum Sequence(优先队列)的相关文章

HDU 6059 17多校3 Kanade&#39;s trio(字典树)

Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k) satisfy that (i<j<k) and ((A[i] xor A[j])<(A[j] xor A[k])) There are T test cases. 1≤T≤20 1≤∑n≤5∗105 0≤A[i]<230 Input There is only one integer T on fi

HDU 6045 17多校2 Is Derek lying?

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 687    Accepted Submission(s): 389 Problem Description Derek and Alfia are good friends.Derek 

HDU 5360(2015多校6)-Hiking(优先队列)

题目地址:HDU 5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须满足c>=l[i]&&c<=ri,问你邀请的顺序是什么才能使尽可能多的人去远足,若有多个最优解,输出任意的一个. 思路:先按照L从小到到排序,把当前符合的L放入优先队列中 ,然后对队列中的R从小到大排序,贪心的选择R小的,然后乱搞一番就可以了. #include <stdio.h> #include <math.h> #includ

HDU 6077 17多校4 Time To Get Up 水题

Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading the time on the clock, Little Q lies down and starts sleeping again. Well, he has 5 alarms, and it's just the first one, he can continue sleeping for a

HDU 6090 17多校5 Rikka with Graph(思维简单题)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For an undirected graph G with n nodes and m edges, we can define the distance between 

HDU 6060 17多校3 RXD and dividing(树+dfs)

Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T. he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,where ?Si={2,3,…,n} and for all d

HDU 6055 17多校 Regular polygon(计算几何)

Problem Description On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make. Input The input file consists of several test cases. Each case the first line is a numbers

HDU 6050 17多校2 Funny Function(数学+乘法逆元)

Problem Description Function Fx,ysatisfies:For given integers N and M,calculate Fm,1 modulo 1e9+7. Input There is one integer T in the first line.The next T lines,each line includes two integers N and M .1<=T<=10000,1<=N,M<2^63. Output For eac

HDU 6092 17多校5 Rikka with Subset(dp+思维)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has n positive A1−An and their sum is m. Then for each subset S of A, Yuta calcula