2017 多校训练第二场 HDU 6047 Maximum Sequence(贪心+优先队列)

来源:互联网 发布:萧敬腾唱功知乎 编辑:程序博客网 时间:2024/05/18 12:42

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
48 11 8 53 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;}


阅读全文
0 0
原创粉丝点击