http://acm.hdu.edu.cn/showproblem.php?pid=6047

【题意】

  • 给定两个长度为n的序列a和b,现在要通过一定的规则找到可行的a_n+1.....a_2n,求sum{a_n+1.....a_2n}的最大值
  • 每次从序列b中选择一个数bk,那么构造的ai就是max{aj -j},其中bk<=j<i
  • 每个bk最多用一次

【思路】

  • 首先我们需要的是aj-j
  • 可以贪心,即对于当前要构造的数ai,尽可能找bk使得能够取得最大的aj-j
  • 线段树区间查询最优值+单点更新
  • 把b排个序,对于每个b查询b[i]~n+i-1的最大值

【Accepted】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#define lson (i<<1)
#define rson (i<<1|1)
#include<vector>
#include<map>
using namespace std;
const int maxn=(25e4+)*;
typedef long long ll;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
long long val[maxn];
int a[maxn];
int b[maxn];
int n;
struct Seg
{
int l,r,lazy,val;
}tree[maxn<<]; void push_up(int i)
{
tree[i].val=max(tree[lson].val,tree[rson].val);
} void push_down(int i)
{
if(tree[i].lazy==-)
{
return;
}
tree[lson].lazy=tree[lson].val=tree[i].lazy;
tree[rson].lazy=tree[rson].val=tree[i].lazy;
tree[i].lazy=-;
} void Build(int l,int r,long long i=)
{
tree[i].l=l;
tree[i].r=r;
tree[i].lazy=-;
if(l==r)
{
tree[i].val=val[l];
return;
}
push_down(i);
int mid=(tree[i].l+tree[i].r)>>;
Build(l,mid,lson);
Build(mid+,r,rson);
push_up(i);
} int Query(int l,int r,int i=)
{
if(tree[i].l==l&&tree[i].r==r)
{
return tree[i].val;
}
push_down(i);
int mid=(tree[i].l+tree[i].r)>>;
if(r<=mid)
{
return Query(l,r,lson);
}
else if(l>mid)
{
return Query(l,r,rson);
}
else
{
return max(Query(l,mid,lson),Query(mid+,r,rson));
}
} void Setval(int l,int r,int x,int i=)
{
if(tree[i].l==l&&tree[i].r==r)
{
tree[i].lazy=tree[i].val=x;
return;
}
push_down(i);
int mid=(tree[i].l+tree[i].r)>>;
if(r<=mid)
{
Setval(l,r,x,lson);
}
else if(l>mid)
{
Setval(l,r,x,rson);
}
else
{
Setval(l,mid,x,lson);
Setval(mid+,r,x,rson);
}
push_up(i);
} int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=*n;i++)
{
val[i]=-inf;
}
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
val[i]=a[i]-i;
}
Build(,*n);
for(int i=;i<=n;i++)
{
scanf("%d",&b[i]);
}
sort(b+,b++n);
ll ans=;
b[]=;
for(int i=;i<=n;i++)
{
int mmax=Query(b[i],n+i-);
ans=(ans+(ll)mmax)%mod;
Setval(n+i,n+i,mmax-(n+i));
}
cout<<ans<<endl;
}
return ;
}

线段树区间查询最优值+单点修改

【多校训练2】HDU 6047 Maximum Sequence的更多相关文章

  1. 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],在 ...

  2. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  3. HDU 6047 Maximum Sequence(贪心+线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  4. HDU 6047 Maximum Sequence

    Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. 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) ...

  6. 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)

    题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...

  7. hdu 6047: Maximum Sequence (2017 多校第二场 1003)【贪心】

    题目链接 可以贪心写,先把b数组按从小到大的顺序排个序,根据b[i]的值来产生a[n+i] 借助一个c数组,c[i]记录,j从i到n,a[j]-j的最大值,再加上一个实时更新的变量ma,记录从n+1到 ...

  8. hdu 6047 Maximum Sequence(贪心)

    Description Steph is extremely obsessed with "sequence problems" that are usually seen on ...

  9. hdu 6047 Maximum Sequence 贪心

    Description Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: ...

随机推荐

  1. Aappcloud 调到二级页面黑屏

    PartnerHead3.html 后面多了一个点

  2. Dapper系列之一:Dapper的入门(多表批量插入)

    Dapper介绍  简介:      不知道博客怎么去写去排版,查了好多相关博客,也根据自己做过项目总结,正好最近搭个微服务框架,顺便把搭建微服务框架所运用的知识都进行博客梳理,为了以后复习,就仔细琢 ...

  3. jQuery在$(function(){})中調用函數

    任務太緊,很少記筆記,記下一篇jQuery中調用函數的例子: 該方法是在載入頁面的時候,判斷 ModelName 不為空,則獲取Model信息加載到Table中: 另外,在點擊半成品編號文本框時,也調 ...

  4. Android提供的对话框

    1.普通对话框: 给出提示信息,有yes.no两个按钮. AlertDialog dialog=new AlertDialog.Builder(this) //this代表当前Activity对象,表 ...

  5. poj2385 Apple Catching

    思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...

  6. General mistakes in parallel computing

    这是2013年写的一篇旧文,放在gegahost.net上面  http://raison.gegahost.net/?p=97 March 11, 2013 General mistakes in ...

  7. Proteus与Keil连接及其仿真(有例子哦!)

    记录一下Proteus仿真的一些设置和使用,以方便自己以后复习和大家交流!如有错误,希望大家指正. 1.Proteus软件的安装,这里就不作说明了.

  8. C# 图片打印杂谈

    日常开头水一下,看了下上次博客,一年零八天了,啧啧,奢侈. 最近这个工作挺满意的,是我想要的发展方向,后续要做机器学习,现在得先把公司之前堆积的问题解决了. 谈人生到此结束,还是说正题吧.(感觉这标题 ...

  9. Mybatis的Service循环调用错误

    org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'z ...

  10. JDO

    JDO 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! JDO(Java Data Object )是Java对象持久化的新的规范,也是一个用于存取某种数据仓库中的对象 ...