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中常见的数组操作函数及用法总结一下 ...
随机推荐
- Hibernate基本演示
保存一个对象到数据库中 目录结构 hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hi ...
- JS格式化 /Date(xxxxxx)/的日期类型
//用来转换/Date(xxxxxx)/类型的JSON日期为要求的日期格式字符串String.prototype._formatJsonDate = function (format) { var s ...
- [UOJ #52]【UR #4】元旦激光炮
题目大意:交互题,给你三个有序数组,长度分别为$n\_a,n\_b,n\_c$,都不超过$10^5$.三个函数$get\_a(i),get\_b(i),get\_c(i)$,分别返回$a_i,b_i, ...
- [Leetcode] unique paths 独特路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- BZOJ3456 城市规划 【多项式求逆】
题目链接 BZOJ3456 题解 之前我们用分治\(ntt\)在\(O(nlog^2n)\)的复杂度下做了这题,今天我们使用多项式求逆 设\(f_n\)表示\(n\)个点带标号无向连通图数 设\(g_ ...
- CodeForces 167B - Wizards and Huge Prize 期望概率dp
初步分析:把赢了的巡回赛的a值加起来就是最后的剩余空间 这个明显的是状态转移的dp啊,然而他的状态比较骚是个数组,表示剩余空间,f(i,j,b),i表示比到第几场,j表示赢了几场,b就是里面的核心状态 ...
- poj 2378 Tree Cutting 树形dp
After Farmer John realized that Bessie had installed a "tree-shaped" network among his N ( ...
- 手动安装GCC
01sunxiaoqiang的博客 Centos离线手动安装gcc.g++教程 转载 2016-11-06 17:35:18 标签:linux应用笔记 在安装LINUX系统的时候很可能会没有安装gcc ...
- hive Illegal Operation state transition from CLOSED to ERROR的处理
异常堆栈如下: 2015-11-24 16:49:11,495 ERROR org.apache.hive.service.cli.operation.Operation: Error running ...
- 使用MAT分析内存泄露
使用MAT分析内存泄露 对于大型服务端应用程序来说,有些内存泄露问题很难在测试阶段发现,此时就需要分析JVM Heap Dump文件来找出问题.随着单机内存越来越大,应用heap也开得越来越大,动辄十 ...