传送门:HDU 6047
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+1…a2n. Just like always, there are some restrictions on an+1…a2n: 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{
∑
n
+
1
2
n
a
i
\sum_{n+1}^{2n}a_i
∑n+12nai} 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≤ai≤1500000, 1≤bi≤n.
Output
For each test case, print the answer on one line: max{
∑
n
+
1
2
n
a
i
\sum_{n+1}^{2n}a_i
∑n+12nai} modulo 109+7。
Sample Input
4
8 11 8 5
3 1 4 2
Sample Output
27
题意:
这题题目挺难懂的,看了好久才明白它是个什么取法TAT。
给你两个长度都是n的数组,分别为a,b。1≤n≤250000, n≤ai≤1500000, 1≤bi≤n
你可以任意在b选一个数bk,然后a从第bk个到最后一个,得到max{ai-i},记a(n+1)=max{ai-i};
如此往复,每次选择的bk不能重复即每个b只能选一次。
当b数组里的每个数全部被选取后,将新得到的数组a(n+1) 到 a(2n)求和。
我们要求最大的和。
题解:
个人觉得这题的难点在于读懂题目,题目意思搞懂以后,很容易发现这是一个线性的问题,我们很容易想到贪心:每次取b数组中最小的数。我们越先把大的数取出来,我们和就越大,因为越往后取要减去的数也越大。
我们只求从i点到后面所有点中最大的那一个,所以后面数的顺序也不重要。
实现就更简单了,只要用sort排个序就行了。
AC代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int mod=1e9+7;
const int N=250000*2;
int n;
ll a[N],b[N];
ll maxn[N];
int main()
{
while(~scanf("%d",&n))
{
memset(maxn,0,sizeof(maxn));
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
a[i]=a[i]-i;
maxn[i]=a[i];
}
for(int i=n-1;i>=1;i--)
{
maxn[i]=max(maxn[i],maxn[i+1]);
}
for(int i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
}
sort(b+1,b+n+1);
ll ans=0;
ll MAX=0,tmp=0;
for(int i=1;i<=n;i++)
{
tmp=max(maxn[b[i]],MAX);
ans=(ans+tmp)%mod;
MAX=max(MAX,tmp-n-i);
}
printf("%lld\n",ans);
}
return 0;
}