最小函数值 洛谷2085 堆

14 篇文章 0 订阅

题目描述


有n个函数,分别为F1,F2,…,Fn。定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*)。给定这些Ai、Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个)。

输入格式:


输入数据:第一行输入两个正整数n和m。以下n行每行三个正整数,其中第i行的三个数分别位Ai、Bi和Ci。Ai<=10,Bi<=100,Ci<=10 000。

输出格式:


输出数据:输出将这n个函数所有可以生成的函数值排序后的前m个元素。这m个数应该输出到一行,用空格隔开。

说明


数据规模:n,m<=10000

Analysis


题意直接粗暴,不知道要怎么说了
开一个优先队列记录函数类型、当前x的值
一开始把所有函数的最小值压进去,然后此时的堆顶一定是最小的
那么我们把堆顶的x+1再压回去,如此做m次

重装系统之后什么都没了,气死

Code


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define debug puts("-----")
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)
#define fill(x, t) memset(x, t, sizeof(x))
#define f(x, a, b, c) x * x * a + x * b + c
#define pb push_back
#define PI (acos(-1.0))
#define EPS (1e-8)
#define INF (1<<30)
#define ll long long
#define db double
#define ld long double
#define N 20001
#define E N * 8 + 1
#define MOD 100000007
#define L 255
using namespace std;
vector<int> a, b, c;
struct pos{
    int x, type;
    bool operator >(const pos &r) const{
        pos l = *this;
        return f(l.x, a[l.type], b[l.type], c[l.type]) <= f(r.x, a[r.type], b[r.type], c[r.type]);
    }
    bool operator <(const pos &r) const{
        pos l = *this;
        return f(l.x, a[l.type], b[l.type], c[l.type]) > f(r.x, a[r.type], b[r.type], c[r.type]);
    }

};
inline int read(){
    int x = 0, v = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9'){
        if (ch == '-'){
            v = -1;
        }
        ch = getchar();
    }
    while (ch <= '9' && ch >= '0'){
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return x * v;
}
int main(void){
    int n = read(), m = read();
    rep(i, 1, n){
        a.pb(read());
        b.pb(read());
        c.pb(read());
    }
    priority_queue<pos> heap;
    rep(i, 0, a.size() - 1){
        heap.push((pos){1, i});
    }
    while (m --){
        pos now = heap.top(); heap.pop();
        printf("%d ", f(now.x, a[now.type], b[now.type], c[now.type]));
        heap.push((pos){now.x + 1, now.type});
    }
    puts("\n");
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值