Ada and List SPOJ
来源:互联网 发布:mac版qq群相册在哪儿 编辑:程序博客网 时间:2024/04/28 08:17
题目链接
https://vjudge.net/problem/SPOJ-ADALIST
题目大意
给你一串按顺序从1-n编号的数字串, 有3种操作:
- 在第k个位置插入一个数x
- 删除第k个数
- 输出第k个数
核心思想
题目给的时间很多, 所以可以考虑用stl容器做。这题是可以用vector做(看到有大佬用vector数组优化到4秒, 膜拜一下:https://vjudge.net/solution/8669611)
但是用一般方法,双端队列(deque)是比vector效率高一点(底层好像是分块数组)
双端队列
双端队列基本操作复杂度
push_back(在末尾插入):O(1)
push_front( 在前端插入 ):O(1)
pop_back( 删除末尾数): O(1)
pop_front(删除前端数):O(1)
insert( 在中间插入):O(n)
erase(在中间删除):O(n)
更多可参考该博客:http://blog.csdn.net/memewry/article/details/7782151
代码
#include<bits/stdc++.h>using namespace std;deque<int>d;//双端队列的声明int main(){ int n, q; while(~scanf("%d%d", &n, &q)) { for(int i=0; i<n; ++i) { int x; scanf("%d", &x); d.push_back(x);//在末尾添加数 } while(q--) { int c, k; scanf("%d%d", &c, &k); if(c==1) { int x; scanf("%d", &x); d.insert(d.begin()+k-1, x);//在指定位置插数写法(减一是因为题目序列从1开始,而deque从0开始) } else if(c==2) { d.erase(d.begin()+k-1);//删数写法 } else if(c==3) { printf("%d\n", d[k-1]);//deque有[]运算符,O(1)访问每个位置的数(也可以这么写d.at(k-1)) } } } return 0;}
1 0
- Ada and List SPOJ
- SPOJ Ada and Field
- Ada and Cycle SPOJ
- SPOJ ADALIST Ada and List(双端队列)
- SPOJ ADALIST Ada and List(vector的使用)
- spoj Ada and Queue 模拟+deque
- Ada and Field
- Ada and Queue
- Ada and Cycle
- Ada and Queue
- Ada
- ada
- ada
- Concurrent and Real-Time Programming in Ada
- Ada and Cycle+bfs+邻接表
- SPOJ ADAQUEUEAda and Queue
- Fibonacci with ADA and others (Part 1/3)
- Fibonacci with ADA and others (Part 2/3)
- 配置安卓环境
- JS的Document属性和方法
- document.createElement()的用法
- java selenium (五) 元素定位大全
- Windows 下查看进程下所有线程
- Ada and List SPOJ
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
- 从零开始学Makefile(六)
- de/encodeURI(Component)--"http%3A%2F%2Fwww.baidu.com%2Fasd%3Fa%3D123"
- tesnorflow的基本使用
- 采用cmd命令去除w7图标
- 一键升级pip
- java selenium (六) XPath 定位
- Apache Thrift 配置环境