HDU-6040 Hints of sd0061
来源:互联网 发布:苹果数据接口转usb 编辑:程序博客网 时间:2024/05/21 05:56
题意:给N个数字,M次查询,第i次查询问A数组中第B[i]小的数是多少。
思路:考察了快排的思想,STL的使用。
nth_element(A, A+K, A+N)
表示在数组A的[0, N-1]中找到第K小的(K从0开始算)并放在第K个位置,并且前K-1(共K个)个位置均为小于A[K]的数。复杂度将近线性。
nth_element(A+Q, A+K, A+N)
则表示从[A+Q, A+N-1]中找第K-Q(K-Q从0开始算)小的并放在第K个位置。
注意到在找到第K小的时候,前K-1个数均是小于A[K]的,这样我们从大往小枚举寻找,可以减少搜索量。
代码:
#include <bits/stdc++.h>using namespace std;const int maxn = 1e7+5;struct node{unsigned val, id;bool operator<(const node k)const{return val > k.val;}} B[105];unsigned A[maxn], ans[105];unsigned N, M, _A, _B, _C, K, UP;unsigned x, y, z;unsigned rng61() {unsigned t;x ^= x << 16;x ^= x >> 5;x ^= x << 1;t = x;x = y;y = z;z = t ^ x ^ y;return z;}int main(){int count = 0;while(~scanf("%u %u %u %u %u", &N, &M, &_A, &_B, &_C)){x = _A, y = _B, z = _C;for(int i = 1; i <= M; ++i){ scanf("%u", &B[i].val); B[i].id = i;}for(int i = 1; i <= N; ++i) A[i] = rng61();sort(B+1, B+M+1); UP = N+1;//nth_element(A, A + k, A+n)for(int i = 1; i <= M; ++i){K = B[i].val+1;nth_element(A+1, A+K, A+UP);UP = K; ans[B[i].id] = A[K];}printf("Case #%d:", ++count);for(int i = 1; i <= M; ++i) printf(" %u", ans[i]);puts("");}return 0;}
继续加油~
阅读全文
1 0
- HDU 6040 Hints of sd0061
- [HDU]-6040 Hints of sd0061
- HDU 6040 Hints of sd0061
- HDU-6040 Hints of sd0061
- hdu 6040 Hints of sd0061
- hdu 6040 Hints of sd0061
- Hints of sd0061 HDU
- HDU 6040 Hints of sd0061 STL
- hdu 6040 Hints of sd0061(STL)
- HDU 6040 Hints of sd0061 (技巧)
- hdu 6040 Hints of sd0061(nth_element)
- hdu 6040 Hints of sd0061(STL)
- HDU 6040 Hints of sd0061(nth_element)
- HDU 6040 Hints of sd0061 stl
- 【HDU 6040 Hints of sd0061】 思维 & STL
- HDU 6040 Hints of sd0061(快速排序)
- hdu 6040 Hints of sd0061[2017 Multi-University Training Contest
- hdu 6040 Hints of sd0061【STL线性排序函数】
- IE浏览器下AJAX缓存问题导致数据不更新的解决办法
- android webview无法上传文件的解决方案
- 详解LAMP源码编译安装
- Python定时任务的实现方式
- AVL树的基本操作 C++
- HDU-6040 Hints of sd0061
- error C2146: 语法错误: 缺少“)”(在标识符“,dtpSelected”的前面) 之中英文输入法的重要性
- 【视频开发】【Live555】摄像头采集,264编码,live555直播
- flask前台数据通过ajax传递到后台之点赞与视频播放量
- 基本数据类型和运算符
- C语言(1)素数距离问题
- C程序编译过程浅析
- Python 日志
- java中多线程与单双核关系