HDU 6040 Hints of sd0061 STL
来源:互联网 发布:com.cn域名注册 编辑:程序博客网 时间:2024/06/05 15:27
传送门:HDU6040
题意:给出一个递推序列,和m个询问,问其中第mi小的数是多少。
官方题解:
1008. Hints of sd0061
最慢的情况是 b 的取值为 0, 1, 2, 3, 5, 8, ⋯ 的情况,但事实上也只有 O(log1.618n) 个取值。从最大的取值到最小的取值依次使用近似线性复杂度的求第 k 小的方法即可,该方法的思想与快排相似,可以保证前 k−1 小的元素都放置在第 k 小元素的前面,这样枚举的时候就可以依次减少每次的枚举量,时间复杂度 O(i≥0∑1.618in)=O(n) 。
标程给的是用nth_element()库函数实现的。基本格式:
nth_element(arr.begin(), arr + n - 1, arr.end)
调用函数后保证比第n个数小的数都在前面,比它大的数在后面
此函数以平均O(n)的时间复杂度求出区间中的第n小的数,同sort一样,函数最后也能加上重载的cmp函数。
本题用了nth_element函数以后还要注意的就是将询问排序后逆序处理,这样每一次的结尾就不用是arr + n了,而是上一次处理到的位置就好了。
代码:
#include<bits/stdc++.h>#define ll long long#define pb push_back#define fi first#define se second#define pi acos(-1)#define inf 0x3f3f3f3f#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define rep(i,x,n) for(int i=x;i<n;i++)#define per(i,n,x) for(int i=n;i>=x;i--)using namespace std;typedef pair<int,int>P;const int MAXN=100010;int gcd(int a,int b){return b?gcd(b,a%b):a;}unsigned x, y, z;unsigned a[10000010];P p[110];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;}unsigned ans[110];int main(){ int n, m, kase = 1; while(~scanf("%d %d %u %u %u", &n, &m, &x, &y, &z)) { for(int i = 0; i < n; i++) a[i] = rng61(); for(int i = 0; i < m; i++) scanf("%d", &p[i].fi), p[i].se = i; sort(p, p + m); int last = n; for(int i = m - 1; i >= 0; i--) { if(p[i].fi == p[i + 1].fi) ans[p[i].se] = ans[p[i + 1].se]; else nth_element(a, a + p[i].fi, a + last); ans[p[i].se] = a[p[i].fi]; last = p[i].fi; } printf("Case #%d:", kase++); for(int i = 0; i < m; i++) printf(" %u", ans[i]); puts(""); } return 0;}
阅读全文
0 0
- HDU 6040 Hints of sd0061 STL
- hdu 6040 Hints of sd0061(STL)
- hdu 6040 Hints of sd0061(STL)
- HDU 6040 Hints of sd0061 stl
- 【HDU 6040 Hints of sd0061】 思维 & STL
- 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
- hdu 6040 Hints of sd0061【STL线性排序函数】
- HDU 6040 Hints of sd0061(STL nth_element函数)
- 【多校训练】hdu 6040 Hints of sd0061 STL
- Hints of sd0061 HDU
- HDU 6040 Hints of sd0061 (技巧)
- hdu 6040 Hints of sd0061(nth_element)
- HDU 6040 Hints of sd0061(nth_element)
- mysql安装
- SAP MM批次管理(7)分割评估
- python小白入门教程——TypeScript的语言特性
- githubPages+jekyll制作博客教程
- CodeForces
- HDU 6040 Hints of sd0061 STL
- nginx 配置项 root与alias区别
- BeanFactory和ApplicationContext的区别
- Spring XMLDI
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
- SharedPreferences的commit和apply
- WERTYU
- 多个panel控件重叠
- Qt Quick核心编程从入门到精通----系列教程