CodeForces 903D Almost Difference

题目描述

Let's denote a function

You are given an array aa consisting of nn integers. You have to calculate the sum of d(a_{i},a_{j})d(ai,aj) over all pairs (i,j)(i,j) such that 1<=i<=j<=n1<=i<=j<=n .

输入输出格式

输入格式:

 

The first line contains one integer nn ( 1<=n<=2000001<=n<=200000 ) — the number of elements in aa .

The second line contains nn integers a_{1}a1 , a_{2}a2 , ..., a_{n}an ( 1<=a_{i}<=10^{9}1<=ai<=109 ) — elements of the array.

 

输出格式:

 

Print one integer — the sum of d(a_{i},a_{j})d(ai,aj) over all pairs (i,j)(i,j) such that 1<=i<=j<=n1<=i<=j<=n .

 

输入输出样例

输入样例#1: 
5
1 2 3 1 3
输出样例#1: 
4
输入样例#2: 
4
6 6 5 5
输出样例#2: 
0
输入样例#3: 
4
6 6 4 4
输出样例#3: 
-8

说明

In the first example:

  1. d(a_{1},a_{2})=0d(a1,a2)=0 ;
  2. d(a_{1},a_{3})=2d(a1,a3)=2 ;
  3. d(a_{1},a_{4})=0d(a1,a4)=0 ;
  4. d(a_{1},a_{5})=2d(a1,a5)=2 ;
  5. d(a_{2},a_{3})=0d(a2,a3)=0 ;
  6. d(a_{2},a_{4})=0d(a2,a4)=0 ;
  7. d(a_{2},a_{5})=0d(a2,a5)=0 ;
  8. d(a_{3},a_{4})=-2d(a3,a4)=2 ;
  9. d(a_{3},a_{5})=0d(a3,a5)=0 ;
  10. d(a_{4},a_{5})=2d(a4,a5)=2 .

 

算法很简单,,,,但是TM的要高精度gg

