1. 程式人生 > >P2085 最小函數值 堆

P2085 最小函數值 堆

put queue 其中 sizeof 空格 view open int nod

  

題目描述

有n個函數,分別為F1,F2,...,Fn。定義Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*)。給定這些Ai、Bi和Ci,請求出所有函數的所有函數值中最小的m個(如有重復的要輸出多個)。

輸入輸出格式

輸入格式:

輸入數據:第一行輸入兩個正整數n和m。以下n行每行三個正整數,其中第i行的三個數分別位Ai、Bi和Ci。Ai<=10,Bi<=100,Ci<=10 000。

輸出格式:

輸出數據:輸出將這n個函數所有可以生成的函數值排序後的前m個元素。這m個數應該輸出到一行,用空格隔開。

輸入輸出樣例

輸入樣例#1: 復制
3 10
4 5 3
3 4 5
1 7 1
輸出樣例#1: 復制
9 12 12 19 25 29 31 44 45 54

說明

數據規模:n,m<=10000

和上一題序列合並類似 用了類貪心思想 很重要!

技術分享圖片
#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define
RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define REP(i,N) for(int i=0;i<(N);i++) #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f const int N=100000+5; int
a[N]; int b[N]; int heap[N]; int from[N]; int now[N]; struct node { int a,b,c; int now; int v; bool operator< (const node & b)const { return v>b.v; } }s[N]; int main() { priority_queue<node>q; int n,m; RII(n,m); rep(i,1,n) RIII(s[i].a,s[i].b,s[i].c),s[i].now=0; rep(i,1,n) { s[i].now++; int x=s[i].now; s[i].v=s[i].a*x*x+s[i].b*x+s[i].c; q.push(s[i]); } int cnt=1; node head; int last; while(cnt<=m) { head=q.top();q.pop(); printf("%d ",head.v); int x=++head.now; head.v=( head.a*x*x+head.b*x+head.c); q.push(head); cnt++; } return 0; }
View Code

P2085 最小函數值 堆