[TC14088]LimitedMemorySeries1

题目大意:

给定长度为\(n(n\le5\times10^6)\)的数组\(X\),询问不超过\(q(q\le100)\)次,每次询问第\(k_i\)大的数是多少。

数组\(X\)生成方式如下:

X[0] = x0
for i = 1 to n-1:
X[i] = (X[i-1] * a + b) % (10^9+7)

内存大小1MB,时间1S。

思路:

内存这么紧肯定不能开一个长度为\(n\)的数组,因此按照值域分块。先对每一个块开桶,统计每一块有多少数,确定答案在哪一块中。然后对于块中的每一个数统计个数。

时间复杂度\(\mathcal O(nq)\)。

源代码:

#include<vector>
#include<cstring>
class LimitedMemorySeries1 {
private:
using int64=long long;
static constexpr int B=31623,mod=1e9+7;
int cnt[B];
public:
int64 getSum(const int &n,const int &x0,const int &a,const int &b,std::vector<int> query) {
int64 ans=0;
for(auto k:query) {
memset(cnt,0,sizeof cnt);
for(register int i=0,x=x0;i<n;i++) {
cnt[x/B]++;
x=((int64)x*a%mod+b)%mod;
}
int p=0,q=0;
for(;p<B;p++) {
if(k-cnt[p]<=-1) break;
k-=cnt[p];
}
memset(cnt,0,sizeof cnt);
for(register int i=0,x=x0;i<n;i++) {
if(x/B==p) cnt[x%B]++;
x=((int64)x*a%mod+b)%mod;
}
for(;q<B;q++) {
if(k-cnt[q]<=-1) break;
k-=cnt[q];
}
ans+=(int64)p*B+q;
}
return ans;
}
};

[TC14088]LimitedMemorySeries1的更多相关文章

  1. TopCoder SRM 675 Div1 Problem 500 LimitedMemorySeries1(分块)

    题意  给定一个长度不超过$5*10^{6}$的数列和不超过$100$个询问,每次询问这个数列第$k$小的数,返回所有询问的和 内存限制很小,小到不能存下这个数列.(数列以种子的形式给出) 时限$10 ...

  2. NodeJs和ReactJs单元测试工具——Jest

    Jest——Painless JavaScript UnitTesting 特点 适应性强 默认使用Jasmine断言 模块化的 可扩展的 可配置的 沙箱式且快速 虚拟化JS环境,模拟浏览器 并行运行 ...

随机推荐

  1. bzoj千题计划290:bzoj3143: [Hnoi2013]游走

    http://www.lydsy.com/JudgeOnline/problem.php?id=3143 计算每条边经过的概率e[] 然后经过概率多的分配的编号大,经过概率少的分配的编号小 如何计算边 ...

  2. MySQL索引背后的数据结构及算法原理 (转)

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

  3. python爬虫-图片批量下载

    # 爬起摄图网的图片批量下载# coding:utf-8 import requests from bs4 import BeautifulSoup from scipy.misc import im ...

  4. [Alg::DP] 袋鼠过河

    一道简单的动态规划问题. 题目来源:牛客网 链接:https://www.nowcoder.com/questionTerminal/74acf832651e45bd9e059c59bc6e1cbf ...

  5. Plus One & Plus One Linked List

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  6. 巧用PHP数组函数

    2014年3月5日 08:48:39 情景:项目中需要根据传递来的参数的不同,使用不同的缓存 假如传递来的参数最多有这几个(在这个范围内,但是每次传过来的参数不确定): $arg = array( ' ...

  7. css中width和padding-top实现正方形

    .div{ width: 100%; height: 0; padding-top: 100% } 这个时候,padding-top的值与width相等,所以可以让div宽高一样,值为width的值

  8. mysql索引 B+tree

    一.B+tree示意图 二.为什么要用索引 1.索引能极大减少存储引擎需要扫描的数据量:因为索引有序所以可以快速查找并且不用全表查找: 2.索引可以把随机IO变为顺序IO:因为B+tree在数据中保存 ...

  9. SqlServer查看表、存储过程、耗时查询、当前进程、开销较大的语句

    --查看数据库中表的语句 SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( SELECT TOP 1 SUBSTRIN ...

  10. Using MongoDB in C#

    In order to use MongoDB in C#, you can import MongoDB C# Driver to your project. It's best to create ...