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;
     }
  • 相关阅读:
    私有云是伪命题:真正的私有云 ≈ 公有云
    云计算的重新构建架构:优化迁移策略
    五个顶级的大数据架构
    Algorithm Gossip: 费式数列
    Algorithm Gossip: 河内塔
    Mysql连接报错:Unknown system variable 'language'
    ssm整合的时候出现的abstactMethodArror 解决
    java集合整理
    Oracle的序列和索引
    关于java堆栈的理解与说明
  • 原文地址:https://www.cnblogs.com/renxin123/p/8467879.html
Copyright © 2011-2022 走看看