LUOGU P2085 minimum function value

Title Description

There are N functions, F1,F2 ,Fn. Define Fi(x)=Ai*x^2+Bi*x+Ci (x ∈ N *). Given these Ai, Bi, and Ci, the smallest m of all function values of all functions is requested (if there are duplicates, multiple values will be output).

I / O format

Input format:
Input data: input two positive integers n and m in the first line. There are three positive integers i n each of the following N lines, among which the three numbers in line I are AI, Bi and CI. Ai<=10,Bi<=100,Ci<=10 000.

Output format:
Output data: output the first m elements after sorting all the generated function values of these n functions. The number of M should be output to a line, separated by spaces.

I've written a few lines. I feel more comfortable... One of the descriptions in this question is that a, b and c are all positive integers. According to the vertex of quadratic function in junior high school mathematics, the x of the minimum value of the function must be monotonically increasing on the negative half axis (which I didn't expect at the beginning), and then on the domain of definition (0, + ∞).
In fact, because the data of this problem is very water (it should be), and then violence can be passed. Build a big root heap, enumerate the values of 1-m of each function, and if the conditions are met, it will be put into the heap Then if the number in the heap is greater than or equal to the required number, and the number is greater than the top of the heap, skip this function. That's it.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN=10005;
int n,m;
//int a[MAXN],b[MAXN],c[MAXN],cnt[MAXN];
int t;
int  hp[MAXN];
int ans[MAXN];
struct O {
    int a,b,c;
} h[MAXN];
int del() { //Large root pile
    int res=hp[1];
    hp[1]=hp[t];
    t--;
    int now=1;
    while(now*2<=t) {
        int tp=now*2;
        if(tp<t&&hp[tp]<hp[tp+1])tp++;
        if(hp[tp]>hp[now])swap(hp[tp],hp[now]);
        else break;
        now=tp;
    }
    return res;
}
bool cmp(O x,O y) {
    return x.c<y.c;
}
void pus(int x) {
    hp[++t]=x;
    int now=t;
    while(now>1) {
        int tp=now/2;
        if(hp[now]>hp[tp])swap(hp[now],hp[tp]);
        else break;
        now=tp;
    }
}
int f(int i,int x) {
    return h[i].a*x*x+h[i].b*x+h[i].c;
}
int main() {
    scanf("%d%d",&n,&m);
    memset(hp,0x7f,sizeof hp);
    for(register int i=1; i<=n; i++) {
        scanf("%d%d%d",&h[i].a,&h[i].b,&h[i].c);
    }
//  sort(h+1,h+n,cmp);
    for(register int i=1; i<=n; i++) {
        for(register int j=1; j<=m; j++) {
            int r=f(i,j);
            while(t>m) {//The number of elements in the heap is not greater than m
                del();
            }
            if(r<hp[1]||t<m) {
                pus(r);
            } else break;
        }
    }
    while(t>m) {
        del();
    }
    for(int i=1; i<=m; i++) {
        ans[i]=del();
    }
    for(int i=m; i>=1; i--)cout<<ans[i]<<" ";//Reverse output
}

It's going to be December... square

Posted by alco19357 on Mon, 18 May 2020 08:24:35 -0700