hdu 6047 Maximum Sequence

来源:互联网 发布:淘宝轮毂 编辑:程序博客网 时间:2024/05/19 04:26

http://acm.hdu.edu.cn/showproblem.php?pid=6047

题意,给定一个数组{an}和一个数组{bn},按照一定规则生成{a2n},规则是:aimax{ajj|bkj<i},每一个bk只能选择一次。

例如样例:

4
8 11 8 5
3 1 4 2

选择a5的时候,前四个为7 9 5 1,选择b2a5=9
选择a6的时候,前五个为7 9 5 1 4,选择b4a6=9
选择a7的时候,前五个为7 9 5 1 4 3,选择b1a7=5
选择a8的时候,前五个为7 9 5 1 4 3 -2,选择b3a8=4

2ni=n+1ai最大为27。

考虑用一个优先队列维护aii以及其下标,然后按照{bn}升序排序,对于队列中下标大于bi的选择出队,然后选择队首作为ai+n的值。

#include <bits/stdc++.h>using namespace std;typedef long long ll;const ll MOD = 1e9+7;int a[300000];int b[300000];struct node{    int a,id;};struct cmp{    bool operator ()(const node &a,const node &b){        return a.a<b.a;    }};priority_queue< node ,vector<node > ,cmp> Q;int main(){    int n;    while(~scanf("%d",&n)){        for(int i=1;i<=n;i++)            scanf("%d",a+i);        for(int i=1;i<=n;i++)            scanf("%d",b+i);        for(int i=1;i<=n;i++)            a[i]-=i;        int tmx = a[n];               while(!Q.empty()) Q.pop();        sort(b+1,b+1+n);        ll ans = 0;        for(int i=1;i<=n;i++){            Q.push(node{a[i],i});            }        int t=0;        for(int i=n+1;i<=n+n;i++){            while(Q.top().id<b[i-n]) Q.pop();            node now = Q.top();            ans += now.a;            ans %= MOD;            Q.push(node{now.a-i,i});        }        cout<<ans<<endl;    }    return 0;}
原创粉丝点击