D - Points on the line 逆向思维

传送门
题意:要求你删去一些元素,使集合中的元素最大值 - 最小值 <= d.问你最少删去的元素。
思路:先排序,然后我们只要求出最长连续符合题意的子串即可。
最后用总数减去最长连续子串,结果的便是需要删除的元素。
最后剩余的数就是我们理想状态下想要得到的符合题意的最长子串。
太聪明了,好思路!!!

/**
* From:
* Qingdao Agricultural University
* Created by XiangwangAcmer
* Date : 2019-11-09-13.14.48
* Talk is cheap.Show me your code.
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<map>
#include<string>
#include<set>
#include<cstdlib>
#define ll long long
using namespace std;
const ll maxn = 1e6 + 5;
const ll minn = 1e9 + 5;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LIMIT = 4294967295LL;
vector<int>v[maxn];
int dp[maxn];
vector<int>G[maxn];
bool row[maxn], col[maxn];
queue<int>q;
int a[200];
int val[200];
set<int>s;
int vis[101][101];
int vis1[101][101];
int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(int i =  1; i <= n; i++)
    {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    int maxnn = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = i; j <= n; j++)
        {
            if(a[j] - a[i] <= m)
            {
                    maxnn = max(maxnn,j - i + 1);
            }
        }
    }
    cout << n - maxnn << endl;
    return 0;
}

发布了244 篇原创文章 · 获赞 8 · 访问量 5121

猜你喜欢

转载自blog.csdn.net/weixin_43960370/article/details/103068643