zoukankan      html  css  js  c++  java
  • cf 466 div.2 A. Points on the line

    A. Points on the line
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round.

    The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1, 3, 2, 1} is 2.

    Diameter of multiset consisting of one point is 0.

    You are given n points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed d?

    Input

    The first line contains two integers n and d (1 ≤ n ≤ 100, 0 ≤ d ≤ 100) — the amount of points and the maximum allowed diameter respectively.

    The second line contains n space separated integers (1 ≤ xi ≤ 100) — the coordinates of the points.

    Output

    Output a single integer — the minimum number of points you have to remove.

    Examples
    input
    Copy
    3 1
    2 1 4
    output
    1
    input
    Copy
    3 0
    7 7 7
    output
    0
    input
    Copy
    6 3
    1 3 4 6 9 10
    output
    3
    分析:
    一开始想错了,觉得直接找最小的最大的贪心就好。结果wa了,
    用暴力直接开个二重循环.依次遍历,找到除了头尾的数字之外的数字个数,取最小的就行,头是每一个数字,尾是第一个比a[i]大d的数字
    代码如下:
    #include<cstdio>
    #include<algorithm>
    #define N 1000
    int i,j,n,a[N],ans,d;
    using namespace std;
     int main()
     {
         for(scanf("%d %d",&n,&d);++i<=n;)
            scanf("%d",&a[i]);
         sort(a+1,a+1+n);
         for(int i=1;i<=n;i++)
         {
             for(int j=i;a[j]<=a[i]+d&&j<=n;j++)
             if(j-i+1>ans)
                ans=j-i+1;
         }
         printf("%d
    ",n-ans);
    
    
    
    
    
         return 0;
     }
  • 相关阅读:
    Flink1.9重大改进和新功能
    【2020】DBus,一个更能满足企业需求的大数据采集平台
    大数据运维:大数据平台+海量数据
    大数据运维尖刀班 | 集群_监控_CDH_Docker_K8S_两项目_腾讯云服务器
    离线数仓和实时数仓架构与设计
    【全集】IDEA入门到实战
    Mysql快速入门
    RabbitMQ安装
    消息队列MQ简介
    C#特性
  • 原文地址:https://www.cnblogs.com/renxin123/p/8467879.html
Copyright © 2011-2022 走看看