一 题面

  C. Match Points

二 分析

  根据题意很容易想到要去找满足条件的数,因为可以打乱输入的顺序,所以很容易想到二分。

  但是如果直接对输入的数组进行二分,如输入$a$,直接在数组里二分找$a+z$,就会出现不是最优解的情况,例如:

$4\ 8\ 9\ 12$ 其中$z = 4$

如果从第一个数直接二分那样找就会出问题。

  那么我们可以思考任意一个数组最优的解是多少?其实就是$n/2$。那么排序后,肯定可以从中间那个位置划分,后面的每个数可以找到最前面的数相对应。那么我们直接遍历一下右半部分的数组,为了找到更多的解,肯定要在满足条件的情况下,在左半部分数组中找最小的,相当于两个指针扫就可以了。

三 AC代码

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int MAXN = 2e5 + 10;
4 int Data[MAXN];
5
6 int main()
7 {
8 ios::sync_with_stdio(false);
9 int n, z, ans = 0;
10 int l, r;
11 cin >> n >> z;
12 for(int i = 0; i < n; i++)
13 cin >> Data[i];
14 sort(Data, Data + n);
15 l = 0;
16 if(n % 2 == 1)
17 r = n/2 + 1;
18 else
19 r = n/2;
20 while(r < n)
21 {
22 if(Data[l] + z <= Data[r])
23 {
24 ans++;
25 l++;
26 }
27 r++;
28 }
29 cout << ans << endl;
30 return 0;
31 }

Educational Codeforces Round 64 C. Match Points 【二分思想】的更多相关文章

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

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  2. Educational Codeforces Round 64 部分题解

    Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...

  3. Educational Codeforces Round 64(ECR64)

    Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...

  4. Educational Codeforces Round 64部分题解

    Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...

  5. Educational Codeforces Round 64 (Rated for Div. 2) A,B,C,D,E,F

    比赛链接: https://codeforces.com/contest/1156 A. Inscribed Figures 题意: 给出$n(2\leq n\leq 100)$个数,只含有1,2,3 ...

  6. Educational Codeforces Round 64 选做

    感觉这场比赛题目质量挺高(A 全场最佳),难度也不小.虽然 unr 后就懒得打了. A. Inscribed Figures 题意 给你若干个图形,每个图形为三角形.圆形或正方形,第 \(i\) 个图 ...

  7. Educational Codeforces Round 64 -C(二分)

    题目链接:https://codeforces.com/contest/1156/problem/C 题意:给出n个数和整形数z,定义一对数为差>=z的数,且每个数最多和一个数组成对,求最多有多 ...

  8. Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)

    题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n  n个数,然后求有多少个区间[l,r] 满足    a[l]+a[r]=max([l, ...

  9. Educational Codeforces Round 21 D.Array Division(二分)

    D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

随机推荐

  1. leetcode347 python

    通过维护最小堆排序,使用heapq模块 一般使用规则:创建列表 heap = [] 函 数                                                        ...

  2. leetcode5 最长回文字符串 动态规划 Manacher法

    dp 注意没有声明S不空,处理一下 o(n^2) class Solution { public: string longestPalindrome(string s) { if (s.empty() ...

  3. 读js DOM编程艺术总结

    第一章主要介绍一些历史性问题,javascript是Netcape和sun公司合作开发的. 第二章JavaScript语法: 1,数据类型:(弱类型)字符串,数值,布尔值(只有true和false,不 ...

  4. Linux Centos7发送QQ邮件

    一.关闭本机的sendmail服务或者postfix服务 #sendmial service sendmail stop chkconfig sendmail off #postfix service ...

  5. React Native for Windows + macOS

    React Native for Windows + macOS React Native 0.63 https://aka.ms/ReactNative https://microsoft.gith ...

  6. React Native & Security

    React Native & Security https://reactnative.dev/docs/security React Native blogs https://reactna ...

  7. Smashing Conf 2020

    Smashing Conf 2020 https://smashingconf.com/online-workshops/ events https://smashingconf.com/ny-202 ...

  8. Java Hipster

    Java Hipster JHipster JHipster is a development platform to quickly generate, develop, & deploy ...

  9. document.getElementById & document.querySelector

    document.getElementById & document.querySelector https://developer.mozilla.org/en-US/docs/Web/AP ...

  10. taro list render bug

    taro list render bug 列表渲染 https://taro-docs.jd.com/taro/docs/list.html not support jsx map 垃圾微信 cons ...