- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定一组数字,每个数字代表一个问题的难度。排队的人应该选择任意两个问题来解决。两个选择的问题应该是不同的,这对问题不应该被任何人以前选择。因为他们知道困难,所以他们会选择困难总和最小的那对。
找出站在队列中第 k 个位置的人的最小难度总和。即来自数组的唯一对的第 k 个最小总和。
方法1:蛮力方法(O(n2))计算所有可能的唯一和并将其存储在数组中,并对唯一和数组进行排序以获得第k个元素。
方法 2:对困难数组进行排序并选择最小元素(对于前 4 个元素,我们可以有 6 个唯一对。因此,如果 k 小于或等于 6,我们可以使用排序数组中的前 4 个元素来找到最小总和)并用最小数组做了方法 1。
这 2 种方法没有解决超时情况。需要提高时间效率的解决方案。
注意:不同的问题也可以具有相同的难度级别(即数组可以包含重复的数字),并且默认情况下不按排序顺序。
difficulties = [1,4,3,2,4]
Person comes first chooses: 1+2 = 3
2nd person: 1+3 = 4
3rd person: 1+4 (or) 1+4(since difficulty of two problems are 4) (or) 2+3 = 5
4th person: 2+3 (or) 1+4(based on the previous selection) = 5
所需的最终答案只是最小总和而不是实际元素。
最佳答案
假设 k <= n*(n-1)/2
.如果没有,那么就不可能有答案。
我们可以使用二分查找来解决这个问题。我们对可能的对的总和进行二分搜索。
这里,低 = 可能的最小总和,即 low = difficulties[0] + difficulties[1]
, 高 = 可能的最大总和,即 high = difficulties[n-1] + difficulties[n-2]
.
所以,mid = low + (high - low)/2
现在,在二分搜索的 1 次迭代中,我们将计算索引对 (i, j), i < j
使得 difficulties[i] + difficulties[j] <= mid
.如果计数小于 k,则 low = mid + 1
否则如果计数 >= k,high = mid
.现在,这一次迭代可以在 O(NlogN)
中完成。 .
你可以这样做直到(high - low) > 1
.因此,每次您将搜索空间减少一半。因此,总时间复杂度为 O(N*logN*logMaxsum)
对于 N <= 1e6
和 difficulties[i] <= 1e18
将在不到 1 秒内运行。
现在高可以等于低或高可以等于低+1。答案可以等于low
或 high
.现在,您只需要解决问题是否为可能的总和(可以在 O(N)
中使用哈希轻松解决),而否。指数对 (i, j), i < j
使得 difficulties[i] + difficulties[j] <= low
.如果这两个条件都满足,那么这就是您的答案。如果不是,那么答案就是高。
运行示例测试用例:
让我们考虑初始数组,difficulties = [1, 4, 3, 2, 4]
和 k = 6
.
您首先对消耗我们的数组进行排序 O(NlogN)
.排序后difficulties = [1, 2, 3, 4, 4]
所有对n*(n-1)/2 = 10
将是:
(1 + 2) => 3
(1 + 3) => 4
(1 + 4) => 5
(1 + 4) => 5
(2 + 3) => 5
(2 + 4) => 6
(2 + 4) => 6
(3 + 4) => 7
(3 + 4) => 7
(4 + 4) => 8
sort(difficulties)
low = difficulties[0] + difficulties[1] // Minimum possible sum
high = difficulties[n-1] + difficulties[n-2] // Maximum possible sum
while(high - low > 1){
mid = low + (high - low)/2
count = all pairs (i, j) and i < j such that difficulties[i] + difficulties[j] <= mid.
if(count < k){
low = mid +1
}else{
high = mid
}
}
Iteration 1:
low = 3
high = 8
mid = 5
count = 5 [(1 + 2), (1 + 3), (1 + 4), (1 + 4), (2 + 3)]
count < k, so low = mid + 1 = 6
----------
Iteration 2:
low = 6
high = 8
mid = 7
count = 9 [(1 + 2), (1 + 3), (1 + 4), (1 + 4), (2 + 3), (2 + 4), (2 + 4), (3 + 4), (3 + 4)]
count >= k, so high= mid = 7
现在,while 循环停止,因为 high(7) - low(6) = 1。
i
那么你只需要找到
mid - difficulties[i]
的上限在数组中
[i+1, n-1]
.然后将 i 增加 1 并重复相同的操作。所以,你遍历每个索引
0 <= i <= n-1
并在
[i+1, n-1]
的数组搜索空间中找到它的上界并且每次迭代都需要
O(NlogN)
.
difficulties = [10, 40, 30, 20, 40]
的算法。 .
O(N*logN*logMaxsum)
包括用于清楚理解逻辑的注释。
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
void solve();
int main(){
solve();
return 0;
}
map<int, int> m;
vector<ll> difficulties;
ll countFunction(ll sum){
/*
Function to count all the pairs of indices (i, j) such that
i < j and (difficulties[i] + difficulties[j]) <= sum
*/
ll count = 0;
int n = (int)difficulties.size();
for(int i=0;i<n-1;i++){
/*
Here the outer for loop means that if I choose difficulties[i]
as the first element of the pair, then the remaining sum is
m - difficulties[i], so we just need to find the upper_bound of this value
to find the count of all pairs with sum <= m.
upper_bound is an in-built function in C++ STL.
*/
int x= upper_bound(difficulties.begin(), difficulties.end(), sum-difficulties[i]) - (difficulties.begin() + i + 1);
if(x<=0){
/*
We break here because the condition of i < j is violated
and it will be violated for remaining values of i as well.
*/
break;
}
//cout<<"x = "<<x<<endl;
count += x;
}
return count;
}
bool isPossible(ll sum){
/*
Hashing based solution to check if atleast 1 pair with
a particular exists in the difficultiesay.
*/
int n = (int) difficulties.size();
for(int i=0;i<n;i++){
/*
Choosing the ith element as first element of pair
and checking if there exists an element with value = sum - difficulties[i]
*/
if(difficulties[i] == (sum - difficulties[i])){
// If the elements are equal then the frequency must be > 1
if(m[difficulties[i]] > 1){
return true;
}
}else{
if(m[sum - difficulties[i]] > 0){
return true;
}
}
}
return false;
}
void solve(){
ll i, j, n, k;
cin>>n>>k;
difficulties.resize(n);
m.clear(); // to run multiple test-cases
for(i=0;i<n;i++){
cin>>difficulties[i];
m[difficulties[i]]++;
}
sort(difficulties.begin(), difficulties.end());
// Using binary search on the possible values of sum.
ll low = difficulties[0] + difficulties[1]; // Lowest possible sum after sorting
ll high = difficulties[n-1] + difficulties[n-2]; // Highest possible sum after sorting
while((high-low)>1){
ll mid = low + (high - low)/2;
ll count = countFunction(mid);
//cout<<"Low = "<<low<<" high = "<<high<<" mid = "<<mid<<" count = "<<count<<endl;
if (k > count){
low = mid + 1;
}else{
high = mid;
}
}
/*
Now the answer can be low or high and we need to check
if low or high is a possible sum and does it satisfy the constraints of k.
For low to be the answer, we need to count the number of pairs with sum <=low.
If this count is >=k, then low is the answer.
But we also need to check whether low is a feasible sum.
*/
if(isPossible(low) && countFunction(low)>=k){
cout<<low<<endl;
}else{
cout<<high<<endl;
}
}
关于arrays - 查找数组中唯一对的第 k 个最低和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66650631/
This question already has answers here: Closed 11 years ago. Duplicate: Recommended website resoluti
我有一个网络应用程序需要 IE9。其他浏览器(Firefox、Chrome、Opera 等)的等效浏览器版本是什么? 我知道如何检查用户当前的浏览器/版本,如果不支持,我需要看看是否可以为用户提供下载
我在比较 Mysql 数据库中的两个值并显示最低值时遇到问题。 比如我有这个: value1 = 23.4 value2 = 4.479 我试过这个: ORDER BY CAST(column AS
我有一个需要使用的功能,但我需要将我的最小 SDK 设置为 23 才能使用它。问题是,我们的应用程序运行在很多低端 SDK 设备上。有什么方法可以设置我的项目以允许我在编译应用程序的同时仍然使用较低的
我需要在32位数字中获得一个1位数字,其中只有一个1位(总是)。用C ++或asm最快的方法。 例如 input: 0x00000001, 0x10000000 output:
我已经对我的数据进行了分组。现在,我要做的是每周从“高”列中选择最高值,并从“低”列中选择最低值,然后使用最高值减去最低值得到范围。但是代码总是错误的。有人对我有想法吗? 这是我的 DataFrame
如何编写一个存储 20 个数字的数组,然后显示以下数据:数组中最小的数字、最大的数字、数字的总和以及它们的平均值? 最佳答案 使用java编程语言 int myArray[] = {15,25,85,
在学习 C 的同时做一些实验,我遇到了一些奇怪的事情。这是我的程序: int main(void) {sleep(5);} 当它被编译时,可执行文件的文件大小为 8496 字节(与 26 字节的源代码
我已经创建了我想在我的项目中使用的包。在包中,我使用的是 UIStackView。当我将包添加到项目并运行它时,我收到错误 'UIStackView' 仅适用于 iOS 9.0 或更高版本。如果我的项
我已经制定了一个程序来显示给定日期的特定时间的最高和最低流行项目。该过程没有错误或异常,并且一切正常。如您所见,为了显示 Items 的第一条记录,查询重复了两次,但唯一的区别在于顺序(ASC 和 D
我是 BPEL/BPMN 新手。 是否可以在没有 WS-* Web 服务和 EJB 容器(例如 jBoss、WebLogic、Glassfish)的情况下编写 BPEL/BPMN 感知软件? 我想知道
我们正在使用 OpenGL 4.3。但是,我们担心我们使用的功能适用于我们的显卡,但不符合 OpenGL 4.3 的“最低”要求规范。 是否有可能模拟最低限度的行为?例如,让显卡拒绝任何非标准纹理格式
我正在我的应用程序中实现 Facebook SDK。 按照 facebook 开发人员指南,除了我没有设置 GIT,所以我下载了 SDK,并将其导入 Eclipse,将 Java 合规级别更改为 1.
所以我构建了一个使用 API 15 中特定默认配色方案的应用程序。但是,我知道大多数设备仍在运行 API 10。为了吸引这些设备,我在我的设备上切换了最小 SDK设备到 10。这样做之后,我注意到我的
数据 所以,假设我有一个应用程序,我正在测试汽车的速度、性能、安全性等。我有一组以下格式的数据: CAR TABLE ID CAR_NAME 1 Ford Focus 2006 2 To
我有以下查询: SELECT AVG(q1) AS q1, AVG (q2) AS q2, AVG(q3) AS q3, AVG(q4) AS q4, AVG(q5) AS q5 FROM tresu
我刚刚创建了我的第一个 Android 应用程序(第 10 次)。我创建的项目的 sdk 比我预期的要高,现在我想降低它。我最初的 android maifest 不包含 部分,所以我补充说: 我还
我的小组作业是制作一个程序,允许用户输入任意数量的数字,然后程序会告诉你输入的最高数字、输入的最低数字、平均值、输入的总数和平均值。我们必须使用菜单。 我们已经写好了菜单。我们的大部分计算代码都在案例
我正在尝试设置一个显示文档目录中数据的 UITableView。 我对代码有点迷茫,因为我尝试了来自 Google 和论坛等的许多示例。 我正在创建没有 Storyboard 的应用程序,所以它全部在
我们都知道将最后 1 位设置为 0 的技巧 n&(n-1)。例如,0110 & (0110-1) = 0100。但是反过来呢?将最后一个 0 设置为 1?所以 0110 变成 0111? 我在 sta
我是一名优秀的程序员,十分优秀!