hdu6040

题意

将一个函数运行 n 次,一共得到 n 个值,有 m 次询问,每次询问第 k 小的值。

分析

考察了 \(nth\_element\) 函数的运用。\(nth\_element(a, a + x, a + n)\) 使得 ( \(a\) 数组下标范围 \([0, n)\) ) \(a[x]\) 存的是第 \(x\) 小的数,且保证 \(x\) 左边的数小于等于它,右边的数大于等于它( 当 \(x = 0\) 时, \(a[0]\) 是最小的值 )。

将 \(k\) 排序后从大到小,求值,并更新 \(n\) 的值,因为右边的数一定大于等于左边的数,剩下第 k 小的取值一定能在左边取到。

code

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const int MAXN = 1e7 + 10;
int kase = 1;
int n, m;
unsigned A, B, C;
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;
}
unsigned a[MAXN];
struct B {
int x, i;
bool operator<(const B&other) const {
return x > other.x;
}
}b[105];
unsigned ans[105];
int main() {
while(~scanf("%d%d%u%u%u", &n, &m, &A, &B, &C)) {
x = A; y = B; z = C;
for(int i = 0; i < n; i++) {
a[i] = rng61();
}
for(int i = 0; i < m; i++) {
scanf("%d", &b[i].x);
b[i].i = i;
}
sort(b, b + m);
int len = n;
for(int j = 0; j < m; j++) {
if(j && b[j].x == b[j - 1].x) {
ans[b[j].i] = ans[b[j - 1].i];
continue;
}
nth_element(a, a + b[j].x, a + len);
ans[b[j].i] = a[b[j].x];
len = b[j].x;
}
printf("Case #%d:", kase++);
for(int i = 0; i < m; i++) {
printf(" %u", ans[i]);
}
printf("\n");
}
return 0;
}

hdu6040的更多相关文章

  1. HDU6040 Hints of sd0061

    题目链接:https://vjudge.net/problem/HDU-6040 题目大意: 给出 \(n\) 个数,有 \(m\) 次询问,每次询问这 \(n\) 个数中第 \(k+1\) 大的数是 ...

  2. 2017 Multi-University Training Contest - Team 1—HDU6040

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6040 题意:不知道北航的同学为何解释题意之前都要想一段故事,导致刚开始题意不是很懂,题意就是给你n,m ...

  3. 随机生成数组函数+nth-element函数

    这几天做了几道随机生成数组的题,且需要用nth-elemeng函数,并且都是北航出的多校题…… 首先我们先贴一下随机生成数组函数的代码: unsigned x = A, y = B, z = C; u ...

  4. O(n)线性时间求解第k大-HDU6040-CSU2078

    目录 目录 思路 (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 HDU6040:传送门  \(m(m\leq 100)\)次查询长度为\(n(n \leq 1e7)\)区间的 ...

随机推荐

  1. USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】

    nocows解题报告------------------------------------------------------------------------------------------ ...

  2. 【Java数据结构学习笔记之三】Java数据结构与算法之队列(Queue)实现

      本篇是数据结构与算法的第三篇,本篇我们将来了解一下知识点: 队列的抽象数据类型 顺序队列的设计与实现 链式队列的设计与实现 队列应用的简单举例 优先队列的设置与实现双链表实现 队列的抽象数据类型 ...

  3. 1043 Is It a Binary Search Tree (25 分)(二叉查找树)

    #include<bits/stdc++.h> using namespace std; typedef struct node; typedef node *tree; struct n ...

  4. chrome浏览器console拓展用法

    chrome 浏览器console打印 使用CSS美化输出信息 console.log("%cThis will be formatted with large, blue text&quo ...

  5. free、vmstat监视内存使用情况

    9. free 查询可用内存 free工具用来查看系统可用内存: /opt/app/tdev1$free total used free shared buffers cached Mem: 8175 ...

  6. HTTP3.0(QUIC的实现机制)

    回顾HTTP2.0 HTTP1.1在应用层以纯文本的形式进行通信,每次通信都要带完整的HTTP的头,而且不考虑pipeli模式的化,每次的过程总是像上面描述的那样一去一回.那样在实时性.并发想上都存在 ...

  7. Spring框架jar包分类(转)

    转自:http://www.cnblogs.com/JSONBEAN/p/6364038.html 长期以来都在写SSM框架的项目,却未能深入理解框架的搭建原理,而只是浅薄的理解前辈的架构,然后不断套 ...

  8. poj 2406 Power Strings (后缀数组 || KMP)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28859   Accepted: 12045 D ...

  9. cdoj 1259 线段树+bitset 区间更新/查询

    Description 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天 ...

  10. Python学习笔记(Django篇)——1、环境搭建篇(如何在Pycharm中配置Python和Django)

      1.准备好以下东东,并且按照先后顺序进行安装: Python 3.6 (64-bit) Django-1.11.tar.gz pycharm-community-2016.3.2.exe 安装好了 ...