POJ 2328 解题报告
来源:互联网 发布:js登录清除cookie 编辑:程序博客网 时间:2024/06/03 23:42
这道题我是用segment tree做的。刚开始TLE,后来把cin, cout改成scanf, printf后就过了。
2823Accepted42344K6641MSC++3335B/* ID: thestor1 LANG: C++ TASK: poj2823 */#include <iostream>#include <fstream>#include <cmath>#include <cstdio>#include <cstring>#include <limits>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <algorithm>#include <cassert>using namespace std;void build(std::vector<pair<int, int> > &segmentTree, int left, int right, int index, std::vector<int> &nums){if (left != right){int mid = left + ((right - left) >> 1);build(segmentTree, left, mid, 2 * index + 1, nums);build(segmentTree, mid + 1, right, 2 * index + 2, nums);segmentTree[index].first = min(segmentTree[2 * index + 1].first, segmentTree[2 * index + 2].first);segmentTree[index].second = max(segmentTree[2 * index + 1].second, segmentTree[2 * index + 2].second);}else{// cout << "[build]index: " << index << ", left: " << left << ", nums[left]: " << nums[left] << endl;segmentTree[index].first = segmentTree[index].second = nums[left];}}void query(std::vector<pair<int, int> > &segmentTree, int left, int right, int index, int lindex, int rindex, pair<int, int> &minmax){if (lindex <= left && right <= rindex){minmax.first = min(minmax.first, segmentTree[index].first);minmax.second = max(minmax.second, segmentTree[index].second);return;}if (left != right){int mid = left + ((right - left) >> 1);if (rindex <= mid){query(segmentTree, left, mid, 2 * index + 1, lindex, rindex, minmax);}else if (mid + 1 <= lindex){query(segmentTree, mid + 1, right, 2 * index + 2, lindex, rindex, minmax);}else{query(segmentTree, left, mid, 2 * index + 1, lindex, rindex, minmax);query(segmentTree, mid + 1, right, 2 * index + 2, lindex, rindex, minmax);}}}void printSegmentTree(std::vector<pair<int, int> > &segmentTree, int left, int right, int index){cout << "index: " << index << " (" << segmentTree[index].first << ", " << segmentTree[index].second << ")" << endl;if (left != right){int mid = left + ((right - left) >> 1);printSegmentTree(segmentTree, left, mid, 2 * index + 1);printSegmentTree(segmentTree, mid + 1, right, 2 * index + 2);}}int main(){// std::ios::sync_with_stdio(false);int n, k;// cin >> n >> k;scanf("%d%d", &n, &k);std::vector<int> nums(n);for (int i = 0; i < n; ++i){// cin >> nums[i];scanf("%d", &nums[i]);}// cout << "nums:" << endl;// for (int i = 0; i < n; ++i)// {// cout << nums[i] << " ";// }// cout << endl;std::vector<pair<int, int> > segmentTree(4 * n);build(segmentTree, 0, n - 1, 0, nums);// printSegmentTree(segmentTree, 0, n - 1, 0);std::vector<pair<int, int > > minmaxs(n - k + 2, make_pair(INT_MAX, INT_MIN));for (int i = 0; i + k - 1 < n; ++i){// i, i + k - 1;query(segmentTree, 0, n - 1, 0, i, i + k - 1, minmaxs[i]);}for (int i = 0; i + k - 1 < n; ++i){// cout << minmaxs[i].first << " ";printf("%d ", minmaxs[i].first);}// cout << endl;printf("\n");for (int i = 0; i + k - 1 < n; ++i){// cout << minmaxs[i].first << " ";printf("%d ", minmaxs[i].second);}// cout << endl;printf("\n");return 0; }
0 0
- POJ 2328 解题报告
- poj数百篇解题报告
- poj 1141解题报告
- poj 1001解题报告
- poj 1011 解题报告
- poj 1087 解题报告
- poj 2153解题报告
- poj 2051解题报告
- POJ 1167 解题报告
- poj 1026 解题报告
- POJ 1077 解题报告
- POJ 1042 解题报告
- POJ 1095 解题报告
- POJ 1159解题报告
- poj 2312解题报告
- POJ 1001解题报告
- POJ 1002解题报告
- POJ 1006解题报告
- [转]Android蓝牙开发浅谈
- POJ1298 The Hardest Problem Ever
- Flume+kafka+storm整合
- systemctl替代service与chkconfig
- HDU1028 Ignatius and the Princess III(母函数模板)
- POJ 2328 解题报告
- UVA12627 Erratic Expansion
- IT小李传--药店里的装饰模式
- Apache Hadoop 2.6.0 新特性
- 第12 章 索引
- Qt QLineEdit QLabel添加clicked事件,给控件添加事件方法
- 数学之路-数据分析进阶-Cox比例风险回归模型
- 新的一周
- [LeetCode] Remove Duplicates from Sorted Linked List 从排序链表中去掉重复值