51Nod-1287-加农炮
来源:互联网 发布:查看linux详细版本 编辑:程序博客网 时间:2024/05/16 01:50
ACM模版
描述
题解
这道题数据比较水,可以直接爆破(代码One)。
也可以先预处理一下每个高度的炮弹能轰炸的位置,接下来逐个轰炸更新状态即可(代码Two)。
也可以用线段树解,是个不错的题。
代码
One:
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int MAXN = 5e4 + 10;int A[MAXN];int main(int argc, const char * argv[]){ int M, N; cin >> M >> N; int maxPos = 0; for (int i = 0; i < M; i++) { scanf("%d", A + i); if (A[i] > A[maxPos]) { maxPos = i; } } int B; for (int i = 0; i < N; i++) { scanf("%d", &B); if (B <= A[0] || B > A[maxPos]) { continue; } for (int i = 0; i < M; i++) { if (A[i] >= B) { A[i - 1]++; break; } } } for (int i = 0; i < M; i++) { printf("%d\n", A[i]); } return 0;}
Two:
#include <iostream>#include <cstdio>#define repe(i, a, n) for (int i = a; i <= n; i++)using namespace std;typedef long long LL;const int MAXN = 5e4 + 10;const int MAXM = 1e6 + 10;int a[MAXN], b[MAXN], p[MAXM];int main(){ int n, m, mx = 0; cin >> n >> m; repe(i, 1, n) { scanf("%d", a + i); } repe(i, 1, m) { scanf("%d", b + i); mx = max(mx, b[i]); } int s = 1; // 预处理不同高度炮弹的轰炸点 repe(i, 1, mx) { while (s <= n && a[s] < i) { s++; } p[i] = s - 1; } // 逐个遍历轰炸更新状态 repe(i, 1, m) { int v = p[b[i]]; if (0 == v || n == v) { continue; } a[v]++; p[a[v]] = min(p[a[v]], v - 1); } repe(i, 1, n) { printf("%d\n", a[i]); } return 0;}
参考
《线段树》
0 0
- 51Nod-1287-加农炮
- 51nod-1287 加农炮
- 51nod 1287 加农炮
- 【51Nod】1287 加农炮
- [51nod 1287]加农炮
- 51nod 1287 加农炮
- 51nod 1287 加农炮
- 51nod 1287: 加农炮 好题啊好题
- 51nod 1287 加农炮【二分】
- 51nod 1287 加农炮 思维
- 51nod 1287 加农炮 acm
- 51nod 1287 加农炮 【线段树】
- 51Nod 1287 加农炮 ( 暴力/线段树
- 51nod 1287 加农炮(分块)
- 51nod-加农炮
- 51Nod 加农炮
- 加农炮 51Nod
- 51nod 1287 加农炮(锻炼思维的好题)
- 洛谷 P1220 关路灯 [动态规划做法]
- 打印回型数组-回型矩阵-环形数组
- win7、win10进程pid4占用80端口的解决办法
- 通过Service播放音乐【Android】
- 欢迎使用CSDN-markdown编辑器
- 51Nod-1287-加农炮
- 工具类:自写一个简单的图片浏览器(LCGPicBrowser)
- 3D Touch-对TableView里的具体控件操作,类似微博。
- Attacking SS7: Mobile Operators Security Analysis
- redis常用场合及命令
- 迭代器设计模式和Map 遍历删除某一元素
- GridView
- 寻找父节点
- jQuery学习笔记--JqGrid相关操作 方法列表 备忘 重点讲解(超重要)