2017 多校训练第二场 HDU 6047 Maximum Sequence(贪心+优先队列)
2017-12-13 19:12
501 查看
Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2404 Accepted Submission(s): 1131
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}: $a_{n+1}…a_{2n}$. Just like always, there are some restrictions on $a_{n+1}…a_{2n}$: for each number $a_i$, you must choose a number $b_k$ from {bi},
and it must satisfy $a_i$≤max{$a_j$-j│$b_k$≤j<i}, and any $b_k$ can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{$\sum_{n+1}^{2n}a_i$} modulo $10^9$+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{$\sum_{n+1}^{2n}a_i$} modulo $10^9$+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;
Source
2017 Multi-University Training Contest - Team 2
题意:构造an+1到a2*n的数。构造方法是从bi中选一个数使得ai≤max{aj-j│bk≤j<i}。bi只能被选一次。求构造出来的an+1到a2*n的最大值。
思路:
aj-j中的aj是从原来a1到an中来的,而j的值是一直变大的。所以应该把大的值先加入ai+n中。
所以可以先将bi从小到大排序。用一个优先队列,存放ai-i的值,以它的较大值为优先级较高的。在构造出一个ai+n的时候并把ai+n-(i+n)放入队列中。继续往后构造,这样就能使得和最大。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=500010;
const int mod=1e9+7;
struct node
{
int i,w;
friend bool operator<(node a,node b)
{
return a.w<b.w;
}
}q[maxn];
int b[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
priority_queue<node> que;
for(int i=1;i<=n;i++)
{
scanf("%d",&q[i].w);
q[i].w=q[i].w-i;
q[i].i=i;
que.push(q[i]);
}
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
sort(b+1,b+1+n);
long long sum=0;
for(int i=1;i<=n;i++)
{
while(que.top().i<b[i]) que.pop();
node tmp=que.top();
sum=(sum+tmp.w)%mod;
tmp.i=i+n,tmp.w=tmp.w-tmp.i;
que.push(tmp);
}
printf("%lld\n",sum);
}
return 0;
}
相关文章推荐
- (2017多校训练第二场)HDU - 6047 Maximum Sequence 贪心 + 单调队列
- 2017 多校训练第二场 HDU 6047 Maximum Sequence
- hdu 6047: Maximum Sequence (2017 多校第二场 1003)【贪心】
- 2017 多校训练第二场 HDU 6055 Regular polygon
- 2017 多校训练第二场 HDU 6052 To my boyfriend
- (2017多校训练第二场)HDU - 6052 To my boyfriend 思维题
- 2017 多校训练第二场 HDU 6050 Funny Function
- 2017 多校训练第九场 HDU 6168 Numbers 贪心枚举
- (2017多校训练第二场)HDU - 6055 & POJ - 2002 Regular polygon 哈希
- 2017 多校训练第二场 HDU 6045 Is Derek lying?
- 2017 杭电多校联赛第二场 1003 Maximum Sequence(单调队列)HDU 6047
- 2017 多校训练第二场 HDU 6053 TrickGCD
- HDU - 6047 多校2 1003 Maximum Sequence (贪心+优先队列)
- (2017多校训练第二场)HDU - 6053 TrickGCD 初见莫比乌斯
- 2017 多校训练第二场 HDU 6045 Is Derek lying?(思维)
- (2017多校训练第一场)HDU - 6034 Balala Power! 贪心
- (2017多校2)1003/hdu-6047 Maximum Sequence(单调队列/优先队列)
- 【双向bfs】2017多校训练十 HDU 6171 Admiral
- 2017 多校训练第十场 HDU 6178 Monkeys 最小点覆盖+树形DP
- 2017 多校训练第一场 HDU 6038 Function