Ruler method/two points + optimized audition spoj-cran04__ Ruler method

Source: Internet
Author: User

Do you:
1 Knowledge Points: Ruler method/two points + optimization (k = = 0 o'clock to judge the length of the cell and then calculate by formula)

2:
(1): Judge the number of 1 in a 01 sequence with a length of N (n <= 1e6) to determine how many intervals are K
3 Ideas:
(1): ruler (k = = 0 o'clock Special decision)
(2): two points + (k = 0 o'clock Special decision)
4 Reflection:
(1): The ruler of the need to strengthen understanding
(2): Time complexity when critical data (eg:k = = 0) is not taken into account
(3): The number of intervals that need to be stored to satisfy the condition without considering the critical data (eg:k = = 0) by a long long

Vjudge Topic link

The following is accepted code--two points + optimization

#include <cstdio> #include <cstring> #include <algorithm> using namespace std;

typedef long Long LL;
int rec[1004014], sum[1004014];

Char str[1004014];
    int main () {LL T, n, K, ans;
    scanf ("%lld", &t);
        while (t--) {scanf ("%lld%lld", &n, &k);
        scanf ("%s", str);
        for (int i = 0; i < n; i++) {rec[i+1] = str[i]-' 0 ';
            } if (!k) {ans = 0;
            LL T, J;
                for (LL i = 1; I <= n; i++) {if (rec[i)) continue;
                for (j = i+1; J <= N; j +) {if (rec[j]) break;
                    } if (j <= N) {t = (j-1)-i+1;
                    Ans + + t* (t+1)/(LL) 2;
                i = j;
                    else {t = n-i+1;
                    Ans + + t* (t+1)/(LL) 2;
                Break
         } printf ("%lld\n", ans);   Continue
        } Sum[0] = 0;
        for (int i = 1; I <= n; i++) {sum[i] = Sum[i-1] + rec[i];
        Ans = 0;
        LL T, L, R, Mid, Mt;
            for (int i = 1; I <= n; i++) {t = Sum[i] + k-rec[i];
            L = i, r = N;
                while (L <= r) {mid = (l+r)/2;
                if (sum[mid] = = t) break;
                if (Sum[mid] < T) {L = mid+1;
                else if (Sum[mid] > t) {r = mid-1;
            } if (L > R) continue;
                else {ans++;
                Mt = mid-1;
                    while (Mt >= i) {if (sum[mt] = = t) ans++;
                    else break;
                mt--;
                Mt = mid+1;
                    while (Mt <= N) {if (sum[mt] = = t) ans++;
                    else break; mt++;
                } printf ("%lld\n", ans);
return 0;
 }

The following is accepted code--ruler + optimization

#include <cstdio> #include <cstring> #include <algorithm> using namespace std;

typedef long Long LL;
int rec[1004014];

Char str[1004014];
    int main () {int T, n, K;
    LL ans;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n, &k);
        scanf ("%s", str);
        for (int i = 1; I <= n; i++) {rec[i] = str[i-1]-' 0 ';
        Ans = 0;
            if (!k) {LL T, J;
                for (LL i = 1; I <= n; i++) {if (rec[i)) continue;
                for (j = i+1; J <= N; j +) {if (rec[j]) break;
                    } if (j <= N) {t = (j-1)-i+1;
                    Ans + + t* (t+1)/(LL) 2;
                i = j;
                    else {t = n-i+1;
                    Ans + + t* (t+1)/(LL) 2;
                Break
     }} else {int id, cnt = 0;       LL A = 0, b = 0;
                    for (int i = 1; I <= n; i++) {if (rec[i] = = 1) {cnt++;
                    if (cnt = = 1) id = i;
                        else if (cnt = = K+1) {ans + = (a+1) * (b+1);
                        A = 0, b = 0, cnt-= 1;
                        id++;
                            while (ID <= n && rec[id] = = 0) {a++;
                        id++;
                    }} else {if (cnt = 0) a++;
                else if (cnt = = k) b++; } if (cnt = = k) ans + = (a+1) * (b+1);/* After the end of the traversal is just CNT cumulative arrival K of the situation needs to consider */} printf ("%lld\n
    ", ans);
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.