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;
    }
}

转载于:https://www.cnblogs.com/Roni-i/p/9215028.html

(smol-env) [heng2@localhost ~]$ pip install transformers -i https://pypi.tuna.tsinghua.edu.cn/simple Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting transformers Downloading https://pypi.tuna.tsinghua.edu.cn/packages/51/51/b87caa939fedf307496e4dbf412f4b909af3d9ca8b189fc3b65c1faa456f/transformers-4.46.3-py3-none-any.whl (10.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 1.4 MB/s eta 0:00:00 Collecting filelock (from transformers) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b9/f8/feced7779d755758a52d1f6635d990b8d98dc0a29fa568bbe0625f18fdf3/filelock-3.16.1-py3-none-any.whl (16 kB) Collecting huggingface-hub<1.0,>=0.23.2 (from transformers) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/40/0c/37d380846a2e5c9a3c6a73d26ffbcfdcad5fc3eacf42fdf7cff56f2af634/huggingface_hub-0.29.3-py3-none-any.whl (468 kB) Collecting numpy>=1.17 (from transformers) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/98/5d/5738903efe0ecb73e51eb44feafba32bdba2081263d40c5043568ff60faf/numpy-1.24.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 2.6 MB/s eta 0:00:00 Collecting packaging>=20.0 (from transformers) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/88/ef/eb23f262cca3c0c4eb7ab1933c3b1f03d021f2c48f54763065b6f0e321be/packaging-24.2-py3-none-any.whl (65 kB) Collecting pyyaml>=5.1 (from transformers) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fd/7f/2c3697bba5d4aa5cc2afe81826d73dfae5f049458e44732c7a0938baa673/PyYAML-6.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (746 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 746.5/746.5 kB 1.7 MB/s eta 0:00:00 Collecting regex!=2019.12.17 (from transformers) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5a/c8/dc7153ceb5bcc344f5c4f0291ea45925a5f00009afa3849e91561ac2e847/regex-2024.11.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (785 kB)
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值