zoukankan      html  css  js  c++  java
  • cf 教育场 . Micro-World

    B. Micro-World
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.

    You know that you have nn bacteria in the Petri dish and size of the ii-th bacteria is aiai. Also you know intergalactic positive integer constant KK.

    The ii-th bacteria can swallow the jj-th bacteria if and only if ai>ajai>aj and aiaj+Kai≤aj+K. The jj-th bacteria disappear, but the ii-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria ii can swallow any bacteria jjif ai>ajai>aj and aiaj+Kai≤aj+K. The swallow operations go one after another.

    For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54]a=[101,53,42,102,101,55,54] and K=1K=1. The one of possible sequences of swallows is: [101,53,42,102,101––,55,54][101,53,42,102,101_,55,54]  [101,53,42,102,55,54][101,53_,42,102,55,54]  [101––,42,102,55,54][101_,42,102,55,54]  [42,102,55,54][42,102,55,54_]  [42,102,55][42,102,55]. In total there are 33 bacteria remained in the Petri dish.

    Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.

    Input

    The first line contains two space separated positive integers nn and KK (1n21051≤n≤2⋅1051K1061≤K≤106) — number of bacteria and intergalactic constant KK.

    The second line contains nn space separated integers a1,a2,,ana1,a2,…,an (1ai1061≤ai≤106) — sizes of bacteria you have.

    Output

    Print the only integer — minimal possible number of bacteria can remain.

    Examples
    input
    Copy
    7 1
    101 53 42 102 101 55 54
    
    output
    Copy
    3
    
    input
    Copy
    6 5
    20 15 10 15 20 25
    
    output
    Copy
    1
    
    input
    Copy
    7 1000000
    1 1 1 1 1 1 1
    
    output
    Copy
    7
    
    Note

    The first example is clarified in the problem statement.

    In the second example an optimal possible sequence of swallows is: [20,15,10,15,20,25][20,15,10,15,20_,25]  [20,15,10,15,25][20,15,10,15_,25]  [20,15,10,25][20,15,10_,25] [20,15,25][20,15_,25]  [20,25][20_,25]  [25][25].

    In the third example no bacteria can swallow any other bacteria.

    题意:对任意一个数a[i] , 可以对任意 满足 i != j 且 a[i] > a[j] && a[i] <= a[j] +k 的 a[j] 可以被删掉,求使最终剩下的个数最少。

    思路:  显然需要对数组排序,之后为了达到剩余个数最少,我们需要从小到大按照题意模拟扫一遍。


    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e6+10;
    int n,a[maxn],k;
    bool vis[maxn];
    
    int main(){
        memset(vis,false,sizeof(vis));
        scanf("%d%d",&n,&k);
        for (int i=1; i<=n ;i++) scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        int cnt=0;
        int i=1,j=1;
        while(i<=n && j<=n){
            if(a[i]>a[j] && a[i]<=a[j]+k){
                vis[j]=1;
                j++;
            }
            else if(a[i]<=a[j])i++;
            else j++;
        }
        for (int i=1; i<=n ;i++) if(!vis[i])cnt++;
        printf("%d
    ",cnt);
        return 0;
    }

  • 相关阅读:
    【学习】reactjs(一)——使用npm创建react项目并整合elementUI
    【学习】整合springboot2.0 和 mybatis,实现基本的CRUD
    macos monterey 系统升级后 go build 错误
    [R语言]关联规则2---考虑items之间严格的时序关系
    [R语言]关联规则1---不考虑items之间的时序关系
    [python]使用python实现Hadoop MapReduce程序:计算一组数据的均值和方差
    [机器学习笔记]奇异值分解SVD简介及其在推荐系统中的简单应用
    [机器学习笔记]主成分分析PCA简介及其python实现
    [游戏数据分析]WAU模型简介及WAU预测
    [R语言]读取文件夹下所有子文件夹中的excel文件,并根据分类合并。
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490309.html
Copyright © 2011-2022 走看看