Great Graphs

题意

给你一个数组\(d\),\(d[i]\)表示从节点\(1\)到其他各个节点的最短路的长度,然后你可以对这个图进行加边(可以是负边),但不允许存在一个权值和为负数的回路。

题解

按样例的思想,大概就是将这些点按距离\(1\)的距离从小到大排个序,这样就使得所有点连成一条直线,这样总是可以保证加最多的负边,然后就开始加负边,对于每个点,将他和他前面的点全连上,这样就可以保证加足够多的负边且没有负环。

然后就是计算,很简单的一个推公式,很容易知道,相邻的两个是互相抵消的,不需要计算,设\(S_i\)表示节点\(i\)到节点\(1\)的距离(排序后),则

\[ans=\sum_{i=1}^n\sum_{j=i+2}^n(S_j-S_i)
\]

硬算的话肯定时间超了,于是对\(S_i\)和\(S_j\)分开计算,对于\(S_i\),它总共被计算了\(n-i-1\)次,最多计算到\(S_{n-2}\)。对于\(S_j\),它总共被计算了\(j-2\)次,从\(S_3\)开始计算,于是上式又可以写为

\[ans=\sum_{i=1}^{n-2}(n-i-1)(S_{n-i+1}-S_i)
\]

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的更多相关文章

  1. Codeforces Round #649 (Div. 2)

    Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...

  2. 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 ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  10. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

随机推荐

  1. 【QT+MSVC2015】不安装VS2015,QT配置MSVC2015编译环境

    本文介绍不安装VS2015的情况下在QT5.10中配置MSVC2015编译器. 系统:windows系统 QT版本:5.10.1 所需文件: QT安装程序--qt-opensource-windows ...

  2. Biba模型(MAC)

    毕巴模型用完整性级别来对完整性进行量化描述. 设i­1和i2是任意两个完整性级别,如果完整性级别为i2的实体比完整性级别为i1的实体具有更高的完整性,则称完整性级别i2绝对支配完整性级别i1,记为:i ...

  3. Delphi获取程序版本号

    参考: http://www.delphitop.com/html/hanshu/4627.html procedure GetVersionInfo(const FileName:string; v ...

  4. 使用Wireshark完成实验3-IP

    1.使用Wireshark打开ip-ethereal-trace-1,如图 电脑IP地址为192.168.1.102 2.如图,IP包头中上层协议字段的值为1,代表为ICMP 3.如图,IP头中有20 ...

  5. popen函数和pyinstaller打包之 -w冲突

    启发文章:https://www.jb51.net/article/184731.htm 之前我也是用到了os.popen()这个函数 1.os.popen(self.excel_path)  括号里 ...

  6. Go组件库总结之无等待锁

    本篇文章我们用Go封装一个无等待锁库.文章参考自:https://github.com/brewlin/net-protocol 1.锁的封装 type Mutex struct { v int32 ...

  7. 模拟ATM系统 —— 用户存款、取款、转账、修改密码和销户功能

    接着上一篇: 5.用户存款功能 分析: *存款就是拿到当前账户对象 *然后让用户输入存款的金额 *调用账户对象的setMoney方法将账户余额修改成存钱后的余额 *存款后需要查询当前账户信息,确认是否 ...

  8. 关于filter_input函数

    PHP: filter_input <?php $search_html = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_ ...

  9. yolov5s yolov8n 在自己数据集上测试比较(640*640)

    yolov5s -> 0.5map:  96.5 -> ncnn:75ms yolov8n -> 0.5map:  94.1 -> ncnn:52ms

  10. win11恢复完整右键菜单

    使用注册表修改 首先,通过修改注册表,我们就可以将 Win11 的右键菜单改为老样式.下面是具体的方法. 运行"regedit",开启注册表编辑器,定位到"HKEY_CU ...