### codility上的練習 （1）

codility上面添加了教程。目前只有lesson 1，講復雜度的……里面有幾個題， 目前感覺題庫的題簡單。

Frog-Jmp:

`// you can also use includes, for example:// #include <algorithm>int solution(int X, int Y, int D) {    // write your code here...    return (X >= Y)?0:((Y - X + D - 1) / D);}`

Perm-Missing-Elem:

1 求和，注意使用long long。

`// you can also use includes, for example:// #include <algorithm>int solution(vector<int> &A) {    // write your code here...int n = A.size(), i;long long r = n + 1;    for (i = 0; i < n; ++i) {        r += (i + 1) - A[i];    }    return r;}`

2 求異或，不需要long long

`// you can also use includes, for example:// #include <algorithm>int solution(vector<int> &A) {    // write your code here...int n = A.size(), i, r = n + 1;    for (i = 0; i < n; ++i) {        r ^= (i + 1) ^ A[i];    }    return r;}`

3 把A[i]換到下標為A[i] - 1的位置，注意(N + 1)單獨拿出來

`// you can also use includes, for example:// #include <algorithm>int solution(vector<int> &A) {    // write your code here...    int n = A.size(),i,x,t;    for (i = 0; i < n; ++i) {        for (x = A[i]; (x <= n) && (A[x - 1] != x); ) {            t = A[x - 1];            A[x - 1] = x;            x = t;        }    }    for (i = 0; i < n; ++i) {        if (A[i] != i + 1) {            break;        }    }    return i + 1;       }`

Tape-Equilibrium:

`// you can also use includes, for example:// #include <algorithm>int solution(vector<int> &A) {    // write your code here...    int n = A.size(), sum, r, i, t;    for (i = sum = 0; i < n; ++i) {        sum += A[i];}r = abs(sum - A[0] - A[0]);for (i = 2, t = A[0]; i < n; ++i) {        t += A[i - 1];        r = min(r, abs(sum - t - t));    }return r;}`

Further Training:

Abs-distinct:

`// you can also use includes, for example:// #include <algorithm>int solution(const vector<int> &A) {    // write your code here...int n = A.size(), i, j ,r, t;    for (i = r = 0, j = n - 1; (i < j) && (A[i] < 0) && (A[j] > 0); ++r) {        t = (A[i] + A[j] < 0)?A[i]:A[j];        for (;((t < 0) && (A[i] == t)) || ((t > 0) && (A[i] + t == 0)) ; ++i);        for (;((t < 0) && (A[j] + t == 0)) || ((t > 0) && (A[j] == t)); --j);    }    for (;i <= j;++r) {        for (t = A[i]; (i <= j) && (A[i] == t); ++i)        ;    }    return r;}`

Binary-gap:

N [1..2147483647]。要求復雜度時間O(logN)，空間O(1)。

`// you can also use includes, for example:// #include <algorithm>int solution(int N) {    // write your code here...    int i,r, last;    for (i = r = 0, last = -1; N; N >>= 1,++i) {        if (N & 1) {            if (last >= 0) {                r = max(i - last - 1, r);            }            last = i;        }    }    return r;}`

Equi:

http://blog.csdn.net/caopengcs/article/details/9323743

`// you can also use includes, for example:// #includeint solution(const vector<int> &A) {    // write your code here...    int n = A.size(),i;    long long sum = 0, left = 0;    for (i = 0; i < n; ++i) {        sum += A[i];    }    for (i = 0; i < n; ++i) {        if (left == sum - A[i] - left) {            return i;        }        left += A[i];    }             }`

Dominator:

`// you can also use includes, for example:// #include <algorithm>int solution(const vector<int> &A) {    // write your code here...    int r, i , t, n = A.size();    for (i = r = t = 0; i < n; ++i) {        if (A[i] == A[r]) {            ++t;        }        else if (--t < 0) {            t = 1;            r = i;        }    }    for (i = n - 1, n >>= 1; i >= 0; --i) {        if ((A[i] == A[r]) && (--n < 0)) {            return r;        }    }    return -1;}`

Max-profit:

`// you can also use includes, for example:// #include <algorithm>int solution(const vector<int> &A) {    // write your code here...    int n = A.size(), i, profit, cheapest;    for (i = profit = cheapest = 0; i < n; ++i) {        if (A[i] < A[cheapest]) {            cheapest = i;        }        profit = max(profit, A[i] - A[cheapest]);    }    return profit;}`

Tree-height:

`// you can also use includes, for example:// #include <algorithm>int solution(tree * T) {    // write your code here...    return T?(max(solution(T->l),solution(T->r)) + 1):(-1);}`

Array-inversion-count:

`// you can also use includes, for example:// #include <algorithm>vector<int> temp;int help(vector<int> &A,int from,int to) {int i,j,r,r1,r2,mid;    if (to - from <= 0) {        return 0;    }    mid = (from + to) >> 1;    r1 = help(A, from, mid);    if (r1 > 1000000000) {        return -1;    }    r2 = help(A, mid + 1, to);    if (r2 > 1000000000) {        return -1;    }    if ((r  = r1 + r2) > 1000000000) {        return -1;    }temp.clear();    for (i = from, j = mid + 1; (i <= mid) && (j <= to);) {        if (A[i] <= A[j]) {            temp.push_back(A[i++]);        }        else {            if ((r += mid - i + 1) > 1000000000) {                return -1;            }            temp.push_back(A[j++]);        }    }    for (;i <= mid;++i) {        temp.push_back(A[i]);           }    for (;j <= to; ++j) {        temp.push_back(A[j]);    }    for (i = from; i <= to; ++i) {        A[i] = temp[i - from];    }    return r;}int solution(const vector<int> &A) {    // write your code here...vector<int> a = A;    return help(a, 0, a.size() - 1);}`