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 ;
}
最新文章
- C#中Thread与ThreadPool的比较
- Alibaba
- iOS开发CoreAnimation解读之二——对CALayer的分析
- 学DSP(一):开始
- nginx-gridfs 的安装配置和使用
- 【C#、csharp】HTTPGET,POST请求
- Nodejs使用coffeescript编写的用户注册/登陆代码(MySQL)
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-3
- NLP —— 图模型(二)条件随机场(Conditional random field,CRF)
- Angular 向组件传递模板的几种方法
- 使用Python定制词云
- Nginx+Memcache+一致性hash算法 实现页面分布式缓存(转)
- 【爬虫】使用xpath与lxml移除特定标签
- 《Java语言实现快速幂取模》
- git回滚远程仓库代码/错提master分支的恢复
- 反向读取Mysql数据库表结构到PowerDesigner中
- Winform控件学习笔记【第六天】——TreeView
- list详解
- Highcharts 动态图
- 【剑指offer】翻转单词顺序,C++实现
热门文章
- 是时候让大家看看你用django写出来的博客了(内含部署教程视频)
- 一文搞懂 deconvolution、transposed convolution、sub-&#173;pixel or fractional convolution
- day 12 特殊权限
- WordPress后台地址路径修改方法
- 【面试题】Java常见面试题
- 一个有意思的js块作用域问题
- Spark 学习笔记之 优雅地关闭Spark Streaming
- centos7 scrapy安装
- egret引擎中使用tiled运行在微信小游戏中
- redis系列之------简单的动态字符串(SDS)