#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct {
long a;
long p;
} el_poz;
el_poz val[1000000];
long n;
long szukaj(long key) {
long i;
for (i = 0; i < n; i++)
if (val[i].a == key) return i;
return -1;
}
int porownaj(const void * a, const void * b)
{
el_poz *x = (el_poz *)a;
el_poz *y = (el_poz *)b;
return ( x->a - y->a );
}
int main()
{
long i, j, ile, x, y, sz;
cin >> n;
for (i = 0; i < n; i++) {
cin >> val[i].a; val[i].p = i;
if (val[i].a == n) x = y = i;
}
qsort (val, n, sizeof(el_poz), porownaj);
ile = 2; sz = n - 1;
for (i = 2; i < n; i++) { // dlugosc przedzialu
if (i % 2 == 0) {
//j = szukaj(--sz);
j = val[--sz].p;
if (j < x) x = j;
if (j > y) y = j;
}
if (y - x < i) {
ile += i - y + x;
if (x + i > n) ile += n - x - i;
if (y - i < -1) ile += y - i + 1;
}
// /**/ cout << i << " " << x << " " << y << " " << ile << endl;
}
cout << (n + n + 1) << " " << ile;
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 53 54 55 56 | #include <iostream> #include <cstdlib> using namespace std; typedef struct { long a; long p; } el_poz; el_poz val[1000000]; long n; long szukaj(long key) { long i; for (i = 0; i < n; i++) if (val[i].a == key) return i; return -1; } int porownaj(const void * a, const void * b) { el_poz *x = (el_poz *)a; el_poz *y = (el_poz *)b; return ( x->a - y->a ); } int main() { long i, j, ile, x, y, sz; cin >> n; for (i = 0; i < n; i++) { cin >> val[i].a; val[i].p = i; if (val[i].a == n) x = y = i; } qsort (val, n, sizeof(el_poz), porownaj); ile = 2; sz = n - 1; for (i = 2; i < n; i++) { // dlugosc przedzialu if (i % 2 == 0) { //j = szukaj(--sz); j = val[--sz].p; if (j < x) x = j; if (j > y) y = j; } if (y - x < i) { ile += i - y + x; if (x + i > n) ile += n - x - i; if (y - i < -1) ile += y - i + 1; } // /**/ cout << i << " " << x << " " << y << " " << ile << endl; } cout << (n + n + 1) << " " << ile; return 0; } |
English