题目链接: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+;
int n;
struct Com{
int ct,mx;
}com[maxn];
int main()
{
scanf("%d",&n);
int _max=;
for(int i=;i<=n;i++)
{
scanf("%d",&com[i].ct);
com[i].mx=;
for(int j=,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=;
for(int i=;i<=n;i++)
{
if(com[i].mx<_max)
ans+=(long long)com[i].ct * (_max-com[i].mx);
}
cout<<ans<<endl;
}

Codeforces 1090A - Company Merging - [签到水题][2018-2019 Russia Open High School Programming Contest Problem A]的更多相关文章

  1. Codeforces 1090M - The Pleasant Walk - [签到水题][2018-2019 Russia Open High School Programming Contest Problem M]

    题目链接:https://codeforces.com/contest/1090/problem/M There are n houses along the road where Anya live ...

  2. Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]

    题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...

  3. Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]

    题目链接:https://codeforces.com/contest/1090/problem/B Examplesstandard input The most famous characters ...

  4. Codeforces Gym 100286G Giant Screen 水题

    Problem G.Giant ScreenTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/con ...

  5. codeforces 577B B. Modulo Sum(水题)

    题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  7. codeforces 696A Lorenzo Von Matterhorn 水题

    这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...

  8. CodeForces 589I Lottery (暴力,水题)

    题意:给定 n 和 k,然后是 n 个数,表示1-k的一个值,问你修改最少的数,使得所有的1-k的数目都等于n/k. 析:水题,只要用每个数减去n/k,然后取模,加起来除以2,就ok了. 代码如下: ...

  9. HDU 6300.Triangle Partition-三角形-水题 (2018 Multi-University Training Contest 1 1003)

    6300.Triangle Partition 这个题就是输出组成三角形的点的下标. 因为任意三点不共线,所以任意三点就可以组成三角形,直接排个序然后输出就可以了. 讲道理,没看懂官方题解说的啥... ...

随机推荐

  1. git merge dryrun

    As noted previously, pass in the --no-commit flag, but to avoid a fast-forward commit, also pass in  ...

  2. SpringBoot乱码

    第一步: 第一步,约定好传参编码格式 不管是运用httpclient,还是原生http,都要设置传参的编码,为了统一,这儿全部设置为utf-8 第二步,修正application.properties ...

  3. class<T>和 class<?>类型

    ? 表示不确定的java类型. T 表示java类型 Class<T>在实例化的时候,T要替换成具体类Class<?>它是个通配泛型,?可以代表任何类型<? extend ...

  4. apache的server-status如何分析的技术说明

    XML/HTML代码 Apache Server Status for www.blogguy.cn Server Version: Apache/2.2.9 (Debian) PHP/5.2.6-1 ...

  5. CZT变换(chirp z-transform)

    作者:桂. 时间:2018-05-20  12:04:24 链接:http://www.cnblogs.com/xingshansi/p/9063131.html 前言 相比DFT,CZT是完成频谱细 ...

  6. [转]:Delphi XE中泛型数组的使用范例

    Delphi XE中泛型数组的使用范例,下面的范例简单的使用了泛型字符串数组,如用 TArray 代替 array of Word, 还可以使用 TArray 类提供的算法(就是少了点). uses ...

  7. Clash Royale开发日志

    2018-04-17 本次更新: [修复点击时间bug] [修复断线重连后不自动开始脚本bug] 2018-04-16 本次更新增加以下功能: [支持断线重连机制(5分钟)] [界面UI设置] 201 ...

  8. 解决ubuntu下IntelliJ IDEA无法锁定到启动器的问题

    参考:https://www.zhihu.com/question/31626161 在目录~/.local/share/applications 中找到以前的IDEA的.desktop文件,删掉即可

  9. C语言 · 积分之迷

    标题:积分之迷 小明开了个网上商店,卖风铃.共有3个品牌:A,B,C. 为了促销,每件商品都会返固定的积分. 小明开业第一天收到了三笔订单: 第一笔:3个A + 7个B + 1个C,共返积分:315 ...

  10. mybatis generator 为数据库保留字段 转义

    为order 添加转义符号“ ` ” `order` <property name="autoDelimitKeywords" value="true"& ...