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 }

原文地址:https://www.cnblogs.com/cmyg/p/9520945.html

时间: 2024-07-30 11:52:36

E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)的相关文章

CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #include <bits/stdc++.h> using namespace std; const int N = 2e5+5; int a[N], b[N], c[N], n; int aa[N], bb[N]; bool cmp1(int x, int y) { return a[x] > a[y

Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function(预处理+二分)

题目链接:点我点我 题意:公式:,给出n个数,从a[1]到a[n],和m个数(b数组),然后从b数组里挑出连续的n个数(也就m-n+1中选择),按公式计算,使得f最小, 还有q次对a数组的操作(某个区间增加值,减少值),求出最小值. 题解:显然对a数组的处理非常简单,一开始确定一定值,然后update的时候,判断一下奇偶性质就可以直接加了(前一项正后一项一定是负的,可以抵消). 然后就是b数组的处理了,一开始没处理好,一直在这边卡超时.先把b数组一项一项正负不同的加进去,然后再进行处理,得到c数

Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

题目:http://codeforces.com/problemset/problem/862/D 题意:交互题,询问15次以内Hamming distance,输出一个二进制串里任意一个0或1的位置 题解:极简单的二分,从最后一位先判断一个,然后二分 根据上次和本次的距离差是否等于二分长度判断在左端还是右端有需要寻找的值寻找另一个. 1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,10

D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/D 询问题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <se

Codeforces Round #435 (Div. 2) c+d

C:给n和k要求,找出n个不同的数,使得亦或起来等于k 可以先预处理从1到1e5,找亦或起来等于(11111111111111111)(二进制)的所有对数,然后四个一起亦或就是0了,再和k亦或 可以看出要分四种情况讨论,对于n%4=p的情况,应该找到p-1个不同的数亦或起来等于0,可以小范围的p-1重循环搜索,对于n%4==2&&x==0的情况要注意特判,可以用6重循环,每层不超过10 代码过于复杂了,应该可以简化一下 #include<bits/stdc++.h> #defi

Codeforces Round #619 (Div. 2) Ayoub&#39;s function

Ayoub thinks that he is a very smart person, so he created a function f(s)f(s) , where ss is a binary string (a string which contains only symbols "0" and "1"). The function f(s)f(s) is equal to the number of substrings in the string s

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When