HDU 6047 Maximum Sequence(贪心+线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047
题目:
Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 90 Accepted Submission(s): 44
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{∑2nn+1ai} modulo 109+7 .
Now Steph finds it too hard to solve the problem, please help him.
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.
多校联赛第二场~
题意:
给定一个长度为n的a数组和b数组,要求a[n+1]…a[2*n]的最大总和。 限制条件为ai≤max{aj-j│bk≤j<i}。
思路:
a[j](j>n)是从当前选择的a数组的b[k]个数开始,到最后一个数中选。由于每个b[k]都只能使用一次,我们要可能地把b[k]较大的数留在后面用,因为刚开始a数组只有n个,只有随着每次操作a数组才会增加一个数。
顺着这个思路,我们很自然地先对b数组做一次升序排序,再以b[k]为左区间,a数组当前的个数为右区间,来找最大的a[j]; 因为数据量比较大,我们经常要获取某个区间a[j]的最大值,所以用线段树维护。
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N=*+;
const int M= 1e9+;
typedef long long ll;
struct node{
int l,r;
int Max;
ll sum;
}tree[*N];
int n;
int a[N],b[N];
void pushup(int i){
tree[i].sum=(tree[*i].sum+tree[*i+].sum)%M;//别忘了mod运算
tree[i].Max=max(tree[*i].Max,tree[*i+].Max);
}
void build(int l,int r,int i){
if(i>*n) return ;
tree[i].l=l;
tree[i].r=r;
if(tree[i].l==tree[i].r) {
if(l>n){
tree[i].sum=;
tree[i].Max=;
}else{
tree[i].sum=a[l];
tree[i].Max=a[l]-l;//MAX存的是a[j]-j;
}
return ;
}
int mid=(l+r)/;
build(l,mid,*i);
build(mid+,r,*i+);
pushup(i);//回溯更新父节点
}
void update(ll v,int x,int i){
if(tree[i].l==tree[i].r){
tree[i].sum=v;
tree[i].Max=v-x;
return ;
}
int mid=(tree[i].l+tree[i].r)/;
if(x<=mid) update(v,x,*i);
else update(v,x,*i+);
pushup(i);
}
int query(int l,int r,int i){
if(tree[i].l==l && tree[i].r==r) return tree[i].Max;
int mid=(tree[i].l+tree[i].r)/;
if(r<=mid) return query(l,r,*i);
else if(l>mid) return query(l,r,*i+);
else if(l<=mid && r>mid) return max(query(l,mid,*i),query(mid+,r,*i+));
return -;
}
int main(){
while(scanf("%d",&n)!=EOF){
int pre=;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++) scanf("%d",&b[i]);
build(,*n,);
sort(b,b+n);
for(int i=n+;i<=*n;i++){
int x,y;
int bg,ed=i-;
x=bg=b[pre++];//排序完直接按顺序取b数组,保证了不会重复使用
y=query(bg,ed,);
a[i]=max(x,y);
update(a[i],i,);
}
printf("%lld\n",tree[].sum);//tree[3]保存的是n+1…2*n的节点信息
}
return ;
}
HDU 6047 Maximum Sequence(贪心+线段树)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)
题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...
- hdu 6047 Maximum Sequence 贪心
Description Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: ...
- 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 ...
- 【多校训练2】HDU 6047 Maximum Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=6047 [题意] 给定两个长度为n的序列a和b,现在要通过一定的规则找到可行的a_n+1.....a_2n,求su ...
- 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 ...
随机推荐
- react navigation goBack()返回到任意页面(不集成redux) 二
实现思路: A -- > B (获取A的key值,传至C)-- >C(获取B传来的A页面key值,传至D) -- >D(获取C传来的A页面key值&C页面的key值,传至下一 ...
- vue-router钩子函数实现路由守卫
接上一篇,我们一起学习了vue路由的基本使用以及动态路由.路由嵌套以及路由命名等知识,今天我们一起来学习记录vue-router的钩子函数实现路由守卫: 何为路由守卫?路由守卫有点类似于ajax的请求 ...
- 关于svn更新失败,clearup异常解决
直接上主题: 1. 下载sqlite3工具(https://files.cnblogs.com/files/eric-fang/sqlite-tools-win32-x86-3210000.zip), ...
- Github 持续化集成 工作流 Npm包自动化发布
Github 持续化集成 工作流 Npm包自动化发布 简介 持续集成指的是,频繁地(一天多次)将代码集成到主干. 它的好处主要有两个: 快速发现错误.每完成一点更新,就集成到主干,可以快速发现错误 ...
- 开源流媒体Red5-编译和部署
源码下载地址:https://github.com/Red5/red5-server 使用工具:IntelliJ IDEA 下载源码后直接用IDEA打开,等待全部加载完成后 编译看是否报错,应该没什么 ...
- .Net Reactor混淆导致匿名类处理出现的问题处理分析
.Net Reactor 是一款比较不错的混淆工具,比VS自带的那个好用很多,一直以来也陪伴着我们的成长,虽然没有完美的混淆工具,不过也算还是不错的,至少能在一定程度上对DLL进行一定的保护处理. 不 ...
- mybatis #{}和${}的区别是什么?
#{}是预编译处理,${}是字符串替换.mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值,最后注入进去是带引号的:mybatis在 ...
- 从壹开始 [Admin] 之五 ║ 实现『按钮』级别权限配置
一.前情回顾 哈喽大家好,在这个欢庆的日子里,老张祝大家工作都能蒸蒸日上!今天正好也是社团成立的第一天,我也是希望今天能是个纪念日,沾沾这个大喜庆! 放假这两天,倒是学到了很多东西,我这个也是承认的, ...
- Java Map知识点
1.遍历 java遍历Map的方式有多种,一下以代码示例来说明使用: Map<String, String> tmap = new HashMap<String, String> ...
- 阿里云服务器ecs配置之安装nginx
一.简介 Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供反向代理服务. 二 .安装 1.准备工作 Nginx ...