石子合并 BZOJ
来源:互联网 发布:小米4s网络制式参数 编辑:程序博客网 时间:2024/05/18 02:35
题目传送门
题意:石子合并问题一般来说都是O(N^3)的复杂度,如果用四边形不等式优化的话可以使时间复杂度降低到O(N^2)的复杂度,但是这个题目的数据范围是40000,所以这个题要用到GarsiaWachs算法,可以使时间复杂度降到O(N*logN),从而解决这个题目。
#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define MAXN 50010#define MAXE 40#define INF 1e9#define MOD 100003#define LL long long#define ULL unsigned long long#define pi 3.14159using namespace std;int stone[MAXN];int sum = 0;int cnt = 1;void combine(int k) { int temp = stone[k] + stone[k - 1]; sum += temp; for (int i = k; i < cnt - 1; ++i) { stone[i] = stone[i + 1]; } cnt--; int pos = k - 1; while (pos && stone[pos - 1] < temp) { stone[pos] = stone[pos - 1]; pos--; } stone[pos] = temp; while (pos >= 2 && stone[pos] >= stone[pos - 2]) { int d = cnt - pos; combine(pos - 1); pos = cnt - d; }}int main() { std::ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> stone[i]; } for (int i = 1; i < n; ++i) { stone[cnt++] = stone[i]; while (cnt >= 3 && stone[cnt - 3] <= stone[cnt - 1]) { combine(cnt - 2); } } while(cnt > 1) combine(cnt - 1); cout << sum << endl; return 0;}
阅读全文
0 0
- 石子合并 BZOJ
- [BZOJ 3895]取石子---博弈+石子合并
- 【BZOJ 3229】 [Sdoi2008]石子合并
- BZOJ-3229 石子合并 GarsiaWachs算法
- BZOJ-3229 合并石子变态版
- BZOJ 3229 [Sdoi2008]石子合并 GarsiaWachs算法
- BZOJ 3229: [Sdoi2008]石子合并 GarsiaWachs算法
- bzoj 3229: [Sdoi2008]石子合并 (GarsiaWachs算法)
- 石子-石子合并
- 石子合并
- 石子合并
- 石子合并
- 石子合并
- 石子合并
- 石子合并
- 【石子合并】
- 合并石子
- 石子合并
- 浅谈python中类,函数,包
- 在IIS部署网站
- 第五周项目3---括号的匹配
- Photoshop详细教程四之滤镜/视图/窗口基本功能介绍
- solr分片后副本数据不一致的问题
- 石子合并 BZOJ
- 12种排序算法
- otto使用心得与存在问题
- 给缺少PYTHON项目实战经验的人
- 针对ArcGIS Server 跨域问题的解释
- 关于VS2015调试DX11的HLSL着色器代码的方法与步骤
- 用redis实现支持优先级的消息队列
- pyspark- ml-features
- Java多线程(一)