51Nod
来源:互联网 发布:淘宝全民抢拍 编辑:程序博客网 时间:2024/05/16 19:05
1287 加农炮
题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注
一个长度为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 11
1
2
0
4
3
2
1
5
7
2
8
0
7
6
5
3
4
5
6
5
Output示例
2
2
2
4
3
3
5
6
7
自己的线段树好菜,,这个题有想法,但是苦于线段树实现自己能力的欠缺,一直没写。。。。昨天比赛时一个线段树水题错误百出,还好队友帮忙改了一下,改的过程中,好像真正明白了一点线段树,然后就做了一下以前没做出来的这个题,真的过了哎。。。。
思路:
就是改一下比当前大的结点的前一个点就好。
详细看代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100000+100;#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,rint dixing[maxn];struct node{ int l,r,val;}tree[maxn*4];void pushup(int rt){ tree[rt].val=max(tree[rt<<1].val,tree[rt<<1|1].val);}void build(int rt,int l,int r){ tree[rt].l=l; tree[rt].r=r; if(l==r) { tree[rt].val=dixing[l]; return; } int mid=(l+r)/2; build(lson); build(rson); pushup(rt);}void xin(int rt,int l,int r){ if(tree[rt].l==l&&tree[rt].r==r) { tree[rt].val+=1; return; } int mid=(tree[rt].l+tree[rt].r)/2; if(l>mid) { xin(rt<<1|1,l,r); } else { xin(rt<<1,l,r); } pushup(rt);}void update(int rt,int w){ if(tree[rt].l==tree[rt].r) { dixing[tree[rt].l-1]+=1; //printf(" 111 :%d\n",tree[rt].l-1); xin(1,tree[rt].l-1,tree[rt].l-1); return ; } if(tree[rt<<1].val>=w) { update(rt<<1,w); } else { update(rt<<1|1,w); } pushup(rt);}int main (){ int n,m; scanf("%d%d",&n,&m); int i,j; for(i=1;i<=n;i++) { scanf("%d",&dixing[i]); } build(1,1,n); while(m--) { int xx; scanf("%d",&xx); if(xx<=dixing[1]||xx>tree[1].val) continue; update(1,xx); //printf("%d 111\n",ans); //xin(1,ans,ans); } for(i=1;i<=n;i++) { printf("%d\n",dixing[i]); }}
- 51Nod
- 51Nod
- 51nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51Nod
- 51nod
- drawable与bitmap的转换,以及如何将一个view生成一个bitmap
- UVA 1339
- Halcon及license文件下载网址及安装步骤
- Java10年技术栈
- java并发编程之五
- 51Nod
- USB基础知识
- 关于Java中集合的简介
- pug模板的使用及终端
- Java笔记
- CSS注释代码
- JAVA入门
- 51Nod
- Intellji oschina同步python工程后异常处理