- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
#include <cstdio>
#include <cstring>
int main() {
unsigned long long int a[100000];
unsigned long long int N;
unsigned long long int max,max1;
unsigned long long int maxC,maxC1;
unsigned long long int maxN[100000];
unsigned long long int ans = 0;
unsigned long long int ans1 = 0;
max = max1 = maxC = maxC1 = 0;
scanf("%llu",&N);
for (unsigned long long int i = 0; i < N; ++i) {
scanf("%llu",a + i);
if (a[i] > a[max]) {
max1 = max;
maxC1 = maxC;
// memcpy(maxN,maxN1,maxC*sizeof(unsigned long long int));
max = i;
maxC = 1;
maxN[0] = max;
} else if (a[i] == a[max]) {
maxN[maxC++] = i;
} else if (a[i] > a[max1]) {
max1 = i;
maxC1 = 1;
} else if (a[i] == a[max1]) {
maxC1++;
}
}
if (maxC == 1) {
ans = a[max];
if (a[max] == a[max1] + 1) {
if (a[max] < (maxC1 + 1) * a[max1]) {
printf("%llu\n",a[max]);
return 0;
}
}
if (max > 0) {
if (a[max - 1] + 1 < ans - 1) {
if (a[max - 1] + 1 == a[max1]) {
if (a[max1] * (maxC1 + 1 + (a[max] - 1 == a[max1]?1:0)) < ans) {
ans1 = a[max1] * (maxC1 + 1 + (a[max] - 1 == a[max1]?1:0));
if (ans > ans1) ans = ans1;
// prunsigned long long intf ("1 : %d\n",ans);
}
}
else {
ans1 = a[max] - 1 + (a[max] - 1 == a[max1]?a[max1] * maxC1:0);
if (ans > ans1) ans = ans1;
// prunsigned long long intf ("2 : %d\n",ans);
}
}
}
if (max < N - 2) {
if (a[max + 1] + 1 < ans - 1) {
if (a[max + 1] + 1 == a[max1]) {
if (a[max1] * (maxC1 + 1 + (a[max] - 1 == a[max1]?1:0)) < ans) {
ans1 = a[max1] * (maxC1 + 1 + (a[max] - 1 == a[max1]?1:0));
if (ans > ans1) ans = ans1;
// prunsigned long long intf ("3 : %d\n",ans);
}
}
else {
ans1 = a[max] - 1 + (a[max] - 1 == a[max1]?a[max1] * maxC1:0);
if (ans > ans1) ans = ans1;
// prunsigned long long intf ("4 : %d\n",ans);
}
}
}
printf("%llu\n",ans);
return 0;
} // one max line
bool f = true;
bool f1 = true;
if (a[max] * maxC > a[max] + 1) {
// prunsigned long long intf("here %d\n",maxC);
for (unsigned long long int i = 0; i < maxC; ++i) {
if (f1 && ((maxN[i] && a[maxN[i] - 1]) || (maxN[i] < N - 1 && a[maxN[i] + 1]))) f1 = false; // get from near line
if (f && ((maxN[i] && a[maxN[i] - 1] < a[max] - 1) || (maxN[i] < N - 1 && a[maxN[i] + 1] < a[max] - 1))) f = false;
}
}
ans1 = ans = maxC * a[max];
if (!f && a[max] > 1) ans = (maxC - 1) * a[max];
if (!f1) ans1 = a[max] + 1;
printf("%llu\n",ans>ans1?ans1:ans);
return 0;
}
Серега говнокодит задачу с OpenCup'a от 15.03.2015 (задача L - Бассейн счастья)