#include<bits/stdc++.h>
#define maxn 200005
#define ll long long
using namespace std;  
const int base = 1000000000;
const int base_digits = 9;struct bigint {vector<int> z;int sign;bigint() : sign(1) { }bigint(long long v) { *this = v; }bigint(const string &s) { read(s); }void operator=(const bigint &v) {sign = v.sign;z = v.z;}void operator=(long long v) {sign = 1;if (v < 0)sign = -1, v = -v;z.clear();for (; v > 0; v = v / base)z.push_back(v % base);}bigint operator+(const bigint &v) const {if (sign == v.sign) {bigint res = v;for (int i = 0, carry = 0; i < (int) max(z.size(), v.z.size()) || carry; ++i) {if (i == (int) res.z.size())res.z.push_back(0);res.z[i] += carry + (i < (int) z.size() ? z[i] : 0);carry = res.z[i] >= base;if (carry)res.z[i] -= base;}return res;}return *this - (-v);}bigint operator-(const bigint &v) const {if (sign == v.sign) {if (abs() >= v.abs()) {bigint res = *this;for (int i = 0, carry = 0; i < (int) v.z.size() || carry; ++i) {res.z[i] -= carry + (i < (int) v.z.size() ? v.z[i] : 0);carry = res.z[i] < 0;if (carry)res.z[i] += base;}res.trim();return res;}return -(v - *this);}return *this + (-v);}void operator*=(int v) {if (v < 0)sign = -sign, v = -v;for (int i = 0, carry = 0; i < (int) z.size() || carry; ++i) {if (i == (int) z.size())z.push_back(0);long long cur = z[i] * (long long) v + carry;carry = (int) (cur / base);z[i] = (int) (cur % base);//asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));}trim();}bigint operator*(int v) const {bigint res = *this;res *= v;return res;}friend pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1) {int norm = base / (b1.z.back() + 1);bigint a = a1.abs() * norm;bigint b = b1.abs() * norm;bigint q, r;q.z.resize(a.z.size());for (int i = a.z.size() - 1; i >= 0; i--) {r *= base;r += a.z[i];int s1 = b.z.size() < r.z.size() ? r.z[b.z.size()] : 0;int s2 = b.z.size() - 1 < r.z.size() ? r.z[b.z.size() - 1] : 0;int d = ((long long) s1 * base + s2) / b.z.back();r -= b * d;while (r < 0)r += b, --d;q.z[i] = d;}q.sign = a1.sign * b1.sign;r.sign = a1.sign;q.trim();r.trim();return make_pair(q, r / norm);}friend bigint sqrt(const bigint &a1) {bigint a = a1;while (a.z.empty() || a.z.size() % 2 == 1)a.z.push_back(0);int n = a.z.size();int firstDigit = (int) sqrt((double) a.z[n - 1] * base + a.z[n - 2]);int norm = base / (firstDigit + 1);a *= norm;a *= norm;while (a.z.empty() || a.z.size() % 2 == 1)a.z.push_back(0);bigint r = (long long) a.z[n - 1] * base + a.z[n - 2];firstDigit = (int) sqrt((double) a.z[n - 1] * base + a.z[n - 2]);int q = firstDigit;bigint res;for (int j = n / 2 - 1; j >= 0; j--) {for (; ; --q) {bigint r1 = (r - (res * 2 * base + q) * q) * base * base + (j > 0 ? (long long) a.z[2 * j - 1] * base + a.z[2 * j - 2] : 0);if (r1 >= 0) {r = r1;break;}}res *= base;res += q;if (j > 0) {int d1 = res.z.size() + 2 < r.z.size() ? r.z[res.z.size() + 2] : 0;int d2 = res.z.size() + 1 < r.z.size() ? r.z[res.z.size() + 1] : 0;int d3 = res.z.size() < r.z.size() ? r.z[res.z.size()] : 0;q = ((long long) d1 * base * base + (long long) d2 * base + d3) / (firstDigit * 2);}}res.trim();return res / norm;}bigint operator/(const bigint &v) const {return divmod(*this, v).first;}bigint operator%(const bigint &v) const {return divmod(*this, v).second;}void operator/=(int v) {if (v < 0)sign = -sign, v = -v;for (int i = (int) z.size() - 1, rem = 0; i >= 0; --i) {long long cur = z[i] + rem * (long long) base;z[i] = (int) (cur / v);rem = (int) (cur % v);}trim();}bigint operator/(int v) const {bigint res = *this;res /= v;return res;}int operator%(int v) const {if (v < 0)v = -v;int m = 0;for (int i = z.size() - 1; i >= 0; --i)m = (z[i] + m * (long long) base) % v;return m * sign;}void operator+=(const bigint &v) {*this = *this + v;}void operator-=(const bigint &v) {*this = *this - v;}void operator*=(const bigint &v) {*this = *this * v;}void operator/=(const bigint &v) {*this = *this / v;}bool operator<(const bigint &v) const {if (sign != v.sign)return sign < v.sign;if (z.size() != v.z.size())return z.size() * sign < v.z.size() * v.sign;for (int i = z.size() - 1; i >= 0; i--)if (z[i] != v.z[i])return z[i] * sign < v.z[i] * sign;return false;}bool operator>(const bigint &v) const {return v < *this;}bool operator<=(const bigint &v) const {return !(v < *this);}bool operator>=(const bigint &v) const {return !(*this < v);}bool operator==(const bigint &v) const {return !(*this < v) && !(v < *this);}bool operator!=(const bigint &v) const {return *this < v || v < *this;}void trim() {while (!z.empty() && z.back() == 0)z.pop_back();if (z.empty())sign = 1;}bool isZero() const {return z.empty() || (z.size() == 1 && !z[0]);}bigint operator-() const {bigint res = *this;res.sign = -sign;return res;}bigint abs() const {bigint res = *this;res.sign *= res.sign;return res;}long long longValue() const {long long res = 0;for (int i = z.size() - 1; i >= 0; i--)res = res * base + z[i];return res * sign;}friend bigint gcd(const bigint &a, const bigint &b) {return b.isZero() ? a : gcd(b, a % b);}friend bigint lcm(const bigint &a, const bigint &b) {return a / gcd(a, b) * b;}void read(const string &s) {sign = 1;z.clear();int pos = 0;while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) {if (s[pos] == '-')sign = -sign;++pos;}for (int i = s.size() - 1; i >= pos; i -= base_digits) {int x = 0;for (int j = max(pos, i - base_digits + 1); j <= i; j++)x = x * 10 + s[j] - '0';z.push_back(x);}trim();}friend istream& operator>>(istream &stream, bigint &v) {string s;stream >> s;v.read(s);return stream;}friend ostream& operator<<(ostream &stream, const bigint &v) {if (v.sign == -1)stream << '-';stream << (v.z.empty() ? 0 : v.z.back());for (int i = (int) v.z.size() - 2; i >= 0; --i)stream << setw(base_digits) << setfill('0') << v.z[i];return stream;}static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {vector<long long> p(max(old_digits, new_digits) + 1);p[0] = 1;for (int i = 1; i < (int) p.size(); i++)p[i] = p[i - 1] * 10;vector<int> res;long long cur = 0;int cur_digits = 0;for (int i = 0; i < (int) a.size(); i++) {cur += a[i] * p[cur_digits];cur_digits += old_digits;while (cur_digits >= new_digits) {res.push_back(int(cur % p[new_digits]));cur /= p[new_digits];cur_digits -= new_digits;}}res.push_back((int) cur);while (!res.empty() && res.back() == 0)res.pop_back();return res;}typedef vector<long long> vll;static vll karatsubaMultiply(const vll &a, const vll &b) {int n = a.size();vll res(n + n);if (n <= 32) {for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)res[i + j] += a[i] * b[j];return res;}int k = n >> 1;vll a1(a.begin(), a.begin() + k);vll a2(a.begin() + k, a.end());vll b1(b.begin(), b.begin() + k);vll b2(b.begin() + k, b.end());vll a1b1 = karatsubaMultiply(a1, b1);vll a2b2 = karatsubaMultiply(a2, b2);for (int i = 0; i < k; i++)a2[i] += a1[i];for (int i = 0; i < k; i++)b2[i] += b1[i];vll r = karatsubaMultiply(a2, b2);for (int i = 0; i < (int) a1b1.size(); i++)r[i] -= a1b1[i];for (int i = 0; i < (int) a2b2.size(); i++)r[i] -= a2b2[i];for (int i = 0; i < (int) r.size(); i++)res[i + k] += r[i];for (int i = 0; i < (int) a1b1.size(); i++)res[i] += a1b1[i];for (int i = 0; i < (int) a2b2.size(); i++)res[i + n] += a2b2[i];return res;}bigint operator*(const bigint &v) const {vector<int> a6 = convert_base(this->z, base_digits, 6);vector<int> b6 = convert_base(v.z, base_digits, 6);vll a(a6.begin(), a6.end());vll b(b6.begin(), b6.end());while (a.size() < b.size())a.push_back(0);while (b.size() < a.size())b.push_back(0);while (a.size() & (a.size() - 1))a.push_back(0), b.push_back(0);vll c = karatsubaMultiply(a, b);bigint res;res.sign = sign * v.sign;for (int i = 0, carry = 0; i < (int) c.size(); i++) {long long cur = c[i] + carry;res.z.push_back((int) (cur % 1000000));carry = (int) (cur / 1000000);}res.z = convert_base(res.z, 6, base_digits);res.trim();return res;}
}ans;ll a[maxn],num[maxn],n,ky;
ll tot,cal,del,cnt[maxn];
int main(){scanf("%lld",&n);for(int i=1;i<=n;i++){scanf("%lld",a+i);num[i]=a[i];}sort(num+1,num+n+1);ky=unique(num+1,num+n+1)-num-1;ans=0;for(int i=1;i<=n;cnt[a[i]]++,tot+=num[a[i]],i++){a[i]=lower_bound(num+1,num+ky+1,a[i])-num;ans+=num[a[i]]*(ll)(i-1);ans-=tot;if(num[a[i]+1]==num[a[i]]+1) ans+=cnt[a[i]+1];if(num[a[i]-1]==num[a[i]]-1) ans-=cnt[a[i]-1];}cout<<ans<<endl;return 0;
}

  

转载于:https://www.cnblogs.com/JYYHH/p/8422709.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/466723.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

据悉,深圳某工程师沦为C语言笔试枪手

事情是这样的&#xff0c;昨晚晚上&#xff0c;有个网友发消息给我&#xff0c;说他有几道C语言笔试题不会写&#xff0c;所以&#xff0c;就出现了解题的这一幕。文章中&#xff0c;我只讲解了一部分&#xff0c;有一些题目觉得没必要讲&#xff0c;然后我在pdf上做了注释&…

大数据工具使用——安装Hadoop(多台服务器)和Hive、Hbase

1.配置环境版本 资料上传百度云&#xff0c;自取&#xff1a;链接&#xff1a;https://pan.baidu.com/s/1evVp5Zk0_X7VdjKlHGkYCw 提取码&#xff1a;ypti 复制这段内容后打开百度网盘手机App&#xff0c;操作更方便哦 &#xff08;之前安装的是apache版本的Hadoop2.6.4,在启…

[转] 关于 WCF 中数据压缩的几篇文章

原文:http://www.cnblogs.com/jiabao/archive/2007/12/04/982534.html在.net3.0出现以前我们进行分布式开发式有两个选择一个是webservice&#xff0c;另一个是remoting&#xff1b;在早期的项目中&#xff0c;比较喜欢remoting&#xff0c;因为remoting可控性好&#xff0c;也…

聊一聊我自己的从业经历和感悟

嵌入式学习&#xff0c;是一个很枯燥的过程&#xff0c;我记得在学习三极管的时候&#xff0c;我真的对这个东西一点感觉都没有&#xff0c;我知道三极管可以放大&#xff0c;然后电子从一个地方去到了另一个地方&#xff0c;然后就触发了某个开关&#xff0c;就发了大水。然后…

大数据——sqoop操作mysql和hive导出导入数据

1.sqoop安装 &#xff08;1&#xff09;下载CDH版本的sqoop &#xff08;2&#xff09;解压并进行环境配置 环境变量为&#xff1a; export SQOOP_HOME/home/sqoop-1.4.6-cdh5.15.1 export PATH$PATH:$SQOOP_HOME/bin 在sqoop安装目录/conf/下&#xff1a; #新建sqoop-en…

年终了,肿一下

也没有没有跟大家好好唠唠&#xff0c;一年时间过得飞快&#xff0c;我还记得那时候从老家开车来深圳&#xff0c;一路狂奔&#xff0c;在广西入境广东的时候&#xff0c;因为疫情排查&#xff0c;我们在那里堵了3个小时&#xff0c;还因为路途颠簸&#xff0c;车子一起一停&am…

大数据——spark安装部署和python环境配置

需要配置多台服务器&#xff0c;实验环境&#xff1a;master和data两台服务器&#xff0c;已安装好hadoop&#xff0c;可参考前文&#xff01;&#xff01;&#xff01; 1.spark安装 master安装 &#xff08;1&#xff09;下载scala和spark &#xff08;2&#xff09;解压并…

2021年,这是你们收到的第一份礼物

一、 前言大家好&#xff0c;2020年就要过去了&#xff0c;这一年来&#xff0c;感谢大家对公众号的支持&#xff0c;但是感谢不能停留在嘴上&#xff0c;所以&#xff0c;这次邀请了正点原子赞助。一起给大家送点礼品&#xff01;作为一名 电子/嵌入式 人&#xff0c;正点原子…

深入理解Linux内核进程上下文切换

在原作者基础上修改了些文字描述&#xff0c;让文章更加通俗易懂作者简介韩传华&#xff0c;就职于南京大鱼半导体有限公司&#xff0c;主要从事linux相关系统软件开发工作&#xff0c;负责Soc芯片BringUp及系统软件开发&#xff0c;乐于分享喜欢学习&#xff0c;喜欢专研Linux…

Linux C高级编程——网络编程基础(1)

Linux高级编程——BSD socket的网络编程 宗旨&#xff1a;技术的学习是有限的&#xff0c;分享的精神是无限的。 一网络通信基础 TCP/IP协议簇基础&#xff1a;之所以称TCP/IP是一个协议簇&#xff0c;是因为TCP/IP包含TCP 、IP、UDP、ICMP等多种协议。下图是OSI模型与TCP/IP模…

使用SQLDMO中“接口SQLDMO.Namelist 的 QueryInterface 失败”异常的解决方法

SQLDMO&#xff08;SQL Distributed Management Objects&#xff0c;SQL分布式管理对象&#xff09;&#xff0c;它封装 Microsoft SQL Server 数据库中的对象。它允许我们通过COM对象&#xff0c;对SQLServer进行管理。SQLDMO对象来自SQLDMO.dll。因为SQLDMO.dll是一个COM对象…

Linux C高级编程——网络编程之以太网(2)

Linux网络编程——以太网 宗旨&#xff1a;技术的学习是有限的&#xff0c;分享的精神是无限的。 1、以太网帧格式 源地址和目的地址是指网卡的硬件地址&#xff08;也叫MAC地址&#xff09;&#xff0c;长度是48位&#xff0c;是在网卡出厂时固化的。用ifconfig命令查看&#…

Tomcat 打开jmx

jmx 配置后可以通过windows java客户端自带的jconsole.exe配置登陆&#xff0c;直观的查看jvm的情况及系统的各项指标&#xff1b; 一、配置linux下tomcat的jmx 具体配置如下&#xff0c;如果生产环境可以适当坐下调整。 # head /usr/local/tomcat/bin/catalina.sh #!/bin/shC…

我不是编译器专家

这是王垠发表的一篇文章&#xff0c;转给大家看看&#xff0c;希望有些收获王垠是谁&#xff1f;王垠&#xff0c;四川大学97级本科毕业&#xff0c;保送到清华大学计算机系直博。期间曾在清华大学计算机系软件所就读&#xff0c;主要进行集成电路布线算法的研究。在此期间&…

自定义实体类简介

< DOCTYPE html PUBLIC -WCDTD XHTML TransitionalEN httpwwwworgTRxhtmlDTDxhtml-transitionaldtd> 摘要&#xff1a;有些情况下&#xff0c;非类型化的 DataSet 可能并非数据操作的最佳解决方案。本指南的目的就是探讨 DataSet 的一种替代解决方案&#xff0c;即&#…

Linux C高级编程——网络编程之TCP(3)

Linux网络编程&#xff08;三&#xff09;——TCP 宗旨&#xff1a;技术的学习是有限的&#xff0c;分享的精神是无限的。 1、TCP段格式 和UDP协议一样也有源端口号和目的端口号&#xff0c;通讯的双方由IP地址和端口号标识。32位序号、32位确认序号、窗口大小。4位首部长度和I…

机器学习——超参数调优

超参数是在开始学习过程之前设置值的参数&#xff0c;而不是通过训练得到的参数数据。超参数可以分为两种类型&#xff1a;定义模型及结构本身的参数&#xff0c;目标函数与与优化算法所需的参数&#xff0c;前者用于训练和预测阶段&#xff0c;后者用于训练阶段。 在实战过程…

单片机的Bootloader,可以实现用户轻松升级程序

去某新能源大厂出了一次差&#xff0c;这次出差是为了升级程序解决Bug&#xff0c;需要给单片机重新烧录.hex文件&#xff0c;用户已经将产品封装起来&#xff0c;无法开盖&#xff0c;只能使用CAN总线来更新程序&#xff0c;用Bootloader实现。其实就是通过上位机把.bin/hex文…

Linux C高级编程——网络编程之UDP(4)

Linux网络编程——UDP 宗旨&#xff1a;技术的学习是有限的&#xff0c;分享的精神是无限的。 下面分析一帧基于UDP的TFTP协议帧。 以太网首部 0000: 00 05 5d 67 d0 b1 00 05 5d 61 58 a8 08 00 IP首部0000: 45 00 0010: 00 53 93 25 00 00 80 11 25 ec c0 a8 00 37 c0 a8…

Android串口通信apk源码

1、SerialPortHelper「Android串口通信」介绍原项目地址https://github.com/freyskill/SerialPortHelperAndroid串口通讯助手可以用于需要使用串口通信的Android外设&#xff0c;该库有如下特点&#xff1a;1、串口通信部分使用C实现&#xff0c;在笔者接触的部分设备上实测&am…