Sort HDU - 5884(优先队列+二分)
Sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4673 Accepted Submission(s): 1177
Problem Description
Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
Alice will give Bob N sorted sequences, and the i-th sequence includes ai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than k sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than T cost. So Bob wants to know the smallest k to make the program complete in time.
Alice will give Bob N sorted sequences, and the i-th sequence includes ai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than k sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than T cost. So Bob wants to know the smallest k to make the program complete in time.
Input
The first line of input contains an integer t0, the number of test cases. t0 test cases follow.
For each test case, the first line consists two integers N (2≤N≤100000) and T (∑Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(∀i,0≤ai≤1000).
For each test case, the first line consists two integers N (2≤N≤100000) and T (∑Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(∀i,0≤ai≤1000).
Output
For each test cases, output the smallest k.
Sample Input
1
5 25
1 2 3 4 5
Sample Output
3
Source
题意:
给出n个数和一个花费c 每次合并不大于k个数 最后把这n个数合并到一起 每次合并所花的费用为k个数的和 总花费不超过c 求最大的k
解析:
先排序 然后二分答案变为判定性问题
每次合并k个最小的值
第一次如果(n-1)%(k-1) != 0 则先合并余数+1个数
为什么是(n-1)%(k-1) 呢 因为最后剩一个数 就相当于删除了n-1个数 每次合并k个数生成1个数 就相当于删除了k-1个数
为什么要余数+1呢 看图吧 。。。

不过我们这里加的1不是第n个数
是余数+1大的数 只是为了形象一点 能明白吧。。。emm。。
所以不算余数的那次计算 一共是进行了(n-1)/(k-1)次操作
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 100100, INF = 0x7fffffff; int n; int c; int sum[maxn], a[maxn]; bool solve(int k) { int res = 0; priority_queue<int, vector<int>, greater<int> > q; int tmp = (n-1)%(k-1); if(tmp > 0) { tmp++; res += sum[tmp]; q.push(sum[tmp]); } rap(i, tmp+1, n) q.push(a[i]); int t = (n-1)/(k-1); rap(i, 1, t) { int ans = 0; rep(j, 0, k) { ans += q.top(); q.pop(); } res += ans; q.push(ans); } if(res > c) return false; return true; } int main() { int T; rd(T); while(T--) { sum[0] = 0; rd(n); rd(c); rap(i, 1, n) { rd(a[i]); } sort(a+1, a+n+1); rap(i, 1, n) sum[i] = sum[i-1] + a[i]; int l = 2, r = n; while(l <= r) { int mid = l + (r - l) / 2; if(solve(mid)) r = mid - 1; else l = mid + 1; } if(n <= 1) l = 1; cout<< l <<endl; } return 0; }
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)