HDU6047 Maximum Sequence(贪心,暑期训练1003)
2017-07-27 19:19
260 查看
Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 159 Accepted Submission(s): 79
Problem DescriptionSteph 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<
4000
span>,
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
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;
题意是给了一个a数组,长度是n,然后要求从a[n+1]~a[2*n]的和
它的计算方法是这样的; 从a[n+1]开始,a[j]的值等于你在b数组中选一个数字k,那么a[j]的值就是a[j]-j(j<k<i)的最大值
以第一个样例来说明
编号 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
a | 8 | 11 | 8 | 5 |
b | 3 | 1 | 4 | 2 |
从a[2]开始算起
a[2]-2=9
a[3]-3=8
a[4]-4=1
a[5]的值就是他们中间最大的,也就是a[5]=9
到了计算a[6]的情况,这次我们在b数组里面选择1
a[1]-1=7
a[2]-2=9
a[3]-3=8
a[4]-4=1
a[5]-5=4
所以a[6]的值是9,以此类推
a[7]=5,a[8]=4
他们的和=9+9+5+4=27
我们的做法是直接在a[i]中存储a[i]-i的值
然后后开一个优先队列,让值比较大的先出队,把b数组中的每一个值都放到优先队列中,每次取队首,那么队首就是我们所要求的那个最大值
#include "stdio.h" #include "string.h" #include <iostream> #include <queue> #include <algorithm> using namespace std; const int MAX=250020; const int mod=1000000007; struct node { int v; int i; friend bool operator<(node n1,node n2) { return n1.v<n2.v; } }a[2*MAX],p; int b[MAX]; int main() { int n; while(~scanf("%d",&n)) { priority_queue<node> q; for(int i=1;i<=n;i++) { scanf("%d",&a[i].v); a[i].v=a[i].v-i; a[i].i=i; q.push(a[i]); } for(int i=1;i<=n;i++) scanf("%d",&b[i]); sort(b+1,b+1+n); int sum=0; for(int i=1;i<=n;i++) { p=q.top(); while(p.i<b[i]) { q.pop(); p=q.top(); } a[i+n].v=p.v; a[i+n].i=i+n; sum+=a[i+n].v; sum%=mod; a[i+n].v=a[i+n].v-(i+n); q.push(a[i+n]); } printf("%d\n",sum); } return 0; }
相关文章推荐
- CSU-ACM2017暑期训练7-模拟&&贪心A - Radar Installation
- HDOJ贪心训练1003最小拦截系统
- CSU-ACM2017暑期训练7-模拟&&贪心 A - Radar Installation POJ - 1328
- CSU-ACM2017暑期训练7-模拟&&贪心 F - Sokoban
- 2017多校训练第二场 hdu6047 Maximum Sequence(贪心)
- CSU-ACM2017暑期训练7-模拟&&贪心 E - 荷马史诗 HYSBZ - 4198
- 暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)
- CSU-ACM2017暑期训练10-并查集&&HASH F - Flying to the Mars HDU - 1800(字符串hash)
- PAT训练(乙级)—— 1003. 我要通过!(20)
- hdu 1003 - Max Sum(分治of贪心)
- CSU-ACM2017暑期训练16-树状数组 C - 按钮控制彩灯实验 CSU - 1770
- 2017多校训练Contest2: 1003 Maximum Sequence hdu6047
- 2014基本贪心1003
- uva 11729 突击队(训练思维+贪心)
- 2016 北邮暑期训练3-D题(Codeforces 698A Vacations)DP,水题
- 蓝桥杯 算法训练 校门外的树 (贪心线段排序)
- CSU-ACM2017暑期训练1-Debug与STL hdu1896-Stones
- hpu暑期训练:I - 0和5 【思维】
- 【【henuacm2016级暑期训练】动态规划专题 E】Destroying Roads
- Languages--CSU-ACM2017暑期训练1-Debug与STL