1370: Minimum function value (minval)

topic

1370: Minimum function value (minval)

Time limit: 1000 ms Memory limit: 65536 KB
[Title description]
There are n functions, which are F 1 , F 2 , . . . , F n F_{1},F_{2},...,F_{n }F1,F2,...,Fn。定义 F i ( x ) = A i x 2 + B i x + C i ( x ∈ N ∗ ) F_{i}(x)=A_{i}x2+B_{i}x+C_{i}(x∈N∗) Fi(x)=Aix2+Bix+Ci(xN * ) . Given theseA i A_{i}Ai B i B_{i} Bi C i C_{i} Ci, request the smallest m of all function values ​​of all functions (if there are duplicates, output more than one).

[Input]
Input two positive integers n and m in the first line.

The following n lines each have three positive integers, where the three numbers in the i-th line are respectively A i A_{i}Ai B i B_{i} Bi C i C_{i} Ci. Input data guarantees A i <= 10 A_{i}<=10Ai<=10 B i < = 100 B_{i}<=100 Bi<=100 C i < = 10000 C_{i}<=10000 Ci<=10000

[Output]
The first m elements after sorting all the function values ​​that can be generated by these n functions. The m numbers should be output on one line, separated by spaces.

【Input example】
3 10
4 5 3
3 4 5
1 7 1
【Output example】
9 12 12 19 25 29 31 44 45 54
【Tips】
【Data size】

n , m ≤ 10000 n,m≤10000n,m10000


Topic analysis: first explain, ( x ∈ N ∗ ) (x ∈ N∗)(xN ) means that x is a positive integer. When many people look at the question, the first thing they think of is to use the priority queue from small to large, that is, the small root heap, but in this case, n*m functions need to be calculated, and they are all saved in the priority queue, and finally the smaller one is taken out. m, in order to guarantee the correct answer. But in this case the memory and time will be exceeded. We can change the idea and limit the queue to have only m data. If there are no more than m data, enter the queue directly after calculation. Otherwise, when calculating the function, first compare the result with the maximum value in the queue. If it is less than the maximum value, the maximum value will be used. The value is dequeued, and the result of the function being evaluated is enqueued. If the currentF i ( x ) F_{i}(x)Fi( x ) is greater than the maximum queue value, then start to calculate the next functionF i + 1 ( x ) F_{i+1}(x)Fi+1( x ) . It can be seen that the big root heap should be used, that is, the priority queue from large to small, and finally the entire queue is reversed.


C++ code

#include<iostream>
#include<queue>
using namespace std;
struct func
{
    
    
	int a, b, c;
	int value(int x)const
	{
    
    
		return a * x * x + b * x + c;
	}
};
int main()
{
    
    
	int n, m;
	cin >> n >> m;
	priority_queue<int> q;
	func* arr = new func[n];
	for (int i = 0; i < n; i++)
	{
    
    
		cin >> arr[i].a >> arr[i].b >> arr[i].c;
		for (int j = 1;; j++)
		{
    
    
			if (q.size() < m)
				q.push(arr[i].value(j));
			else
			{
    
    
				int k = arr[i].value(j);
				if (k < q.top())
				{
    
    
					q.pop();
					q.push(k);
				}
				else
					break;
			}
		}
	}
	int* result = new int[m];
	for (int i = 0; i < m; i++)
	{
    
    
		result[i] = q.top();
		q.pop();
	}
	for (int i = m - 1; i >= 0; i--)
		cout << result[i] << ' ';
	delete[]arr, result;
	return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324130226&siteId=291194637