2022-05-19每日刷题打卡
代码源——每日一题
贪就是了 - 题目 - Daimayuan Online Judge
题目描述
给你一个序列 aa,我们定义
S(l,r)=∑i=li=rai
, 显然易见我们会有n∗(n+1)/2个不同的Sl,r,请你输出其中前 k大的Sl,r
输入描述
第一行输入两个整数 n,k 第二行输入 n个整数代表序列 a
输出描述
输出一行包含 k个整数代表答案
样例输入1
6 8
1 1 4 5 1 4
样例输出1
16 15 14 12 11 11 10 10
数据范围
0≤ai≤10e9,1≤n≤10e5,1≤k≤min(10e5,n∗(n+1)/2)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#define endl '\n'
#define int ll
typedef long long ll;
typedef pair<ll, ll>PII;
const int N = 1e6 + 50;
int n, k;
int a[N];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
priority_queue<pair<ll, int>> que;
ll sum = 0;
cin >> n >> k;
for (int i = 1; i <= n; ++i)
{
cin>>a[i];
sum += a[i];
que.push({ sum,1 });
}
for (int i = 1; i <= k; ++i)
{
ll res = que.top().first;
int j = que.top().second;
cout << res << " ";
que.pop();
if (res - a[j] <= 0) continue;
que.push({ res - a[j],j + 1 });
}
return 0;
}