博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
51nod 1287 加农炮(二分/线段树)
阅读量:4207 次
发布时间:2019-05-26

本文共 1201 字,大约阅读时间需要 4 分钟。

题目来源: 
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 
 收藏
 关注
一个长度为M的正整数数组A,表示从左向右的地形高度。测试一种加农炮,炮弹平行于地面从左向右飞行,高度为H,如果某处地形的高度大于等于炮弹飞行的高度H(A[i] >= H),炮弹会被挡住并落在i - 1处,则A[i - 1] + 1。如果H <= A[0],则这个炮弹无效,如果H > 所有的A[i],这个炮弹也无效。现在给定N个整数的数组B代表炮弹高度,计算出最后地形的样子。
例如:地形高度A = {1, 2, 0, 4, 3, 2, 1, 5, 7}, 炮弹高度B = {2, 8, 0, 7, 6, 5, 3, 4, 5, 6, 5},最终得到的地形高度为:{2, 2, 2, 4, 3, 3, 5, 6, 7}。
Input
第1行:2个数M, N中间用空格分隔,分别为数组A和B的长度(1 <= m, n <= 50000)第2至M + 1行:每行1个数,表示对应的地形高度(0 <= A[i] <= 1000000)。第M + 2至N + M + 1行,每行1个数,表示炮弹的高度(0 <= B[i] <= 1000000)。
Output
输出共M行,每行一个数,对应最终的地形高度。
Input示例
9 1112043215728076534565
Output示例
222433567

另建一个数组conno,表示h[]的递增序列,用二分找炮弹打过来的高地,将其之前的土地高度+1,然后更新高地高度。详见代码

const int maxn=5e4+10;int n,m;int h[maxn],conno[maxn];int main(){    ios::sync_with_stdio(false);    cin>>n>>m;    int maxh=-1;    memset(conno,0,sizeof(conno));    for(int i=1;i<=n;i++)    {        cin>>h[i];        maxh=max(maxh,h[i]);        conno[i]=maxh;    }    int gun,pos;    for(int i=1;i<=m;i++)    {        cin>>gun;        if(gun<=conno[0] || gun>conno[n]) continue;        pos=lower_bound(conno+1,conno+1+n,gun)-conno;        h[pos-1]++;        conno[pos-1]=max(conno[pos-1],h[pos-1]);    }    for(int i=1;i<=n;i++)cout<
<

转载地址:http://peali.baihongyu.com/

你可能感兴趣的文章
Maven 使用Intellij IDEA部署添加Maven Module出现 'pom.xml' already exists in VFS
查看>>
Git .gitignore文件比较完善的写法
查看>>
JavaWeb 提交中文数据乱码问题总结
查看>>
JavaWeb forward与sendRedirect区别
查看>>
JavaWeb 报错The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml
查看>>
JavaWeb getParameter和getAttribute的区别
查看>>
JavaWeb jsp内置对象与servlet对应关系
查看>>
Spring 之依赖注入DI
查看>>
Spring 注解总结
查看>>
Spring 面向切面编程AOP
查看>>
数据库优化 SQL语句优化
查看>>
Spring 各个jar包的作用
查看>>
SpringMVC 出现ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
查看>>
SpringMVC 过滤器HiddenHttpMethodFilter
查看>>
SpringMVC 返回json数据报错IllegalArgumentException: No converter found for return value of type:xxx
查看>>
SpringMVC 基本配置文件
查看>>
Velocity 模板出现NestedIOException: Cannot find Velocity template for URL [layout.vm]
查看>>
Velocity 模板基本用法
查看>>
SpringMVC 使用总结
查看>>
Mybatis 出现Mapped Statements collection does not contain value for xxx
查看>>