zoukankan      html  css  js  c++  java
  • Function HDU

    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), '', 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 == '
    ');
        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';
            }
        }
    }
    
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    win10 uwp 弹起键盘不隐藏界面元素
    win10 uwp 存放网络图片到本地
    win10 uwp 存放网络图片到本地
    sublime Text 正则替换
    sublime Text 正则替换
    win10 uwp 绘图 Line 控件使用
    win10 uwp 绘图 Line 控件使用
    AJAX 是什么?
    什么是 PHP SimpleXML?
    PHP XML DOM:DOM 是什么?
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/11306319.html
Copyright © 2011-2022 走看看