P2085 最小函数值 堆
生活随笔
收集整理的這篇文章主要介紹了
P2085 最小函数值 堆
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述
有n個(gè)函數(shù),分別為F1,F2,...,Fn。定義Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*)。給定這些Ai、Bi和Ci,請(qǐng)求出所有函數(shù)的所有函數(shù)值中最小的m個(gè)(如有重復(fù)的要輸出多個(gè))。
輸入輸出格式
輸入格式:
?
輸入數(shù)據(jù):第一行輸入兩個(gè)正整數(shù)n和m。以下n行每行三個(gè)正整數(shù),其中第i行的三個(gè)數(shù)分別位Ai、Bi和Ci。Ai<=10,Bi<=100,Ci<=10 000。
?
輸出格式:
?
輸出數(shù)據(jù):輸出將這n個(gè)函數(shù)所有可以生成的函數(shù)值排序后的前m個(gè)元素。這m個(gè)數(shù)應(yīng)該輸出到一行,用空格隔開。
?
輸入輸出樣例
輸入樣例#1:?復(fù)制 3 10 4 5 3 3 4 5 1 7 1 輸出樣例#1:?復(fù)制 9 12 12 19 25 29 31 44 45 54說明
數(shù)據(jù)規(guī)模: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?
轉(zhuǎn)載于:https://www.cnblogs.com/bxd123/p/10804445.html
總結(jié)
以上是生活随笔為你收集整理的P2085 最小函数值 堆的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看Unity工程版本
- 下一篇: 什么是跨域,为什么浏览器会禁止跨域,以及