zoukankan      html  css  js  c++  java
  • Codeforces 1090A

    题目链接:https://codeforces.com/contest/1090/problem/A

    A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.

    But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.

    To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.

    Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.

    Input
    The first line contains a single integer n — the number of companies in the conglomerate (1≤n≤2⋅10^5). Each of the next n lines describes a company.

    A company description start with an integer mi — the number of its employees (1≤mi≤2⋅10^5). Then mi integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.

    The total number of employees in all companies does not exceed 2⋅10^5.

    Output
    Output a single integer — the minimal total increase of all employees that allows to merge all companies.

    Example
    input
    3
    2 4 3
    2 2 1
    3 1 1 1
    output
    13
    Note
    One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4,3,4,3] and [1,1,1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2+2+3+3+3=13.

    题解:

    要将 $n$ 家公司合并,每次只能合并两家,而且只有当两家公司的最高薪资相等时才能合并。而且,一旦提薪,是所有员工同时提薪。

    因此,至少所有公司的最高薪资都要提到和最高薪资最大的那家公司一样才行。这样一来,很容易就能计算出总高要提薪多少。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+10;
    int n;
    struct Com{
        int ct,mx;
    }com[maxn];
    int main()
    {
        scanf("%d",&n);
        int _max=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&com[i].ct);
            com[i].mx=0;
            for(int j=1,x;j<=com[i].ct;j++)
            {
                scanf("%d",&x);
                com[i].mx=max(com[i].mx,x);
            }
            _max=max(_max,com[i].mx);
        }
        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            if(com[i].mx<_max)
                ans+=(long long)com[i].ct * (_max-com[i].mx);
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    MSSQL中join的用法詳解
    程序员的十层楼
    【搜集】错误为: [Microsoft][SQL Native Client]客户端不支持加密
    關於 ASP.NET 中System.OutOfMemoryException 的問題與解決方法
    CSS:快速提升设计可读性和维护性
    【调查 】DBA的压力究竟有多大?
    从沙子到芯片:且看处理器是怎样炼成的
    與伺服器的連接已成功建立 但在登入程序時發生錯誤。 provider 共用記憶提供者 error 0 管道的另一端上無任何處理程序。
    【ZT】Visual Studio Team System縱覽
    Win32.Induc.A的清理方法
  • 原文地址:https://www.cnblogs.com/dilthey/p/10094324.html
Copyright © 2011-2022 走看看