E.Boxers
time limit per test2 seconds
memory limit per test256 megabytes
inputstdin
outputstdout
There are 𝑛 boxers, the weight of the 𝑖-th boxer is 𝑎𝑖. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.
It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique).
Write a program that for given current values 𝑎𝑖 will find the maximum possible number of boxers in a team.
It is possible that after some change the weight of some boxer is 150001 (but no more).
Input
The first line contains an integer 𝑛 (1≤𝑛≤150000) — the number of boxers. The next line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛, where 𝑎𝑖 (1≤𝑎𝑖≤150000) is the weight of the 𝑖-th boxer.
Output
Print a single integer — the maximum possible number of people in a team.
input
4 3 2 4 1
output
4
input
6 1 1 1 4 4 4
output
5
Note
In the first example, boxers should not change their weights — you can just make a team out of all of them.
In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5,4,3,2,1.
题意就是给你一堆数,它们可以上下变动1或者不变,但是1不能变0,注意对于每个数字,你只能选择一次,+1,-1,or不变。现在问你,这些数能变成多少个不同的数字。
注意数据范围和时限,大概是O(nlogn)。
排序?
为什么排序?——贪心?
想一想,大概能发现,其实从小到大排序,一个数尽量往小了变,搞个vis数组记录一下,应该稳妥吧。
#include<bits/stdc++.h> using namespace std; int a[150011]; int vis[150011]; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } sort(a,a+n); int cnt=0; for(int i=0;i<n;i++){ if(a[i]>1){ if(!vis[a[i]-1]){ vis[a[i]-1]=1; cnt++; }else if(!vis[a[i]]){ vis[a[i]]=1; cnt++; }else if(!vis[a[i]+1]){ vis[a[i]+1]=1; cnt++; } }else if(a[i]==1){ if(!vis[a[i]]){ vis[a[i]]=1; cnt++; }else if(!vis[a[i]+1]){ vis[a[i]+1]=1; cnt++; } } } cout<<cnt<<endl; return 0; }