码迷,mamicode.com
首页 > 其他好文 > 详细

P2085 最小函数值

时间:2018-07-31 00:29:45      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:ref   https   scanf   tps   printf   cpp   bool   http   algo   

这道题跟P1631 序列合并嘻嘻相关


题目给你\(n\)个二次函数,给你\(a\),\(b\),\(c\)

不过仔细的话可以发现:这三个系数都是正整数!

所以意味着二次函数的对称轴在x轴负半轴,在我们考虑的\([1, +\infty]\)中的整数区间都是单调递增的。

所以同一个函数中,\(x=1\)时的函数值是最小的。

如何求目标值?下面给出算法。

对于每个函数,是不是有一个单调答案序列:\(a_i \times 1^2 + b_i \times 1 + c_i\), \(a_i \times 2^2 + b_i \times 2 + c_i\)...\(a_i \times \infty^2 + b_i \times \infty + c_i\)

对于总共\(n\)个函数,就有这么\(n\)个单调序列。

我们的做法是将所有的单调序列中的最小元素入队。

然后每一次取出优先队列中最小的元素出来,这个元素就计入答案,被我们输出。

我们可以补充一个数,就是相同序列的下一个元素。例如出队了\(x=1\)的元素,就补充同个序列的\(x=2\)进去。

这样做的话保证优先队列中是稳定的\(n\)个元素。

不知道怎么证明。。。

实现过程中,堆肯定用priority_queue实现啦。只不过其中元素可以自定结构体方便写。

代码:

#include<cstdio>
#include<queue>
#include<algorithm>
const int maxn = 10005;
int n, m;
int a[maxn], b[maxn], c[maxn];
struct Nodes
{
    int val, i, x;
    bool operator < (const Nodes &rhs) const
    {
        return val > rhs.val;
    }
};
std::priority_queue<Nodes> q;
int f(int i, int x)
{
    return a[i] * x * x + b[i] * x + c[i];
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &a[i], &b[i], &c[i]);
    }
    for(int i = 1; i <= n; i++) q.push((Nodes){f(i, 1), i, 1});
    for(int t = 1; t <= m; t++)
    {
        Nodes temp = q.top(); q.pop();
        printf("%d", temp.val);
        q.push((Nodes){f(temp.i, temp.x + 1), temp.i, temp.x + 1});
        if(t != m) printf(" ");
    }
    printf("\n");
    return 0;
}

P2085 最小函数值

标签:ref   https   scanf   tps   printf   cpp   bool   http   algo   

原文地址:https://www.cnblogs.com/Garen-Wang/p/9393545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!