1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> #include <algorithm> #include <vector> using namespace std; bool f0(vector<int> &a,int m, int k){ int s=0; for(int i=0,j=0;i<a.size();i++){ while(a[i]-a[j]>m) j++; s+=i-j; } return s>=k; } int f(vector<int> &a,int k){ sort(a.begin(),a.end()); int g=0; int h=a.back()-a[0]; while(g<h){ int m=g+(h-g)/2; if(f0(a,m,k)){ h=m; } else{ g=m+1; } } return g; } int main(){ int n,k; cin>>n>>k; vector<int> a(n,0); for(int i=0;i<n;i++){ cin>>a[i]; } cout<<f(a,k)<<endl; return 0; } |
假设输入总是合法的且 1≤𝑎𝑖≤108,𝑛<=10000,1≤𝑘≤𝑛(𝑛−1)/2,完成下面的判断题和单选题:
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
将第 24 行的 m
改为 m - 1
,输出有可能不变,而剩下情况为少 1。()
2、将第 22 行的 g + (h - g) / 2
改为 (h + g) >> 1
,输出不变。( )
3、当输入为 5 7 2 -4 5 1 -3
,输出为 5
。()
设 𝑎 数组中最大值减最小值加 1为 A,则 f
函数的时间复杂度为()。
5、将第 10 行中的 >
替换为 >=
,那么原输出与现输出的大小关系为( )。
6、当输入为 5 8 2 -5 3 8 -12
,输出为()。