CF 990B B. 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;
    }
}

原文地址:https://www.cnblogs.com/Roni-i/p/9215028.html

时间: 2024-08-29 16:08:32

CF 990B B. Micro-World【数组操作/贪心/STL/二分搜索】的相关文章

PHP:数组操作函数array_count_values()的实现

    PHP作为一门弱类型的脚本语言,其变量无需声明,即用即得,其数组更是与其他强类型语言相差巨大,比如PHP数组中的关联键和数值键,其中最有趣的莫过于关联键了,这是在C++或JAVA中无法见到的,而且PHP还提供诸多强大的数组操作函数,比如   array_values()//可以剥离数组中的关联键和数值键,或得有其元素的值所组成的数组 array_keys()//获得所有的关联键和数值键  利用这两个函数就可以非常方便简单的实现  array_count_values()函数  思路就是先

PHP内核探索之变量(4) - 数组操作

上一节(PHP内核探索之变量(3)- hash table),我们已经知道,数组在PHP的底层实际上是HashTable(链接法解决冲突),本文将对最常用的函数系列-数组操作的相关函数做进一步的跟踪. 本文主要内容: PHP中提供的数组操作函数 数组操作函数的实现 结语参考文献 一.PHP中提供的数组操作函数 可以说,数组是PHP中使用最广泛的数据结构之一,正因如此,PHP为开发者提供了丰富的数组操作函数(参见http://cn2.php.net/manual/en/ref.array.php

PHP:常用PHP数组操作函数

php为我们提供了丰富的数组操作函数,用这些函数可以非常方便的实现我们所想实现的功能. 添加和删除数组元素 添加元素的方法 array_unshift(array array,mixed var [,mixed var...]) 在数组头添加元素,所有的数值键会被相应的修改,但关联键不会受到影响 array_push(array array,mixed var [,mixed var.....]) 在数组尾添加元素 删除数组元素的方法 array_shift(array array) 删除数组头

js数组操作

js数组操作大全(转) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b:1 unshift:将参数添加到原数组开头,并返回数组的长度 var a = [1,2,3,4,5]; var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5] b:7 注:在IE6.0下测试返回值总为undefined,FF2.0下测试

js数组操作常用方法(转自:http://www.jbxue.com/article/js/20224.html)

js数组操作常用方法,包括数组的创建.数组的元素的访问.数组元素的删除.数组的拷贝等. 原文参考自:http://www.jbxue.com/article/js/20224.html 在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像JSON数据是以数组的形式出现的,查阅了下相关JS中数组的操作一试果然很爽.记录下来.1.数组的创建 var arr

JavaScript 数组操作

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组操作</title> </head> <body> <script type="text/javascript"> //定义数组 var arry = [0,1,2,3,5,6,7,8]; //获取

JavaScript中常见的数组操作函数及用法

昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下,这样方便大家准备参考.如果恰好你也在准备各种笔试,希望对你有所帮助.同时,也欢迎补充. 1.数组创建 创建数组应该是最简单的了,有用数组字面量创建和数组构造函数两种方法,见下: var array1 = new Array(); var array2 = []; 上面是创建数组的最常见的两种方法,其中第二种方法因为简单直观而被开发者推崇.其中,

js基础复习---数组操作

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="keywords" content="js数组操作"> <meta name="description" content="js基础之数组api练习"> <title

js中数组操作

var selectedCodeArray = []; var num = $.inArray(值, selectedCodeArray)  //值在数组中的位置 selectedCodeArray.push(值码); selectedCodeArray.splice(num, 1);//从数组中删除 js中数组操作,布布扣,bubuko.com