CodeForces 990B
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have nn bacteria in the Petri dish and size of the ii-th bacteria is aiai. Also you know intergalactic positive integer constant KK.
The ii-th bacteria can swallow the jj-th bacteria if and only if ai>ajai>aj and ai≤aj+Kai≤aj+K. The jj-th bacteria disappear, but the ii-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria ii can swallow any bacteria jj if ai>ajai>aj and ai≤aj+Kai≤aj+K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54]a=[101,53,42,102,101,55,54] and K=1K=1. The one of possible sequences of swallows is: [101,53,42,102,101−−−,55,54][101,53,42,102,101_,55,54] →→ [101,53−−,42,102,55,54][101,53_,42,102,55,54] →→ [101−−−,42,102,55,54][101_,42,102,55,54] →→ [42,102,55,54−−][42,102,55,54_] →→ [42,102,55][42,102,55]. In total there are 33 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers nn and KK (1≤n≤2⋅1051≤n≤2⋅105, 1≤K≤1061≤K≤106) — number of bacteria and intergalactic constant KK.
The second line contains nn space separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Sample Input
7 1
101 53 42 102 101 55 54
3
6 5
20 15 10 15 20 25
1
7 1000000
1 1 1 1 1 1 1
7
Hint
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20,15,10,15,20−−,25][20,15,10,15,20_,25] →→ [20,15,10,15−−,25][20,15,10,15_,25] →→ [20,15,10−−,25][20,15,10_,25] →→ [20,15−−,25][20,15_,25] →→ [20−−,25][20_,25] →→ [25][25].
In the third example no bacteria can swallow any other bacteria.
题意:满足题中所给条件的病菌会发生吞并,问最后剩下的病菌的最少的数量。
吞并的条件有两个,可以通过在先确定一个条件的前提下,判断另一个条件。
先对病菌从大到小排一遍序,然后从后向前遍历一遍,只需要判断相邻的两个是否满足条件即可(特殊的是,有相等的数的时候),因为相邻的两个是差最小的,最容易满足第二个条件。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; #define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r map<LL,LL> mp;
LL con[N]; bool cmp(LL a,LL b)
{
return a>b;
}
int main()
{
LL i,p,j,x;
LL n,k;
scanf("%lld%lld",&n,&k);
for(i=;i<n;i++)
{
scanf("%lld",&con[i]);
mp[con[i]]++;
}
sort(con,con+n,cmp);
p=n;
for(i=n-;i>=;i--)
{
if(con[i]>con[i+]&&con[i]<=(con[i+]+k))
{
p=p-mp[con[i+]];
}
}
printf("%lld\n",p);
return ;
}
CodeForces 990B的更多相关文章
- CodeForces 990B Micro-World(思维、STL)
http://codeforces.com/problemset/problem/990/B 题意: 有n个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且a ...
- Codeforces 990B :Micro-World
B. Micro-World time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- playbook详解—YAML格式的文本
在playbook中有一些核心的指令 hosts:指明命令运行在哪个node之上 remote_user:在远端的node之上以什么用户的身份运行命令 var:给模板传递变量值 tasks:指明需要执 ...
- QCryptographicHash实现哈希值计算,支持多种算法
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QCryptographicHash实现哈希值计算,支持多种算法 本文地址:http: ...
- ping(团队作业)
一,团队成员 何守成 031602408(队长) 黄锦峰 031602411 肖逸清 031602435 张子纯 031602441 蔡志斌 031602602 柯叶祥 031602414 二.作业链 ...
- 使用selenium遍历frame中的表单信息 ;
遍历frame中的表单 : package webDriverPro; import java.util.List; import java.util.regex.Matcher; import ja ...
- mysql子查询批量找id最大的
$sql = "select a.id as max_id,a.uid from(SELECT `uid`, idFROM (`users_level_change_log`)WHERE ` ...
- [历史百科]抗战时期兵团简介 From 百度知道
中央军委1948年11月1日和1949年1月15日两次关于统一全军组织和部队番号的训令,我军先后进行了整编.西北野战军改称第一野战军,司令员兼政治委员彭德怀,第一副司令员张宗逊,第二副司令员赵寿山,参 ...
- pcap的安装与配置
1.打开网址:www.tcpdump.org/ 下载 libpcap-1.0.0.tar.gz (512.0KB) 软件包,通过命令 tar zxvf libpcap-1.0.0.tar.gz 解压文 ...
- 按着shift键对dbgrid进行多条记录选择的问题(50分)
可以用sendmessage,想dbgrid 发键盘信息,按下shift键,同时按下button1procedure TForm1.Button1Click(Sender: TObject);vari ...
- Vue 自定义指令练习
Vue.directive(id,definition)注册一个全局自定义指令,接收两个参数,指令ID以及定义对象 取值: <div v-demo="{ color: 'white', ...
- HDU4701_Game
很有意思,很好的一个题目. 题目的意思是两个人初始状态分别有A和B元,现在有N件可买的商品.两人轮流买,商品必须从左到右买过去,一次可以买若干个.第一个无法买到商品的人输. 一看就知道是博弈题目,但是 ...