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 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- Java-Java调用mysqldump进行数据库备份
1 public ResultData backupDatabase(Integer type) { 2 // 构建备份sql的文件名 3 String sqlFileName = "tes ...
- [Swift] 在 OC 工程中,创建和使用 Swift
1.我们创建了一个 Objective-C 的工程,叫做 playGround. 2.首先,我们需要在 工程的 Build Settings,找到 如中所示的项目,并将 Defines Module ...
- 使用Libusb和hidapi测试HID设备
一.测试中断或者Bulk传输: 首先要使用Libusb打印出HID设备的Endpoint查看是否支持中断或者Bulk传输模式:如果支持的话才可以进一步测试: 因为HID设备在插入的时候无需安装,并且一 ...
- Jenkins+Git+Gitlab+Ansible 持续集成和自动部署
- scrollIntoView()方法将元素滚动到浏览器窗口的可见区域
TIPS:容器可滚动时才有用! 语法 element.scrollIntoView(); // 等同于element.scrollIntoView(true) element.scrollIntoVi ...
- Tomcat启动报A fatal error has been detected by the Java Runtime Environment
# # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x0000 ...
- Hadoop完全分布式开发配置流程
完全分布式开发 整体流程 1.准备3台纯净虚拟机 2.修改每台ip,主机名,主机映射,关闭防火墙 3.安装jdk和hadoop,配置环境变量 4.集群分发脚本编写 5.集群配置 6.ssh免密登录 7 ...
- python peewee
from peewee import MySQLDatabase, Model from peewee import CharField, IntegerField, BooleanField # 引 ...
- class_task
#!/usr/bin/python # -*- coding: UTF-8 -*- class Task(): _cls_name = "cls name" def ...
- Salesforce 发送Email时遇到的问题(Case当中的Filed不出现Email选项:ケースのフィールドにメールタブが表示されない)。
普段はケースの発生源はメールと選択する場合.ディフォルトで「フィールド」の中に「メール」というタブが出てきますが. (平时当我们选择[Case]的[来源]为[Email]时,默认就会在field中出现 ...