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 ...
随机推荐
- Cookie的删除
1.设置一个Cookie,与要删除的Cookie同名,并将有效时间设置为0: protected void doGet(HttpServletRequest request, HttpServletR ...
- 多线程——Runnable接口
以实现Runable接口的方式创建线程比继承Thread类有很大的优越性,因为类不能多重继承,即一个类只能继承一个类,那么如果该类已经继承了一个类,就不能实现多线程了,但是可以通过实现Runable接 ...
- odoo Botton标签属性详解
按钮属性 1)icon 按钮图标名,可用的按钮图标在 addons/web/static/src/img/下. 2)string 按钮的显示文字 3)type 动作执行类型.可能值是:workflow ...
- selenium--定位--CSS
大家在使用selenium元素定位的时候,通常更多使用的是XPATH,css定位方式用得比较少 但有时候css定位方式还是有一些优势的, 优势1:一般情况下定位速度要比XPATH快 优势2:语法要比X ...
- Flutter免费(视频)教程汇总
Flutter学习导航 Flutter简介: Flutter可以轻松快速地构建漂亮的移动应用程序. Flutter是谷歌的移动应用SDK,用于短时间内在iOS和Android上制作高质量的原生界面应用 ...
- 【Django】url(路由系统)
1.单一路由对应 url(r'^index/',views.index), 2.基于正则的路由 url(r'^index/(\d*)', views.index), url(r'^manage/(?P ...
- Micronaut 微服务中使用 Kafka
今天,我们将通过Apache Kafkatopic构建一些彼此异步通信的微服务.我们使用Micronaut框架,它为与Kafka集成提供专门的库.让我们简要介绍一下示例系统的架构.我们有四个微型服务: ...
- Spring 梳理 - 视图解析器 VS 视图(View,ViewResolver)
View View接口表示一个响应给用户的视图,例如jsp文件,pdf文件,html文件等 该接口只有两个方法定义,分别表明该视图的ContentType和如何被渲染 Spring中提供了丰富的视图支 ...
- 死磕 java线程系列之创建线程的8种方式
(手机横屏看源码更方便) 问题 (1)创建线程有哪几种方式? (2)它们分别有什么运用场景? 简介 创建线程,是多线程编程中最基本的操作,彤哥总结了一下,大概有8种创建线程的方式,你知道吗? 继承Th ...
- 如何配置VMware客户虚拟机使用NAT模式联网通信
本例中配置客户虚拟机(CentOS)使用NAT(网络地址转换Network Address Translation)模式进行联网到公网(互联网).客户机网段为192.168.1.0/24网段,经NAT ...