Educational Codeforces Round 64 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 【二分思想】的更多相关文章
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- Educational Codeforces Round 64(ECR64)
Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- 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 ...
- Educational Codeforces Round 64 选做
感觉这场比赛题目质量挺高(A 全场最佳),难度也不小.虽然 unr 后就懒得打了. A. Inscribed Figures 题意 给你若干个图形,每个图形为三角形.圆形或正方形,第 \(i\) 个图 ...
- Educational Codeforces Round 64 -C(二分)
题目链接:https://codeforces.com/contest/1156/problem/C 题意:给出n个数和整形数z,定义一对数为差>=z的数,且每个数最多和一个数组成对,求最多有多 ...
- 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, ...
- 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 ...
随机推荐
- 多线程(四) AQS底层原理分析
J.U.C 简介 Java.util.concurrent 是在并发编程中比较常用的工具类,里面包含很多用来在并发 场景中使用的组件.比如线程池.阻塞队列.计时器.同步器.并发集合等等.并 发包的作者 ...
- Docker项目demo
Docker数据持久化 4.1 Volume (1)创建mysql数据库的container (2)查看volume (3)具体查看该volume docker volume inspect 485 ...
- keras自定义网络层
在深度学习领域,Keras是一个高度封装的库并被广泛应用,可以通过调用其内置网络模块(各种网络层)实现针对性的模型结构:当所需要的网络层功能不被包含时,则需要通过自定义网络层或模型实现. 如何在ker ...
- css sticky & 吸顶效果
css sticky & 吸顶效果 demo https://codepen.io/xgqfrms/pen/PoqyVYz css position sticky not working ht ...
- SEO & JSON-LD & structured-data
SEO & JSON-LD & structured-data script type="application/ld+json" script type=&quo ...
- CSS font-weight all in one
CSS font-weight all in one font-weight: bolder: 没毛病呀! /* 关键字值 */ font-weight: normal; font-weight: b ...
- ES-Next & ES7 @decorator
ES-Next & ES7 @decorator @decorator https://tc39.github.io/proposal-decorators/#sec-syntax https ...
- SVG & Sprite & symbol & use
SVG & Sprite & symbol & use https://www.zhangxinxu.com/sp/svgo/ https://www.zhangxinxu.c ...
- 11月16日NGK公链第13期官方快讯!
- jetty的jndi
jetty的jndi和tomcat的用法 tomcat的jndi是内置的,在web.xml文件里直接默认支持的,所有web项目可以直接使用 <resources> <!-- < ...