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 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- 用 yaml 写测试用例
采用 excel 的方式写测试用例,格式比较死板,比较容易出错,采用 yaml / yml 格式来写用例. 首先安装 pyyaml:pip install pyyaml 建立一个 login.yaml ...
- git仓库搭建及免密使用
- 存储单位bit、byte、KB、MB、GB、TB、PB、EB、ZB、YB、BB、NB、DB、CB、XB
bit(binary digit)比特,这是表示信息的最小单位,它只能表示 0 或 1. byte(字节),其表示的值范围是0~255(无符号) 或 -127~127(有符号),1 byte = 8 ...
- flutter SafeArea和SliverSafeArea,适应特殊屏幕
现如今的手机已经不能提供给应用程序规整的矩形界面了,一些带圆角或者是刘海屏让应用程序的布局更加复杂,甚至是需要单独适配,这对开发者来来太糟糕了. 因此SafeArea控件应用而生,SafeArea通过 ...
- mqtt抓包
mqtt消息抓包 账号.密码.imappclientId 或者imwebclientId校验成功后跟imserver连接成功 部分交互:先获取到uid,再根据uid拿到其他用户信息,且部分信息通过 ...
- Ubuntu之切换root用户提示认证失败
出现认证失败(Authentication failure)的原因有两种,要么是密码输入错误,要么是新安装的系统还没有给root设置密码,,如果是后者,可以通过命令sudo passwd root进行 ...
- FastJson JdbcRowSetImpl
Java安全之FastJson JdbcRowSetImpl 链分析 利用限制 RMI利用的JDK版本≤ JDK 6u132.7u122.8u113 LADP利用JDK版本≤ 6u211 .7u201 ...
- iis发布后设置文件夹用户安全权限
发布iis后异常截图: 401 - Unauthorized: Access is denied due to invalid credentials.You do not have permissi ...
- 在axios拦截器js文件中无法使用vue-router问题
如果在axios设置拦截器的js文件或者说在vue-router路由配置文件,配置好的路由文件以外的js文件中使用 import {useRouter} from 'vue-router' 系统不会默 ...
- 小程序使用webview嵌套H5两边如何传参.
需求:项目里面需要进行人脸核身.需要调起小程序的人脸核身功能.需要h5跳转到小程序页面.验证完后回退 1.h5页面先引入一个js文件 2.当用微信小程序的web-view内嵌H5页面的时候,H5页面的 ...