码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-26 20:56:55      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:def   pre   linker   span   out   name   nbsp   memset   答案   

技术分享图片

题意:求一个数列中所有的绝对值差大于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 }

 

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

标签:def   pre   linker   span   out   name   nbsp   memset   答案   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!