题目:
输入格式
第一行包含一个整数 n,k。
接下来一行,包括 n 个整数,第 i 个数是 v[i]。
输出格式
输出共 k 行,每行包括一个整数,第 i 行的数表示第 i 大的收益。
样例数据 1
备注
【数据范围】
对于 10% 的数据,1≤n≤200。
对于 30% 的数据,1≤n≤2000。
对于 50% 的数据,1≤n≤100000,1≤k≤1000。
对于 100% 的数据,1≤n≤100000,1≤k≤n,-1000≤v[i]≤1000。
分析:
抓住性质收益为绝对值,这就好写了
随便求出前缀和,升序,降序sort,max-min一定是最优(因为是绝对值,顺序不重要)
然后用堆(queue) 采用先多加后少加 来去重添加入堆即可
#include<string>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<vector>
using namespace std;
int hemin[100010];
int hemax[100010];
int a[100010];
int n,m;
bool comp(const int &a,const int &b)
{
return a>b;
}
struct node
{
int a,b,c;
bool operator < (const node &t)const
{
return c<t.c;
}
};
priority_queue <node> q;
int main()
{
//freopen("zhugeliang.in","r",stdin);
// freopen("zhugeliang.out","w",stdout);
int i,j,k;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
hemin[i]=hemin[i-1]+a[i];
hemax[i]=hemin[i];
}
sort(hemin,hemin+1+n);
sort(hemax,hemax+1+n,comp);
j=0;
k=0; //max
node hehe,mn;
for(i=0;i<=n;i++)
{
int dd=hemax[i]-hemin[0];
hehe.c=dd;
hehe.a=i;
hehe.b=0;
q.push(hehe);
}
int aa,bb;
for(i=1;i<=m;i++)
{
mn=q.top();
q.pop();
aa=mn.a;
bb=mn.b;
printf("%d\n",mn.c);
int dd=hemax[mn.a]-hemin[mn.b+1];
hehe.c=dd;
hehe.a=mn.a;
hehe.b=mn.b+1;
q.push(hehe);
}
return 0;
}