#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 500100; int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif static pair<int, int> valToNum[MAXN]; memset(valToNum, 0x00, sizeof(valToNum)); int n; scanf("%d", &n); for (int v = 1; v <= n; v++) valToNum[v].second = v; for (int i = 0; i < n; i++) { int v; scanf("%d", &v); valToNum[v].first++; } sort(valToNum + 1, valToNum + n + 1); int left = 1, right = n; int res = 0; while (left <= right) { int numOfLeaders = valToNum[right].first; int maxNonLeadersNow = numOfLeaders - 1; valToNum[right].first = 0; while (left < right && valToNum[left].first <= maxNonLeadersNow) { maxNonLeadersNow -= valToNum[left].first; valToNum[left].first = 0; left++; } if (left < right && maxNonLeadersNow > 0) { valToNum[left].first -= maxNonLeadersNow; if (valToNum[left].first < 0) { #ifdef _DEBUG _DEBUG_ERROR("valToNum[left].first > 0"); #endif valToNum[left].first = 0; } } res++; right--; } printf("%d\n", res); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 500100; int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif static pair<int, int> valToNum[MAXN]; memset(valToNum, 0x00, sizeof(valToNum)); int n; scanf("%d", &n); for (int v = 1; v <= n; v++) valToNum[v].second = v; for (int i = 0; i < n; i++) { int v; scanf("%d", &v); valToNum[v].first++; } sort(valToNum + 1, valToNum + n + 1); int left = 1, right = n; int res = 0; while (left <= right) { int numOfLeaders = valToNum[right].first; int maxNonLeadersNow = numOfLeaders - 1; valToNum[right].first = 0; while (left < right && valToNum[left].first <= maxNonLeadersNow) { maxNonLeadersNow -= valToNum[left].first; valToNum[left].first = 0; left++; } if (left < right && maxNonLeadersNow > 0) { valToNum[left].first -= maxNonLeadersNow; if (valToNum[left].first < 0) { #ifdef _DEBUG _DEBUG_ERROR("valToNum[left].first > 0"); #endif valToNum[left].first = 0; } } res++; right--; } printf("%d\n", res); return 0; } |