superoj738 诸葛亮

题目:

输入格式

第一行包含一个整数 n,k。
接下来一行,包括 n 个整数,第 i 个数是 v[i]。

输出格式

输出共 k 行,每行包括一个整数,第 i 行的数表示第 i 大的收益。

样例数据 1

输入  [复制]

5 2 
1 2 -1 -1 3

输出


3

备注

【数据范围】
对于 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值