#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX_COINS = 1000001;
int n; //number of coins
int expon; //current exponent
int pos; //current position
int carryover;
int counter; //counter of current position
int nums[MAX_COINS]; //exponents of particular coins
int main(int argc, char *argv[]) {
scanf("%d\n", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
sort(nums, nums+n);
carryover = 0; //initial carryover = 0
pos = 0;
while (pos < n) {
expon = nums[pos];
counter = carryover + 1;
while ((pos < n-1) && (nums[pos] == nums[pos+1])) {
pos++;
counter++;
}
carryover = counter / 2;
pos++;
if (pos < n) {
expon++;
while ((expon != nums[pos]) && (carryover > 0)) {
expon++;
carryover = carryover / 2;
}
} else {
while (carryover > 0) {
expon++;
carryover = carryover / 2;
}
}
}
printf("%d\n", expon);
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 | #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int MAX_COINS = 1000001; int n; //number of coins int expon; //current exponent int pos; //current position int carryover; int counter; //counter of current position int nums[MAX_COINS]; //exponents of particular coins int main(int argc, char *argv[]) { scanf("%d\n", &n); for (int i = 0; i < n; i++) { scanf("%d", &nums[i]); } sort(nums, nums+n); carryover = 0; //initial carryover = 0 pos = 0; while (pos < n) { expon = nums[pos]; counter = carryover + 1; while ((pos < n-1) && (nums[pos] == nums[pos+1])) { pos++; counter++; } carryover = counter / 2; pos++; if (pos < n) { expon++; while ((expon != nums[pos]) && (carryover > 0)) { expon++; carryover = carryover / 2; } } else { while (carryover > 0) { expon++; carryover = carryover / 2; } } } printf("%d\n", expon); return 0; } |
English