Educational Codeforces Round 34 (Rated for Div. 2) D. Almost Difference[数据结构]

题意:求一个数列中所有的绝对值差大于2的数,并用后面的数字减前面的数字的加和。

分析:可以用树状数组每次找前面的差值大于2的数,也可以直接每次加前面所有的数字,再减去差值为1的数字。题目最坑爹的是答案也许会爆long long,可以用long double或者使用unsigned long long手动模拟符号位,或者用python

C++代码:

 1 #define _CRT_SECURE_NO_DEPRECATE
 2 #pragma comment(linker, "/STACK:102400000,102400000")
 3 #include<iostream>
 4 #include<cstdio>
 5 #include<fstream>
 6 #include<iomanip>
 7 #include<algorithm>
 8 #include<cmath>
 9 #include<deque>
10 #include<vector>
11 #include<bitset>
12 #include<queue>
13 #include<string>
14 #include<cstring>
15 #include<map>
16 #include<stack>
17 #include<set>
18 #include<functional>
19 #define pii pair<int, ll>
20 #define mod 1000000007
21 #define mp make_pair
22 #define pi acos(-1)
23 #define eps 0.00000001
24 #define mst(a,i) memset(a,i,sizeof(a))
25 #define all(n) n.begin(),n.end()
26 #define lson(x) ((x<<1))
27 #define rson(x) ((x<<1)|1)
28 #define inf 0x3f3f3f3f
29 typedef long long ll;
30 typedef unsigned long long ull;
31 using namespace std;
32 const int maxn = 2e5 + 50;
33 map<ll, ll>a;
34 int main()
35 {
36     ios::sync_with_stdio(false);
37     cin.tie(0); cout.tie(0);
38     ll i, j, k, m, n;
39     cin >> n;
40     long double ans = 0;
41     ll sum = 0;
42     for (int i = 1; i <= n; ++i)
43     {
44         cin >> m;
45         sum += m;
46         ans += i*m - sum;
47         ans -= a[m - 1];
48         ans += a[m + 1];
49         a[m]++;
50     }
51     cout << setiosflags(ios::fixed) << setprecision(0) << ans << endl;
52     return 0;
53 }

python:

 1 n=input()
 2 mp = list(map(int,input().split()))
 3 sum = 0
 4 ans = 0
 5 cnt = 0
 6 tt = dict()
 7 for i in mp:
 8     sum+=i
 9     cnt=cnt+1
10     ans+=cnt*i-sum
11     if i-1 in tt:
12         ans-=tt[i-1]
13     if i+1 in tt:
14         ans+=tt[i+1]
15     if i not in tt:
16         tt[i]=0
17     tt[i]=tt[i]+1
18 print(ans)

树状数组错误代码(答案爆long long)

  1 #define _CRT_SECURE_NO_DEPRECATE
  2 #pragma comment(linker, "/STACK:102400000,102400000")
  3 #include<iostream>
  4 #include<cstdio>
  5 #include<fstream>
  6 #include<iomanip>
  7 #include<algorithm>
  8 #include<cmath>
  9 #include<deque>
 10 #include<vector>
 11 #include<bitset>
 12 #include<queue>
 13 #include<string>
 14 #include<cstring>
 15 #include<map>
 16 #include<stack>
 17 #include<set>
 18 #include<functional>
 19 #define pii pair<int, ll>
 20 #define mod 1000000007
 21 #define mp make_pair
 22 #define pi acos(-1)
 23 #define eps 0.00000001
 24 #define mst(a,i) memset(a,i,sizeof(a))
 25 #define all(n) n.begin(),n.end()
 26 #define lson(x) ((x<<1))
 27 #define rson(x) ((x<<1)|1)
 28 #define inf 0x3f3f3f3f
 29 typedef long long ll;
 30 typedef unsigned long long ull;
 31 using namespace std;
 32 const int maxn = 2e5 + 50;
 33 ll ori[maxn];
 34 int n;
 35 ll t[maxn];
 36
 37 int Lowbit(int x)
 38 {
 39     return x&(-x);
 40 }
 41 void update(ll i, ll x)
 42 {
 43     while (i <= n)
 44     {
 45         t[i] += x;
 46         i += Lowbit(i);
 47     }
 48 }
 49 ll sum(int x)
 50 {
 51     ll sum = 0;
 52     while (x>0)
 53     {
 54         sum += t[x];
 55         x -= Lowbit(x);
 56     }
 57     return sum;
 58 }
 59
 60 map<ll, ll>id;
 61 ll orin[maxn];
 62 set<ll>kp;
 63 int main()
 64 {
 65     ios::sync_with_stdio(false);
 66     cin.tie(0); cout.tie(0);
 67     int i, j, k, m;
 68     cin >> n;
 69     int cnt = 1;
 70     for (int i = 1; i <= n; ++i)
 71     {
 72         cin >> ori[i];
 73         kp.insert(ori[i]);
 74     }
 75     for (auto it : kp)
 76     {
 77         orin[cnt] = it;
 78         id[it] = cnt++;
 79     }
 80     for (int i = 1; i <= n; ++i)
 81     {
 82         int iid = id[ori[i]];
 83         update(id[ori[i]], 1);
 84     }
 85     ll fans = 0;
 86     for (int i = 1; i <= n; ++i)
 87     {
 88         auto it = id.lower_bound(ori[i] + 2);
 89         if (it != id.end())
 90         {
 91             auto ta = sum(n);
 92             auto tb = sum(it->second - 1);
 93             ll cnt = ta - tb;
 94             fans -= cnt*ori[i];
 95         }
 96         it = id.lower_bound(ori[i] - 1);
 97         if (it != id.begin())
 98         {
 99             it--;
100             ll ta = sum(it->second);
101             fans -= ta*ori[i];
102         }
103         update(id[ori[i]], -1);
104     }
105     mst(t, 0);
106     for (int i = 1; i <= n; ++i)
107     {
108         int iid = id[ori[i]];
109         update(id[ori[i]], 1);
110     }
111
112     for (int i = n; i>= 1; --i)
113     {
114         auto it = id.lower_bound(ori[i] + 2);
115         if (it != id.end())
116         {
117             auto ta = sum(n);
118             auto tb = sum(it->second - 1);
119             ll cnt = ta - tb;
120             fans += cnt*ori[i];
121         }
122         it = id.lower_bound(ori[i] - 1);
123         if (it != id.begin())
124         {
125             it--;
126             ll ta = sum(it->second);
127             fans += ta*ori[i];
128         }
129         update(id[ori[i]], -1);
130     }
131     cout << fans << endl;
132     return 0;
133 }

原文地址:https://www.cnblogs.com/Meternal/p/8119807.html

时间: 2024-08-30 17:39:48

Educational Codeforces Round 34 (Rated for Div. 2) D. Almost Difference[数据结构]的相关文章

Educational Codeforces Round 34 (Rated for Div. 2) B题【打怪模拟】

B. The Modcrab Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab. After two hours of playing the game Vova has tracked the monster and analyzed its tactics

Educational Codeforces Round 34 (Rated for Div. 2) ABC

A. Hungry Student Problem Ivan's classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken. CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large o

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm