有没有实力?
2024-06-04 21:30:40
发布于:广东
23阅读
0回复
0点赞
#include<iostream>
#include<algorithm>
using namespace std;
long long a[9000010];
bool cmp(int a,int b){
return a>b;
}
int main(){
long long t,n,m,b[3010];
cin>>t;
while(t--){
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>b[i];
long long len = 0;
sort(b,b+n,cmp);
if(m>=n0.5(n-1))n=n;
else n=min(n,m)/2+7;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
a[len++]=b[i]+b[j];
}
}
sort(a,a+len,cmp);
for(int i=0;i<min(len,m);i++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}
全部评论 1
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>using namespace std;
int main() {
int T;
cin >> T; // Read number of test cases
while (T--) {
int N, M;
cin >> N >> M; // Read N and M
vector<int> nums(N);
for (int i = 0; i < N; ++i) {
cin >> nums[i]; // Read N numbers
}// Min-heap to store the largest M sums priority_queue<int, vector<int>, greater<int>> minHeap; // Calculate all unique sums and maintain the top M sums for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { int sum = nums[i] + nums[j]; minHeap.push(sum); if (minHeap.size() > M) { minHeap.pop(); // Keep only the largest M sums } } } // Extract sums from the min-heap vector<int> largestSums; while (!minHeap.empty()) { largestSums.push_back(minHeap.top()); minHeap.pop(); } // Sort in descending order sort(largestSums.rbegin(), largestSums.rend()); // Print the result for (int i = 0; i < largestSums.size(); ++i) { cout << largestSums[i]; if (i < largestSums.size() - 1) cout << " "; // Space between numbers } cout << endl; } return 0;
}
2024-08-27 来自 山东
0
有帮助,赞一个