Codeforces Round #560 (Div. 3) B题

题目网址:http://codeforces.com/contest/1165/problem/B

题目大意:给出n个数,问有多少个数满足连续大于等于自然数集中的数,即以此大于等于1,2,3……

题解:直接sort一下,然后从小到大和1,2,3……比较

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=2e5+7;
 4 int a[maxn];
 5 int main()
 6 {
 7     int n,tot=1,ans=0;
 8     cin>>n;
 9     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
10     sort(a+1,a+1+n);
11     for(int i=1;i<=n;i++) {
12         if(a[i]>=tot) {
13             ans++;tot++;
14         }
15     }
16     cout<<ans<<endl;
17     return 0;
18 } 
View Code

猜你喜欢

转载自www.cnblogs.com/duxing201806/p/10885790.html