洛谷 P2085 最小函数值

题目

思路

首先这些函数全部单带递增,因为(a)(b)(c)都是正整数。
我们将全部的函数的(x)(1)时的函数值放入优先度列(小根堆),然后输出并弹出堆顶元素,将该函数的(x++)再将函数值放入优先队列。

(Code)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#define MAXN 10001
using namespace std;
int n,m;
int a[MAXN],b[MAXN],c[MAXN];
struct info{
	int num,now,w;
	bool operator > (const info xx) const{
		return w>xx.w;
	}
}fff[MAXN];
priority_queue<info,vector<info>,greater<info> > q;
inline int read(){
	int x=0;bool f=0;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=!f;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return f?-x:x;
}

int main(){
	n=read(),m=read();
	for(int i=1;i<=n;++i){
		a[i]=read(),b[i]=read(),c[i]=read();
	}
	for(int i=1;i<=n;++i){
		fff[i].now=1,fff[i].num=i;
		fff[i].w=a[i]+b[i]+c[i];
		q.push(fff[i]);
	}
	while(m--){
		info temp=q.top();
		printf("%d ",temp.w);
		q.pop();
		temp.now++;
		temp.w=(a[temp.num]*temp.now*temp.now)+(b[temp.num]*temp.now)+c[temp.num];
		q.push(temp);
	}
	puts("");
	return 0;
}
原文地址:https://www.cnblogs.com/poi-bolg-poi/p/11289097.html