2017多校训练第二场 hdu6047 Maximum Sequence(贪心)

2017多校训练第二场 hdu6047 Maximum Sequence(贪心)
Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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.

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{
∑(2n n+1)ai} 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;*

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

样例理解:
该组数据数列的初始长度为4(n),我们要把它变成一个长度为8(2n)的数列
第一行是这个数列每一位上的值(a1=8,a2=11,a3=8,a4=5)
第二行是你可以选每次从第几个数开始看起,每个数(bk)只能使用一次
比如第一次选择b数列中的2,那么就在a2到数列的最后选择一个aj-j里最大的数,那么就是a2-2=11-2=9,a3-3=8-3=5,a4-4=5-4=1,所以我们选择这些结果中最大的数9作为a5;第二次选择b数列中的1,那么就在a1到数列中的最后一个aj-j里最大的数,那么就是a1-1=7,a2-2=11-2=9,a3-3=8-3=5,a4-4=5-4=1,a5-5=4,所以我们选择这些结果中最大的数9作为a6;第三次选择b数列中的3,那么就在a3到数列中的最后一个aj-j里最大的数,那么就是a3-3=8-3=5,a4-4=5-4=1,a5-5=4,a6-6=3所以我们选择这些结果中最大的数5作为a7,第四次只能选择b数列中的4,那么就在a3到数列中的最后一个aj-j里最大的数,那么就是a4-4=5-4=1,a5-5=4,a6-6=3,a7-7=-2所以我们选择这些结果中最大的数4作为a8,所以这里最大的a5+a6+a7+a8数列和为9+9+5+4=27

算法:个人觉得这题的难点在于读懂题目,题目意思搞懂以后,很容易发现这是一个完全线性的问题,那么我们很容易想到贪心:第一次先把b数列中最小的数据取掉,越有利于最后答案变大。证明:若所有的最大值点都取自原本a1…an的数列,那么bk取的顺序是不会影响最后答案的(只是先加和后加的问题);若最大值的点从an+1取了,那么我们要做的是保证an+1是最大的。那么,我第一次把b中最小的数取出,就能找到可以取到的最大的aj-j,此时的值减掉(n+1),就是我们能得到的最大值,否则在后面取到的话,减掉的j只会越变越大。而当an+1取到的值最大以后,后面的数的顺序也不重要了,毕竟我们只求从i点到后面所有点中最大的那一个。

实现的话并不是太困难,排个序就好啦~
AC代码:

#include  <iostream>
#include  <stdio.h>
#include  <algorithm>
#include  <iomanip>
#include  <vector>
#include  <queue>
#include  <map>
#include  <cstring>
#include  <string>
#include  <cctype>
#include  <cmath>
typedef long long LL;
using namespace std;
const LL modd=1e9+7;
LL rec[250005];
LL maxx[250005];
LL p[250005];

int main()
{
    LL n,ans,res,tmpmax;
    while(~scanf("%lld",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&rec[i]);
            rec[i]=rec[i]-i;
        }
        maxx[n]=rec[n];
        for(int i=n-1;i>=1;i--)
            maxx[i]=max(maxx[i+1],rec[i]);
        for(int i=1;i<=n;i++) scanf("%lld",&p[i]);
        sort(p+1,p+1+n);
        ans=0;
        tmpmax=0;
        for(int i=1;i<=n;i++)
        {
            res=max(maxx[p[i]],tmpmax);
            ans=(ans+res)%modd;
            tmpmax=max(tmpmax,res-i-n);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值