赞
踩
解析:
详见代码:
- #include<bits/stdc++.h>
- using namespace std;
- long long a[1000005];
- int n;
- int main() {
- int m, ans = 0;
- cin >> n;
- m = n;
- int s = 1;
- for(int i = 1; i <= n; i++) {
- cin >> a[i];
- }
- sort(a + 1, a + n + 1);
- for(int j = 1; j <= m; j++) {
- for(int k = s; k <= n; k++) {
- if(j <= a[k]) {
- ans++;
- a[k] = j;
- s = k;
- break;
- }
- }
- }
- cout << ans;
- return 0;
- }
另一种解法:
- #include<bits/stdc++.h>
- using namespace std;
- long long a[1000005];
- int n;
- int main() {
- int m, ans = 0;
- cin >> n;
- for(int i = 1; i <= n; i++) {
- cin >> a[i];
- }
- sort(a + 1, a + n + 1);
- for(int i = 1, j = 1; i <= n; i++) {
- while(a[j] < i && j <= n) {
- j++;
- }
- if(j > n) break;
- ans++;
- j++;
- }
- cout << ans;
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。