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;
     }
  • 相关阅读:
    Python第二弹--------类和对象
    Python第一弹--------初步了解Python
    Java标记接口
    CentOS7下的YUM源服务器搭建详解,过程写的很详细(转)
    CentOS7.0安装Nginx 1.10.0
    QT中C++与Html端通信例子
    QT基础:QMainWindow学习小结
    QT基础:QT 定时器学习
    QT3D场景快速绘制入门学习
    QT编译错误:cannot find file: *.pro
  • 原文地址:https://www.cnblogs.com/renxin123/p/8467879.html
Copyright © 2011-2022 走看看