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

LGOJ P2085 最小函数值

时间:2019-11-11 14:21:41      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:floor   for   org   ref   ack   数值   mes   cto   输出   

P2085 最小函数值

这题好水……但是还是写一写吧……

問題の解

\(now[i]\)记录使得当前状态下\(f_i(x)\)取得最小值的自变量的值。

初始化:
初始状态,我们有\(n\)个二次函数\(f(x)=A_ix^2+B_ix+C_i,x \in \mathbb{N}^+\);

  1. 对称轴\(- \frac{B_i}{2A_i} \in (- \infty , 1)\)\(now[i]=1\)
  2. 对称轴\(- \frac{B_i}{2A_i} \in [1, \infty )\)\(now[i]= \lfloor - \frac{B_i}{2A_i} \rfloor\)

定义一个multiset<node> S;
其中,node:

struct node
{
    int val, i;
    node() {}
    node(int Value, int i_f)
    {
        val = Value;
        i = i_f;
    }
    bool operator<(const node &o) const
    {
        return val < o.val;
    }
};

val存放函数值,i表示这是这是\(i\)号函数,也就是\(f_i(x)\);

\(n\)个函数怼入S,内部顺序按照\(f_i(now[i])\)升序;

以下内容执行\(m\)次:

每次取S的头,也就是当前最小值,输出头的val,并记录一下这个函数是\(cur\)号函数;

\(f_{cur}(x)\)对应的\(now[cur]++\),把\(node(f_{cur}(now[cur]),cur)\)S里;

code:

#include <bits/stdc++.h>
using namespace std;
const int N = 10005;

int n, m, k;
int now[N], A[N], B[N], C[N], cnt[N];
struct node
{
    int val, i;
    node() {}
    node(int Value, int i_f)
    {
        val = Value;
        i = i_f;
    }
    bool operator<(const node &o) const
    {
        return val < o.val;
    }
};
multiset<node> S;
vector<int> ans;
inline int f(int i, int x)
{
    return A[i] * x * x + B[i] * x + C[i];
}

inline int sym(int i)
{
    int s = -(B[i] / (2 * A[i]));
    if (s <= 0)
        s = 1;
    return s;
}

int main()
{
    cin >> 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++)
        now[i] = sym(i);
    for (int i = 1; i <= n; i++)
        S.insert(node(f(i, now[i]), i));
    for (int i = 1; i <= m; i++)
    {
        multiset<node>::iterator it = S.begin();
        ans.push_back(it->val);
        S.insert(node(f(it->i, ++now[it->i]), it->i));
        S.erase(it);
    }
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << ' ';

    return 0;
}

LGOJ P2085 最小函数值

标签:floor   for   org   ref   ack   数值   mes   cto   输出   

原文地址:https://www.cnblogs.com/kion/p/11834516.html

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