Codeforces Round #728 (Div. 2) C. Great Graphs
题意
给你一个数组\(d\),\(d[i]\)表示从节点\(1\)到其他各个节点的最短路的长度,然后你可以对这个图进行加边(可以是负边),但不允许存在一个权值和为负数的回路。
题解
按样例的思想,大概就是将这些点按距离\(1\)的距离从小到大排个序,这样就使得所有点连成一条直线,这样总是可以保证加最多的负边,然后就开始加负边,对于每个点,将他和他前面的点全连上,这样就可以保证加足够多的负边且没有负环。
然后就是计算,很简单的一个推公式,很容易知道,相邻的两个是互相抵消的,不需要计算,设\(S_i\)表示节点\(i\)到节点\(1\)的距离(排序后),则
\]
硬算的话肯定时间超了,于是对\(S_i\)和\(S_j\)分开计算,对于\(S_i\),它总共被计算了\(n-i-1\)次,最多计算到\(S_{n-2}\)。对于\(S_j\),它总共被计算了\(j-2\)次,从\(S_3\)开始计算,于是上式又可以写为
\]
AC代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <cstring>
#include <map>
#include <set>
#define IO ios::sync_with_stdio(NULL)
#define sc(z) scanf("%d", &(z))
#define _ff(i, a, b) for (ll i = a; i <= b; ++i)
#define _rr(i, a, b) for (ll i = b; i >= a; --i)
#define _f(i, a, b) for (ll i = a; i < b; ++i)
#define _r(i, a, b) for (ll i = b - 1; i >= a; --i)
#define mkp make_pair
#define endl "\n"
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 5;
const ll mod = 1e9 + 7;
const ld EPS = 1e-8;
ll d[N];
int main() {
IO;
int T; cin >> T;
while (T--) {
ll n; cin >> n;
_ff(i, 1, n) cin >> d[i];
sort(d + 1, d + 1 + n);
ll ans = 0;
_ff(i, 1, n - 2) {
ans += (n - i - 1) * (d[n - i + 1] - d[i]);
}
// ans -= d[n];
if (ans) cout << "-";
cout << ans << endl;
}
return 0;
}
Codeforces Round #728 (Div. 2) C. Great Graphs的更多相关文章
- Codeforces Round #649 (Div. 2)
Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- Esp8266 Arduino PubSubClient连接阿里云老是返回rc=2错误的解决方案
最近在用Esp8266 Arduino连接阿里云IOT的时候,服务器端老是返回rc=2 的错误! 用了MQTTfx模拟连接阿里云IOT没有问题,但是把同样的ClientID,UserName和Pass ...
- python学习——set集合,sorted函数
题目: 输入a,b班的名单,并进行如下统计. 输入格式: 第1行::a班名单,一串字符串,每个字符代表一个学生,无空格,可能有重复字符.第2行::b班名单,一串字符串,每个学生名称以1个或多个空格分隔 ...
- 解决不了ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing啊
我今天真的好难受,在我结束hbase的配置的后,又是成功运行了shell命令,我以为万事大吉,结果下午再用shell命令时,给我报了这个错,我按着网上说的删了点东西,还是解决不了,难受啊. 我现在时决 ...
- nanoPi R1 资料
eflasher脱机烧写 在命令行终端中通过执行下列命令进行烧写: $ su root $ eflasher root 用户的密码是 fa. 串口登录 控制台波特率 115200
- Raid磁盘阵列更换磁盘时另一块盘离线恢复案例(v7000存储数据恢复)
Raid磁盘阵列更换磁盘时另一块盘离线恢复案例(v7000存储数据恢复) [故障描述]客户设备型号为IBM V7000存储,架构为AIX+oracle+V7000存储阵列柜,需要恢复的数据主要存放在阵 ...
- sql 行转列分析 以后再也不用记了
表scores 请转成的横表是这样子的: // ::::我们来分析下:首先 姓名这一列肯定是不重复的姓名[重复也没办法了 这需求只能这样了] 因此 我们用group by [姓名] 然后姓名这一列就有 ...
- clickHouse安装踩坑
1.查看版本 clickhouse-server --version 2.下载 linux机器上 wget下载,有两个安装包体积较大 wget https://packages.clickhouse ...
- start-stop服务器启动
springBoot打成jar包扔到linux服务器 start.sh nohup java -Dfile.encoding=utf-8 -jar XXX-1.0-SNAPSHOT.jar > ...
- 分布式事务seata
1.事务的4大基本特征. 1)原子性 2)一致性 3)隔离性 4)持久性 2.什么是分布式事务? 本地事务:单服务进程,单数据库资源,同一个连接conn多个事务操作. 分布式事务:多服 ...
- Hibernate的工作流程