1370:最小函数值(minval)

http://ybt.ssoier.cn:8088/problem_show.php?pid=1370

一:炸堆操作

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 priority_queue <int ,vector<int> ,greater<int> > small_h;
 5 int n, m;
 6 void f(int a, int b, int c)
 7 {
 8     for(int x=1; x<=m; x++)
 9         small_h.push(a*x*x+b*x+c);
10 }
11 int main()
12 {
13     cin>>n>>m;
14     for(int i=0; i<n; i++){
15         int a, b, c;
16         cin>>a>>b>>c;
17         f(a, b, c);
18     }
19     
20     for(int i=1; i<=m; i++){
21         cout<<small_h.top()<<" ";
22         small_h.pop();
23     }
24     return 0;
25  } 

二:正确操作

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 priority_queue <int> big_h;//建立最大堆 
 5 int n, m;
 6 int ans[10010];//用于存放答案 
 7 void f(int a, int b, int c)
 8 {
 9     for(int x=1; x<=m; x++){
10         int fx=a*x*x+b*x+c;//计算函数f 
11         if(big_h.size()<m)//如果堆大小少于答案 ,直接入堆 
12             big_h.push(fx);
13         else{
14             if(fx<big_h.top()){//如果fx小于堆顶,堆顶出堆,fx入堆 
15                 big_h.pop();
16                 big_h.push(fx);
17             }
18         }
19     }    
20 }
21 int main()
22 {
23     cin>>n>>m;
24     for(int i=0; i<n; i++){
25         int a, b, c;
26         cin>>a>>b>>c;
27         f(a, b, c);
28     }
29     int cnt=0;
30     while(!big_h.empty()){//把堆存放到数组逆序输出即为答案 
31         ans[++cnt]=big_h.top();
32         big_h.pop();
33     }
34     for(int i=cnt; i>=1; i--)
35         cout<<ans[i]<<" ";
36     return 0;
37  } 
原文地址:https://www.cnblogs.com/tflsnoi/p/14150443.html