é£æ³é åã®è¯ãç·´ç¿åé¡ï¼
åé¡æ¦è¦
æ£ã®æ´æ°ãããªãæ°åãè¯ãæ°åã§ããã¨ã¯ãæ°åã«å«ã¾ããä»»æã®æ´æ°å¤ ã«ã¤ãã¦ãæ°åä¸ã« ãã¡ããã© åå«ã¾ãã¦ãããã¨ãããã
ä¸ããããæ°å ã«å¯¾ãã¦ãããã¤ãã®è¦ç´ ãåé¤ãããã¨ã§ãããæ°åã¨ãªãããã«ããããåé¤ãã¹ãè¦ç´ æ°ã®æå°å¤ãæ±ããã
å¶ç´
èãããã¨
æ°åã®æ°ã®ä¸¦ã³ã¯å
¨ãé¢ä¿ãªãã¦ããã©ã®æ°å¤ãä½åããããã ããéè¦ããªã®ã§ãã¾ãã¯éè¨å¦çããããã次ã®ãããªé£æ³é
å (C++ ãªãã° map
å) ãæ§ç¯ããã
nums[v]
ï¼æ°å ã«å«ã¾ããæ´æ°å¤ ã®åæ°
ãããããã¨ã¯ãå¤ ãã¨ã«èãã¦ãããã次ã®ããã«ããã°ãããï¼çãã表ãå¤æ°ã res
ã¨ãã)
nums[v] >= v
ã®ã¨ãï¼å¤ ãnums[v] - v
ååé¤ããã°ãæ°åä¸ã« ã åå«ã¾ããç¶æ ã«ãªã- ã¤ã¾ãã
res += nums[v] - v
ã¨ãã
- ã¤ã¾ãã
nums[v] < v
ã®ã¨ãï¼ãã®å ´åã¯å¤ ããã¹ã¦åé¤ãããããªã- ã¤ã¾ãã
res += nums[v]
ã¨ãã
- ã¤ã¾ãã
ãã®è§£æ³ã®è¨ç®é㯠ã¨ãªãã
ã³ã¼ã
#include <bits/stdc++.h> using namespace std; int main() { long long N, a; cin >> N; map<long long, long long> nums; for (int i = 0; i < N; i++) { cin >> a; nums[a]++; } long long res = 0; for (auto [val, num] : nums) { if (num < val) res += num; else res += (num - val); } cout << res << endl; }