[NOIP模拟] 序列操作
来源:互联网 发布:c语言a和a差 编辑:程序博客网 时间:2024/06/01 22:22
About:
Solution:
好气,我这道题写的权值线段树,结果维护的时候搞忘清零了,只有 20 分,加上清零就 A了,当然我是指的评测机上AC,在 OJ 上要 T 一些点,有点小气。
我用权值线段树,在修改的时候加了一个
Code :
权值线段树
#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <cmath>#include <ctime>#include <map>#include <vector>using namespace std;inline int read() { int i = 0, f = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)) { i = (i << 3) + (i << 1) + ch - '0'; ch = getchar(); } return i * f;}const int MAXH = 1e4 + 5;int h[MAXH], tr[MAXH << 2], MAX, mx;inline void update(int k) { tr[k] = tr[k << 1] + tr[k << 1 | 1];}inline void build(register int k, register int s, register int t) { if(s == t) return (void)(tr[k] = h[s]); int mid = s + t >> 1; build(k << 1, s, mid); build(k << 1 | 1, mid + 1, t); update(k);}inline int query(register int k, register int s, register int t, register int x) { if(s == t) return s; int mid = s + t >> 1; if(tr[k << 1 | 1] >= x) return query(k << 1 | 1, mid + 1, t, x); else return query(k << 1, s, mid, x - tr[k << 1 | 1]);}inline int querysze(register int k, register int s, register int t, register int l, register int r) { if(l <= s && t <= r) return tr[k]; int mid = s + t >> 1, res = 0; if(l <= mid) res = querysze(k << 1, s, mid, l, r); if(mid < r) res += querysze(k << 1 | 1, mid + 1, t, l, r); return res;}inline void modify(register int k, register int s, register int t, register int pos, register int x) { if(s == t) return (void)(tr[k] += x); int mid = s + t >> 1; if(pos <= mid) modify(k << 1, s, mid, pos, x); else modify(k << 1 | 1, mid + 1, t, pos, x); update(k);}int main() { int n = read(), m = read(); for(register int i = 1; i <= n; ++i) { int x = read(); ++h[x + 1], mx = max(mx, x + 1); } int MAX = 1e4 + 2; build(1, 1, MAX); int flag = 0; for(register int i = 1; i <= m; ++i) { int x = read(); int now = query(1, 1, MAX, x); if(now == 1) { flag = 1; cout<<i - 1; return 0; } int sze = querysze(1, 1, MAX, now, mx); if(now == mx) { int delta = sze - x; if(delta) modify(1, 1, MAX, now, -x), modify(1, 1, MAX, now - 1, x), h[now - 1] += x, h[now] -= x; else modify(1, 1, MAX, now, -sze), modify(1, 1, MAX, now - 1, sze), h[now - 1] += sze, h[now] -= sze, --mx; } else { int delta = sze - x; int nowsze = h[now]; modify(1, 1, MAX, now, -nowsze + delta), modify(1, 1, MAX, now - 1, nowsze - delta), h[now - 1] += nowsze - delta, h[now] = delta; for(register int i = mx; i >= now + 1; --i) { if(h[i]) modify(1, 1, MAX, i, -h[i]), modify(1, 1, MAX, i - 1, h[i]); } for(int i = now + 1; i <= mx; ++i) if(h[i]) h[i - 1] += h[i], h[i] = 0; --mx; } } if(!flag) cout<<m;}
普通的数组,但是AC
#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <cmath>#include <ctime>#include <map>#include <vector>using namespace std;inline int read() { int i = 0, f = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)) { i = (i << 3) + (i << 1) + ch - '0'; ch = getchar(); } return i * f;}const int MAXH = 1e4 + 5;int h[MAXH], tr[MAXH << 2], MAX, mx;inline int find(int x) { int now = 0; for(int i = mx; i >= 1; --i) { now += h[i]; if(now >= x) return i; }}inline int query(int x) { int now = 0; for(int i = mx; i >= x; --i) now += h[i]; return now;}int main() { int n = read(), m = read(); for(register int i = 1; i <= n; ++i) { int x = read(); ++h[x + 1], mx = max(mx, x + 1); } int MAX = mx; int flag = 0; for(register int i = 1; i <= m; ++i) { int x = read(); int now = find(x); if(now == 1) { flag = 1; cout<<i - 1; return 0; } int sze = query(now); if(now == mx) { int delta = sze - x; if(delta) h[now - 1] += x, h[now] -= x; else h[now - 1] += sze, h[now] -= sze, --mx; } else { int delta = sze - x; int nowsze = h[now]; h[now - 1] += nowsze - delta, h[now] = delta; for(int i = now + 1; i <= mx; ++i) h[i - 1] += h[i], h[i] = 0; --mx; } } if(!flag) cout<<m;}
阅读全文
0 0
- [NOIP模拟] 序列操作
- 【NOIP模拟】 (11.6) T2 序列操作
- NOIP模拟(11.06)T2 序列操作
- NOIP模拟 序列操作【线段树】
- 【NOIP模拟】序列
- 【noip模拟赛】序列问题
- NOIP 模拟题 可接受序列
- 括号序列noip模拟赛
- NOIP模拟题 括号序列
- [NOIP模拟](二)T1-操作
- 【noip模拟赛】球的序列
- NOIP模拟(11.02)T2 最佳序列
- 【NOIP模拟】 (11.2) T2最佳序列
- NOIP模拟(20171102)T2 最佳序列
- [NOIP2017模拟]序列操作
- noip模拟赛(10.4) 序列(sequence)
- [NOIP模拟题][DP][水题][数学][树链剖分][差分序列]
- [NOIP模拟][状压dp][dfs序列][线段树]
- css屏幕适配 自定义
- spring boot 命令行启动的一些坑
- verilog小笔记_文件的读取
- Zend的使用
- 文章标题 实验七:将menu设计为可重用的子系统
- [NOIP模拟] 序列操作
- Python正则表达式re模块
- Linux学习记录—进程相关
- js原生代码练习 js控制div属性
- 详解Java中的修饰符
- 用Antd和React写的Antd介绍网站,源码有详细注释
- 当app出现bug时,怎么判断是客户端的问题,还是接口的问题,或者是服务端的问题?
- php 验证码封装类
- @Autowired:【Could not autowire field:】