有一个未排序的列表a和一个范围列表,例如ranges = [(10, 20), (30, 50), (15, 35) ...]。 a中的最大值是uint64_t。目标是计算每个范围的元素数量。正常的解决方案非常直观,只需计算范围内的元素并打印结果即可。但问题是来自在线法官。我厌倦了保密的解决方案,但对于每个解决方案,OJ都给出了超过时限的限制。
a的最大长度为10,000,000,ranges的最大长度为1,000,000。
python浪漫代码,测试列表a具有1000万个随机数,而ranges具有100万对范围:
import numpy as np
a = list(np.random.randint(low=1, high=0x7fffffffffffffff, size=10_000_000))
python循环结构。ranges = []
for _ in range(1_000_000):
x, y = np.random.randint(low=1, high=0x7fffffffffffffff, size=2)
"python"?ranges.append((x, y) if x < y else (y, x))
第一个解决方案是:
import bisect
python爬虫教程?a.sort()
low_d = {}
up_d = {}
python web、def count(r):
low, up = r
if low not in low_d:
python代码。l = bisect.bisect_left(a, low)
low_d[low] = l
else:
l = low_d[low]
if up not in up_d:
u = bisect.bisect_right(a, up, lo=l)
up_d[up] = u
else:
u = up_d[up]
return u - l
result = [*map(count, ranges)]
缺点很明显,当sort()很大时,a会非常耗时。
原始的第二个解决方案比上述解决方案要慢得多。
弃。
两种解决方案均导致TLE错误。我使用的OJ就像一个黑盒子,我不知道它用来测试程序的测试示例。
由于该程序在OJ上运行,因此不允许使用numpy。
有什么方法可以优化性能?
参考方案
此C ++代码在此硬件上以1.9s运行,而-O2与我最好的Python代码13.2s相比运行(与Python中的基准测试相比,这是一个较慢的硬件)。
可能的改进:
上等分应在下等分之上搜索
使用预先计算的等分值,例如python代码
从Unisort: an Algorithm to Sort Uniformly Distributed Numbers in O(n) Time. R.T. Ionescu 2013实现Unisort算法
码:
#include
#include
#include
#include
#include
#include
#include
#include
int tdiff(std::chrono::time_point<:chrono::system_clock> start, std::chrono::time_point<:chrono::system_clock> _end) {
int result;
result = (std::chrono::duration_cast<:chrono::milliseconds>(_end - start)).count();
return result;
}
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution dis(1, 0x7fffffffffffffff);
#define A_SIZE 10000000
#define R_SIZE 1000000
std::vector a(A_SIZE);
int a_size = A_SIZE;
int r_size = R_SIZE;
for (int i=0; i
a[i] = dis(gen);
}
std::vector<:vector>> ranges1(R_SIZE, std::vector(2));
int64_t x,y;
for (int i=0; i
x = dis(gen);
y = dis(gen);
if (x < y){
ranges1[i] = {x,y};
}else{
ranges1[i] = {y,x};
}
}
std::chrono::time_point<:chrono::system_clock> start, _end;
start = std::chrono::system_clock::now();
std::sort(a.begin(), a.end());
std::vector counts(A_SIZE);
std::vector::iterator l;
std::vector::iterator u;
for (int i=0; i
l = std::lower_bound(a.begin(),a.end(),ranges1[i][0]);
u = std::upper_bound(a.begin(),a.end(),ranges1[i][1]);
counts[i] = (int64_t)std::distance(l,u);
}
_end = std::chrono::system_clock::now();
std::cout << tdiff(start, _end) << "\n";
std::cout << counts[0] << "\n";
return 0;
}
Python Pandas导出数据 - python
我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…Python 3运算符>>打印到文件 - python
我有以下Python代码编写项目的依赖文件。它可以在Python 2.x上正常工作,但是在使用Python 3进行测试时会报告错误。depend = None if not nmake: depend = open(".depend", "a") dependmak = open(".depend.mak…Python:对于长时间运行的进程,通过还是休眠? - python
我正在编写一个队列处理应用程序,该应用程序使用线程等待和响应要发送到该应用程序的队列消息。对于应用程序的主要部分,只需要保持活动状态即可。对于像这样的代码示例:而True: 通过要么而True: time.sleep(1)哪一个对系统的影响最小?除了保持python应用运行外,什么都不做的首选方式是什么? 参考方案 我可以想象time.sleep()会减少系…如何使用BeautifulSoup在
中捕获特定的 - python尝试从nyc Wiki页面中的高中列表中获取所有高中名称。我已经写了足够多的脚本,可以让我获取包含在高中,学业和入学条件列表的表的
标记中的所有信息-但是我如何才能缩小到我认为的范围内在td[0]内休息(会弹出KeyError)-只是学校的名称?到目前为止我写的代码:from bs4 import BeautifulSoup from ur…Python NotImplemented常数 - python浏览decimal.py,它在许多特殊方法中使用NotImplemented。例如class A(object): def __lt__(self, a): return NotImplemented def __add__(self, a): return NotImplemented Python docs say: 未实现 可以通过“丰富比较”返回的特…
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态