Function HDU - 6546 (数学,贪心)
wls 有 n 個二次函數 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n).
現在他想在∑ni=1xi = m 且 x 為正整數的條件下求∑ni=1Fi(xi)的最小值。
請求出這個最小值。
Input
第一行兩個正整數 n, m。
下面 n 行,每行三個整數 a, b, c 分別代表二次函數的二次項, 一次項,常數項系數。
1 ≤ n ≤ m ≤ 100, 000
1 ≤ a ≤ 1, 000
?1, 000 ≤ b, c ≤ 1, 000
Output
一行一個整數表示答案。
Sample Input
2 3
1 1 1
2 2 2
Sample Output
13
思路:
因為題目要求所以的xi 都要為正整數,那么每一個xi最小也要是1 ,所以我們先給每一個xi賦值為1,
同時,我們用堆來維護對于每一個二次函數 當前的 F(xi+1) - F( xi ) 為什么維護這個數?
因為當前的xi值對應的函數值是F(xi ) 我們要讓sum xi = m 如果 M>n 肯定要給一些二次函數值得xi增加數值的,那么我們通過維護的這個信息,
每一次貪心的去增加一個讓 那個函數值 xi 增加為 xi+1 最答案的貢獻是最小。
重復此過程,直至sum xi = m
細節見代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ struct node {ll a,b,c;ll x;ll val;bool operator < (const node & t) const {return val>t.val;} }; priority_queue<node> heap; int n; int m; ll gao(ll a,ll b, ll c ,ll x) {return a*x*x+b*x+c; } int main() {//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);gbtb;cin>>n>>m;ll a,b,c;ll ans=0ll;repd(i,1,n){cin>>a>>b>>c;node temp;temp.a=a;temp.b=b;temp.c=c;temp.x=1;temp.val=gao(a,b,c,2)-gao(a,b,c,1);heap.push(temp);}m-=n;while(m--){node temp=heap.top();heap.pop();temp.x++;temp.val=gao(temp.a,temp.b,temp.c,temp.x+1)-gao(temp.a,temp.b,temp.c,temp.x);heap.push(temp);}while(!heap.empty()){node temp=heap.top();heap.pop();ans+=gao(temp.a,temp.b,temp.c,temp.x);}cout<<ans<<endl;return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }轉載于:https://www.cnblogs.com/qieqiemin/p/11306319.html
總結
以上是生活随笔為你收集整理的Function HDU - 6546 (数学,贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个程序员的创业失败教训
- 下一篇: Delphi纯代码连SQLite数据库,