hdu 6047 Maximum Sequence 贪心
Description
Given two integer sequences {ai} and {bi} with the same length n, you are to find the next n numbers of {ai}: an+1…a2nan+1…a2n. Just like always, there are some restrictions on an+1…a2nan+1…a2n: for each number aiai, you must choose a number bkbk from {bi}, and it must satisfy aiai≤max{ajaj-j│bkbk≤j<i}, and any bkbk can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{∑2nn+1ai∑n+12nai} modulo 109109+7 .
Now Steph finds it too hard to solve the problem, please help him.
Input
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
Sample Input Sample Output
数列要满足aj <= max{ai - i},其中bk <= j < i,bk是数列b中的一项,且每个bk最多仅能取一次
看到这里肯定还看不明白。
具体说一下第一个样例:
a数组是8 11 8 5 b数组是3 1 4 2
现在要扩充a数组,然后数组的扩充方法是,a数组中每个数等于本身减去他的下标,然后从b数组中选一个数,然后a数组的这个数的位置开始到最后选一个最大的(只能解释到这样了)。
实现:a数组相当于 (8-1)(11-2)(8-3)(5-4)
从b数组中选择1,代表从a[1]到a[4]中选择一个最大的,选择9。然后将9添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)
然后第二次从b数组中选择2代表从a[2]到a[5]中选择一个最大的,选择9。然后将9添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)(9-6)
然后第三次从b数组中选择3代表从a[3]到a[6]中选择一个最大的,选择5。然后将5添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)(9-6)(5-7)
然后第四次从b数组中选择4代表从a[4]到a[7]中选择一个最大的,选择4。然后将9添加到a数组中,然后数组为(8-1)(11-2)(8-3)(5-4) (9-5)(9-6)(5-7)(4-8)
然后更新完成了。最后数列a的n+1到2*n项的值为9+9+5+4=27
思路:
要想使数列a的n+1到2*a项最大,又每项都要减去i
所以应该尽量使bk最小,从最前面开始(可以理解为贪心)
那么肯定要先把最大的放进来
将b排序,计算ai - i的值
开一个max数组记录从i 到最后一项的最大ai-i的值(可以从后往前找)
每次加入点的时候,从后往前比较,如果小于这个数,就更新为这个数。
用一个sum记录总和。
记得每次加的结果要%mod。
注意数组要开为数据量的两倍,因为a数组要扩展!
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
int mod=1e9+;
using namespace std;
int a[],b[],maxx[];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(maxx,,sizeof(maxx));
for(int i=; i<=n; i++)
{
scanf("%d",a+i);
a[i]-=i;
}
for(int i=; i<=n; i++)
{
scanf("%d",b+i);
}
sort(b+,b+n+);
maxx[n]=a[n];
for(int i=n; i>=; i--)//更新max数组
{
maxx[i-]=max(a[i-],maxx[i]);
}
int flag=n+;
int sum=;
for(int i=; i<=n; i++)
{
sum+=maxx[b[i]];//选择最大的数
a[flag]=maxx[b[i]]-(flag);
maxx[flag]=a[flag];
for(int j=flag-; j>=; j--)//加入新的点之后,要更新对应的max
{
if(maxx[j]<maxx[flag])
maxx[j]=maxx[flag];
else break;
}
flag++;
sum=sum%mod;
}
printf("%d\n",sum);
}
}
hdu 6047 Maximum Sequence 贪心的更多相关文章
- HDU 6047 Maximum Sequence(贪心+线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- HDU 6047 Maximum Sequence (贪心+单调队列)
题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由 ...
- HDU 6047 - Maximum Sequence | 2017 Multi-University Training Contest 2
/* HDU 6047 - Maximum Sequence [ 单调队列 ] 题意: 起初给出n个元素的数列 A[N], B[N] 对于 A[]的第N+K个元素,从B[N]中找出一个元素B[i],在 ...
- HDU 6047 Maximum Sequence(线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- HDU 6047 Maximum Sequence
Maximum Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 2017 Multi-University Training Contest - Team 2&&hdu 6047 Maximum Sequence
Maximum Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 6047 Maximum Sequence(贪心)
Description Steph is extremely obsessed with "sequence problems" that are usually seen on ...
- hdu 6047: Maximum Sequence (2017 多校第二场 1003)【贪心】
题目链接 可以贪心写,先把b数组按从小到大的顺序排个序,根据b[i]的值来产生a[n+i] 借助一个c数组,c[i]记录,j从i到n,a[j]-j的最大值,再加上一个实时更新的变量ma,记录从n+1到 ...
- 【多校训练2】HDU 6047 Maximum Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=6047 [题意] 给定两个长度为n的序列a和b,现在要通过一定的规则找到可行的a_n+1.....a_2n,求su ...
随机推荐
- 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队
[算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...
- JS中的表单验证+正则表达式
表单验证+正则表达式 一.非空验证 trim:去空格(去掉前后的空格),任何字符串都可以用这个方法.写法为:if(v.trim().length==0),表示如果去掉空格后的字符串的长度为0. < ...
- Codeforces Round #478 (Div. 2)
题目链接:http://codeforces.com/contest/975 A. Aramic script time limit per test:1 second memory limit pe ...
- bzoj 1483 链表启发式合并
首先我们可以比较容易的在n的时间内算出来开始的答案,我们维护一些链表,分别表示不同的颜色,那么我们在计算答案的时候,只需要扫一遍所有的链表,判断链表相邻两项是否在序列中相邻,不相邻的话肯定在这其中的一 ...
- web-project 故障查看功能 检测是否启动fmd服务
def check_fmd_service(): try: output = subprocess.check_output('svcs -H -o state fmd',shell=True) st ...
- G题 hdu 1466 计算直线的交点数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1466 计算直线的交点数 Time Limit: 2000/1000 MS (Java/Others) ...
- SUSE 11.3 linux ISO下载地址
http://linux.iingen.unam.mx/pub/Linux/Suse/isos/SLES11/ SLE-11-SP3-SDK-DVD-i586-GM-DVD1.iso 6deaa960 ...
- MVC自定义路由实现URL重写,SEO优化
//App_Start-RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollect ...
- 【codeforces85D】
去实验培训回来了……写个题先玩玩 这题给人一种平衡树的感觉 但是呢,实际上操作离线+离散化+线段树一样能做 #include<bits/stdc++.h> #define lson (o& ...
- JPA注解一对多报Could not determine type for: java.util.List错误
在原来的项目上加新功能,启动后报Caused by: org.hibernate.MappingException: Could not determine type for: java.util.L ...