2017多校训练Contest2: 1003 Maximum Sequence hdu6047

news/2024/5/20 9:18:01 标签: , 贪心

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+1a2n . Just like always, there are some restrictions on  an+1a2n : 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


按照题意把bi-i压入中,每次取最大的即可

#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cassert>
#include<iostream>
#include<algorithm>
using namespace std;
long long mod=1000000007;
long long a[250001];
int b[250001];
priority_queue<pair<long long,int>> Q;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i;
        while(!Q.empty())
            Q.pop();
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            Q.push(make_pair(a[i]-i,i));
        }
        for(i=1;i<=n;i++)
            scanf("%d",&b[i]);
        sort(b+1,b+1+n);
        long long ans=0;
        for(i=1;i<=n;i++)
        {
            pair<long long,int> d;
            d=Q.top();
        //    Q.pop();
            while(d.second<b[i])
            {
                Q.pop();
                d=Q.top();
            }
            ans+=d.first;
            ans%=mod;
            d.first-=(i+n);
            d.second=i+n;
            Q.push(d);
        }
        ans=(ans+mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}



http://www.niftyadmin.cn/n/942461.html

相关文章

BZOJ 4566 [Haoi2016]找相同字符 后缀数组+ST表

Description 给定两个字符串&#xff0c;求出在两个字符串中各取出一个子串使得这两个子串相同的方案数。两个方案不同当且仅当这两个子串中有一个位置不同。Input 两行&#xff0c;两个字符串s1&#xff0c;s2&#xff0c;长度分别为n1&#xff0c;n2。1 <n1, n2< 20000…

[51nod1040]最大公约数之和

Description 求∑i1ngcd(i,n)n<10^9Solution 这道题有多种做法。 我们设f(n)∑ni1gcd(i,n)那么f应该是积性函数。&#xff08;证明自行脑补&#xff09; 也就是说我们要求出来f(pk)p是质数直接推式子似乎很麻烦&#xff0c;我们换个思路。 如何从f(pk)转移到f(pk1)? …

BZOJ 3230 相似子串 后缀数组+二分+ST表

Description Input 输入第1行&#xff0c;包含3个整数N&#xff0c;Q。Q代表询问组数。 第2行是字符串S。 接下来Q行&#xff0c;每行两个整数i和j。&#xff08;1≤i≤j&#xff09;。 Output 输出共Q行&#xff0c;每行一个数表示每组询问的答案。如果不存在第i个子串或第j个…

2017多校训练Contest2: 1009 TrickGCD hdu6053

Problem DescriptionYou are given an array A, and Zhu wants to know there are how many different array Bsatisfy the following conditions?* 1≤Bi≤Ai* For each pair( l , r ) (1≤l≤r≤n) , gcd(bl,bl1...br)≥2InputThe first line is an integer T(1≤T≤10) des…

[51nod1201]整数划分

Description 求将一个正整数n划分成不同的正整数的和的方案数。 n<50000 Solution 很巧妙的一道DP题。 我们设Fi,j表示将i划分成j个不同整数的方案。 j的上界大概是根号n的&#xff0c;不会超时。 那么我们有两种转移方案。 我们可以从Fi-j,j转移过来&#xff0c;把…

BZOJ 2756 [SCOI2012]奇怪的游戏 二分+网络流

Description Blinker最近喜欢上一个奇怪的游戏。 这个游戏在一个 N*M 的棋盘上玩&#xff0c;每个格子有一个数。每次 Blinker 会选择两个相邻 的格子&#xff0c;并使这两个数都加上 1。 现在 Blinker 想知道最少多少次能使棋盘上的数都变成同一个数&#xff0c;如果永远不能…

[51nod1766]树上的最远点对

Description 给出一棵n个点的树&#xff0c;每次询问编号在[a,b]中的一个点和编号在[c,d]一个点的最远距离。 n<10^5 Solution 我们知道&#xff0c;树上最远的距离是树的直径。 然后&#xff0c;直径可以由两个点集中的直径的总共四个端点两两配对得到。 于是我们就可…

BZOJ 1715 [Usaco2006 Dec]Wormholes 虫洞 SPFA

Description John在他的农场中闲逛时发现了许多虫洞。虫洞可以看作一条十分奇特的有向边&#xff0c;并可以使你返回到过去的一个时刻&#xff08;相对你进入虫洞之前&#xff09;。John的每个农场有M条小路&#xff08;无向边)连接着N &#xff08;从1..N标号&#xff09;块地…