E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/E

二分答案

一个数与数组中的哪个数最接近:

先对数组中的数排序,然后lower_bound

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <time.h>
 6 #include <string>
 7 #include <set>
 8 #include <map>
 9 #include <list>
10 #include <stack>
11 #include <queue>
12 #include <vector>
13 #include <bitset>
14 #include <ext/rope>
15 #include <algorithm>
16 #include <iostream>
17 using namespace std;
18 #define ll long long
19 #define minv 1e-6
20 #define inf 1e18
21 #define pi 3.1415926536
22 #define E  2.7182818284
23 const ll mod=1e9+7;//998244353
24 const int maxn=1e5+10;
25 
26 ll b[maxn],f[maxn];
27 
28 int main()
29 {
30     int n,m,q,g=0,i,l,r;
31     ll a,tot=0,sum=0,v;
32     scanf("%d%d%d",&n,&m,&q);
33     for (i=1;i<=n;i++)
34     {
35         scanf("%lld",&a);
36         if (i & 1)
37             sum+=a;
38         else
39             sum-=a;
40     }
41     for (i=1;i<=m;i++)
42     {
43         scanf("%lld",&b[i]);
44         if (i & 1)
45             tot+=b[i];
46         else
47             tot-=b[i];
48         if (i>=n)
49         {
50             g++;
51             if ((i-n) & 1)
52                 f[g]=-tot;
53             else
54                 f[g]=tot;
55             if ((i-n+1) & 1)
56                 tot-=b[i-n+1];
57             else
58                 tot+=b[i-n+1];
59         }
60     }
61     sort(f+1,f+g+1);
62 
63         i=lower_bound(f+1,f+g+1,sum)-f;
64         v=inf;
65         if (i-1>=1 && i-1<=g)
66             v=min(v,abs(sum-f[i-1]));
67         if (i>=1 && i<=g)
68             v=min(v,abs(sum-f[i]));
69         if (i+1>=1 && i+1<=g)
70             v=min(v,abs(sum-f[i+1]));
71         printf("%lld\n",v);
72 
73     while (q--)
74     {
75         scanf("%d%d%lld",&l,&r,&a);
76         if ((r-l)%2==0)
77         {
78             if (l & 1)
79                 sum+=a;
80             else
81                 sum-=a;
82         }
83 
84         i=lower_bound(f+1,f+g+1,sum)-f;
85         v=inf;
86         if (i-1>=1 && i-1<=g)
87             v=min(v,abs(sum-f[i-1]));
88         if (i>=1 && i<=g)
89             v=min(v,abs(sum-f[i]));
90         if (i+1>=1 && i+1<=g)
91             v=min(v,abs(sum-f[i+1]));
92         printf("%lld\n",v);
93     }
94     return 0;
95 }

 

posted @ 2018-08-22 22:51  congmingyige  阅读(146)  评论(0编辑  收藏  举报