P2085 min function value (minval), p2085minval
Description
There are n functions, namely F1, F2,..., Fn. Define Fi (x) = AiX ^ 2 + BiX + Ci (x ε N *). Given these Ai, Bi, and Ci, the minimum m of all function values of all functions are requested (if there are duplicates, multiple functions need to be output ).
Input/Output Format
Input Format:
Input data: enter two positive integers n and m in the first row. The following n rows have three positive integers in each row, where the three numbers in row I are Ai, Bi, and Ci respectively. Ai <= 10, Bi <= 100, Ci <= 10 000.
Output Format:
Output Data: output the first m elements after sorting all the function values generated by the n functions. The m number should be output to a row separated by spaces.
Input and Output sample
Input example #1:
3 104 5 33 4 51 7 1
Output sample #1:
9 12 12 19 25 29 31 44 45 54
Description
Data scale: n, m <= 10000
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<queue> 5 using namespace std; 6 const int N = 1000000 + 5; 7 int a[N],b[N],c[N],p[N],n,m; 8 struct node 9 {10 int a,b;11 }f[N];12 int function(int i,int x)13 {14 return a[i]*x*x+b[i]*x+c[i];15 }16 void update(int i)17 {18 if(f[i].a>f[i*2].a && i*2<=n)19 {20 swap(f[i],f[i*2]);21 update(i*2);update(i/2);22 }23 if(f[i].a>f[i*2+1].a && i*2+1<=n)24 {25 swap(f[i],f[i*2+1]);26 update(i*2+1);update(i/2);27 }28 }29 int main()30 {31 scanf("%d%d",&n,&m);32 for(int i=1;i<=n;i++)33 {34 p[i]=1;35 scanf("%d%d%d",&a[i],&b[i],&c[i]);36 f[i].a=function(i,p[i]++);f[i].b=i;37 }38 for(int i=1;i<=n;i++)update(i);39 for(int i=0;i<m;i++)40 {41 printf("%d ",f[1].a);42 f[1].a=function(f[1].b,p[f[1].b]++);43 update(1);44 }45 return 0;46 }