• CF 990B. Micro-World【数组操作/贪心/STL/二分搜索】


    【链接】:CF
    【题意】:对任意一个数a[i] ,可以对任意 满足 i != j 且 a[i] > a[j] && a[i] <= a[j] +k 的 a[j] 可以被删掉,求使最终剩下的个数最少。
    【分析】:扫一遍,二分搜索合法的。
    【代码】:

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const ll LNF = 1e18;
    const int maxn = 1e6+ 100;
    const int maxm = 100;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    //const int dx[] = {-1,1,0,0,1,1,-1,-1};
    //const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dx[] = {-1,0,1,0};
    int dy[] = {0,1,0,-1};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
    int n,k;
    int a[maxn], b[maxn];
    int main()
    {
        while(cin>>n>>k)
        {
            int cnt=0;
            for(int i=0;i<n;i++) cin>>a[i];/*
            for(int i=0;i<n;i++)
            {
                cout<<i<<"   ";
            }
            cout<<endl;*/
            sort(a,a+n,greater<int>());/*
            for(int i=0;i<n;i++)
            {
                cout<<a[i]<<' ';
            }
            cout<<endl;*/
            for(int i=0;i<n;i++)
            {
                int p = lower_bound(a,a+n,a[i]+k,greater<int>())-a;   //第一个 <= (a[i]+k) 的位置
                //cout<<"a[i]+k="<<a[i]+k<<" p="<<p<<" a[p]="<<a[p]<<" a[i]="<<a[i];
                if(a[p]>a[i])
                {
                    cnt++;
                    //cout<<" YES";
                }
                //cout<<endl;
            }
            cout<<n-cnt<<endl;
        }
    }
    /*
    7 1
    101 53 42 102 101 55 54
    42 53 54 55 101 101 102
    outputCopy
    3
    42 55 102
    inputCopy
    6 5
    20 15 10 15 20 25
    25
    outputCopy
    1
    inputCopy
    7 1000000
    1 1 1 1 1 1 1
    outputCopy
    7
    
    
    7 1
    101 53 42 102 101 55 54
    101 53 42 102 101 55 54
    102 101 101 55 54 53 42
    a[i]+k=103 p=0 a[p]=102 a[i]=102
    a[i]+k=102 p=0 a[p]=102 a[i]=101 YES
    a[i]+k=102 p=0 a[p]=102 a[i]=101 YES
    a[i]+k=56 p=3 a[p]=55 a[i]=55
    a[i]+k=55 p=3 a[p]=55 a[i]=54 YES
    a[i]+k=54 p=4 a[p]=54 a[i]=53 YES
    a[i]+k=43 p=6 a[p]=42 a[i]=42
    */
    

    [模拟]

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const ll LNF = 1e18;
    const int maxn = 1e6+ 100;
    const int maxm = 100;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    //const int dx[] = {-1,1,0,0,1,1,-1,-1};
    //const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dx[] = {-1,0,1,0};
    int dy[] = {0,1,0,-1};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
    int n,k,j;
    int a[maxn], b[maxn];
    int main()
    {
        while(cin>>n>>k)
        {
            int cnt, j=0;
            for(int i=0;i<n;i++) cin>>a[i];
    
            sort(a,a+n);
            cnt=n;
            for(int i=0;i<n;i++)
            {
               while(a[j]<a[i])
               {
                   if(a[j]+k>=a[i]) cnt--;
                   j++;
               }
            }
            cout<<cnt<<endl;
        }
    }
    
  • 相关阅读:
    [USACO14DEC] Cow Jog_Gold 牛慢跑(金)题解
    [USACO16DEC]Moocast(gold)奶牛广播-金 题解
    [USACO17FEB]Why Did the Cow Cross the Road III S题解
    [USACO4.3]逢低吸纳Buy Low, Buy Lower题解
    洛谷P5057 [CQOI2006]简单题题解
    ksum及二维版本
    [Noip2015] 信息传递
    数据库常用操作
    解决Mac连接MySQL需要输入绝对路径的问题
    在MAC上安装OpenCV(C++)
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9215028.html
Copyright © 2020-2023  润新知