CF 990B. Micro-World【数组操作/贪心/STL/二分搜索】
【链接】:CF
【题意】:对任意一个数a[i] ,可以对任意 满足 i != j 且 a[i] > a[j] && a[i] <= a[j] +k 的 a[j] 可以被删掉,求使最终剩下的个数最少。
【分析】:扫一遍,二分搜索合法的。
【代码】:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int maxn = 1e6+ 100;
const int maxm = 100;
const double PI = acos(-1.0);
const double eps = 1e-8;
//const int dx[] = {-1,1,0,0,1,1,-1,-1};
//const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
int n,k;
int a[maxn], b[maxn];
int main()
{
while(cin>>n>>k)
{
int cnt=0;
for(int i=0;i<n;i++) cin>>a[i];/*
for(int i=0;i<n;i++)
{
cout<<i<<" ";
}
cout<<endl;*/
sort(a,a+n,greater<int>());/*
for(int i=0;i<n;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;*/
for(int i=0;i<n;i++)
{
int p = lower_bound(a,a+n,a[i]+k,greater<int>())-a; //第一个 <= (a[i]+k) 的位置
//cout<<"a[i]+k="<<a[i]+k<<" p="<<p<<" a[p]="<<a[p]<<" a[i]="<<a[i];
if(a[p]>a[i])
{
cnt++;
//cout<<" YES";
}
//cout<<endl;
}
cout<<n-cnt<<endl;
}
}
/*
7 1
101 53 42 102 101 55 54
42 53 54 55 101 101 102
outputCopy
3
42 55 102
inputCopy
6 5
20 15 10 15 20 25
25
outputCopy
1
inputCopy
7 1000000
1 1 1 1 1 1 1
outputCopy
7
7 1
101 53 42 102 101 55 54
101 53 42 102 101 55 54
102 101 101 55 54 53 42
a[i]+k=103 p=0 a[p]=102 a[i]=102
a[i]+k=102 p=0 a[p]=102 a[i]=101 YES
a[i]+k=102 p=0 a[p]=102 a[i]=101 YES
a[i]+k=56 p=3 a[p]=55 a[i]=55
a[i]+k=55 p=3 a[p]=55 a[i]=54 YES
a[i]+k=54 p=4 a[p]=54 a[i]=53 YES
a[i]+k=43 p=6 a[p]=42 a[i]=42
*/
[模拟]
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int maxn = 1e6+ 100;
const int maxm = 100;
const double PI = acos(-1.0);
const double eps = 1e-8;
//const int dx[] = {-1,1,0,0,1,1,-1,-1};
//const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
int n,k,j;
int a[maxn], b[maxn];
int main()
{
while(cin>>n>>k)
{
int cnt, j=0;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
cnt=n;
for(int i=0;i<n;i++)
{
while(a[j]<a[i])
{
if(a[j]+k>=a[i]) cnt--;
j++;
}
}
cout<<cnt<<endl;
}
}
CF 990B. Micro-World【数组操作/贪心/STL/二分搜索】的更多相关文章
- NDK(19)简单示例:ndk调用java基本方法、数组;使用stl、访问设备
一.ndk调用java类示例 1,调用基本方法 /* * Class: com_example_ndksample_MainActivity * Method: ndkFindJavaClass * ...
- Javascript数组操作
使用JS也算有段时日,然对于数组的使用,总局限于很初级水平,且每每使用总要查下API,或者写个小Demo测试下才算放心,一来二去,浪费不少时间:思虑下,堪能如此继续之?当狠心深学下方是正道. 原文链接 ...
- JavaScript jQuery 中定义数组与操作及jquery数组操作
首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...
- php数组操作集锦- 掌握了数组操作, 也就掌握了php
参考下面的文章, 是很好的: http://www.cnblogs.com/staven/p/5142515.html http://pcwanli.blog.163.com/blog/static/ ...
- JavaScript 数组操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JavaScript中数组操作常用方法
JavaScript中数组操作常用方法 1.检测数组 1)检测对象是否为数组,使用instanceof 操作符 if(value instanceof Array) { //对数组执行某些操作 } 2 ...
- php 常用数组操作
php常用的数组操作函数,包括数组的赋值.拆分.合并.计算.添加.删除.查询.判断.排序等 array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 <?p ...
- 005-Scala数组操作实战详解
005-Scala数组操作实战详解 Worksheet的使用 交互式命令执行平台 记得每次要保存才会出相应的结果 数组的基本操作 数组的下标是从0开始和Tuple不同 缓冲数组ArrayBuffer( ...
- JavaScript中常见的数组操作函数及用法
JavaScript中常见的数组操作函数及用法 昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下 ...
随机推荐
- Elasticsearch1.x 和Elasticsearch2.x 拼音分词插件lc-pinyin安装教程
Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文.拼音.同义词搜索 https://blog.csdn.net/chennanymy/article/category/60 ...
- SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase
sqlserver 插入数据的时候 插入失败,报错内容为 “SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, ...
- Luogu3731 HAOI2017新型城市化(二分图匹配+强连通分量)
将未建立贸易关系看成连一条边,那么这显然是个二分图.最大城市群即最大独立集,也即n-最大匹配.现在要求的就是删哪些边会使最大匹配减少,也即求哪些边一定在最大匹配中. 首先范围有点大,当然是跑个dini ...
- 【算法】Prüfer编码 —— HNOI2004树的计数
的确,如果不知道这个编码的话的确是一脸懵逼.在这里放一篇认为讲的很详细的 BLOG,有关于编码的方式 & 扩展在里面都有所提及. 欢迎点此进入 --> 大佬的博客 在这里主要想推导一下最 ...
- [LG4890]Never·island DP
---题面--- 题解: 感到此题非常的神奇...看了大佬的题解才懂的. 首先建模: 先把所有队伍的出去回来时间都放在一个数组里,然后排序,从左到右扫一边,给每个队伍都建一个带权点,进行如下操作: ( ...
- Windows查看进程CMD命令和终止进程CMD命令
将小米路由器3刷机成openwrt的方法,请参考上篇文章< 家庭宽带多运营商接入方案>这里介绍怎么在已经刷成openwrt系统的小米路由器3上安装私有云nextcloud openwrt开 ...
- HDOJ.2501 Tiling_easy version
Tiling_easy version Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【NOIP模拟赛】beautiful 乱搞(平衡树)+ST
biubiu~~~ 我用平衡树处理的这道题,然而这种方法还是要看评测姬..... 正解是乱搞....就是枚举每一位数作为中位数,比他小的看做-1比他大的看做1,那么我们从一开始就有了一个绵延的山,我们 ...
- jQuery源码分析笔记
jquery-2.0.3.js版本源码分析 (function(){ (21,94) 定义了一些变量和函数 jQuery = function(){}; (96,283) 给JQ对象,添加一些方法 ...
- bzoj 2426 【HAOI2010】工程选址 贪心
[HAOI2010]工厂选址 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 447 Solved: 308[Submit][Status][Disc ...