这题好水……但是还是写一写吧……
問題の解
用\(now[i]\)记录使得当前状态下\(f_i(x)\)取得最小值的自变量的值。
初始化:
初始状态,我们有\(n\)个二次函数\(f(x)=A_ix^2+B_ix+C_i,x \in \mathbb{N}^+\);
- 对称轴\(- \frac{B_i}{2A_i} \in (- \infty , 1)\)则\(now[i]=1\);
- 对称轴\(- \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;
}
原文地址:https://www.cnblogs.com/kion/p/11834516.html
时间: 2024-10-10 10:13:55