一 题面

  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. C# 类 (10) - 命名空间NameSpace

    NameSpace 命名空间是一系列 类型的集合,比如很多方法,很多类,集合在一个空间(myspace)里,谁想用就先 using myspace,然后直接用不using myspace的话,想用里面 ...

  3. 使用 js 实现十大排序算法: 桶排序

    使用 js 实现十大排序算法: 桶排序 桶排序 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  4. 1GB === 1000MB & 1GB === 1024MB

    1GB === 1000MB & 1GB === 1024MB 字节单位换算 1 Gigabyte = 1000 Megabytes 1 Gibibyte = 1024 Mebibytes 十 ...

  5. Linux bash shell All In One

    Linux bash shell All In One Linux https://tinylab.gitbooks.io/shellbook/content/zh/chapters/01-chapt ...

  6. parcel bug & not support normal import React & ReactDOM module

    bug report not support normal import React & ReactDOM module, why Code Sample OK import * as Rea ...

  7. qt 获取窗口句柄的线程id和进程id GetWindowThreadProcessId

    int lpdwProcessId; int id = GetWindowThreadProcessId((HWND)0x707d6, (LPDWORD)&lpdwProcessId); qD ...

  8. Redis的数据结构与应用场景

    一.Redis简介 Redis 是一个开源的使用 ANSI C 语言编写.遵守 BSD 协议.支持网络.可基于内存.分布式.可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API ...

  9. 【重榜?】.NET 6 Preview 1 开箱上手!带你尝试新版本更新!

    目录 跨平台 UI 应用 Blazor 桌面应用 System.CommandLine 其它更新 ASP.NET Core 最近 .NET 6 Preview 1 发布了,.NET 统一是此版本的核心 ...

  10. 百度 Apollo无人车平台增加传感器

    https://github.com/ApolloAuto/apollo/issues/1649 如果想加入一个新的传感器不是百度官方推荐的传感器到Apollo平台做法: First you can ...