zoukankan      html  css  js  c++  java
  • csuoj-1008-Horcrux

    题目:

    Description

    A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or her soul for the purpose of attaining immortality. Constructing a Horcrux is considered Dark magic of the foulest, most evil kind, as it violates laws of nature and morality, and requires a horrific act (a.k.a. murder) to accomplish.

    There are two kinds of horcruxes, the white one, denoted as , and the black one, denoted as . Topper has got N horcruxes, and he wants to destroy them to win the Dark wizard. Toper places all the horcruxes in a line from left to right, one by one, and then says a magic spell to destroy them. In order to make the magic spell works, Toper needs to know the number of the white horcruxes.

    Since the horcruxes also has magic, when placing the horcruxes, they will change color from white to black or from black to white under the following rules:

    1. When Topper places the i-th horcrux and i is an even number: If the i-th horcrux and the rightmost horcrux have different colors, all consecutive horcruxes of the same color on the right change its color.

    2. In other situations, no magic works.

    For example, suppose the horcruxes on the line are:

    △△▲▲△△△

    After placing 7 horcruxes.

    If the 8-th horcrux is white, since its color and the color of the rightmost horcrux are the same. Therefore, the horcruxes on the line become as follows:

    △△▲▲△△△△

    If the 8-th horcrux is black, since its color and the color of the rightmost horcrux are different, the 3 consecutive white stones on the right change their color. Therefore, the stones on the line become as follows:

    △△▲▲▲▲▲▲

    You see, it’s not an easy job to beat the Dark wizard. So write a program to help Topper.

    Input

    There are some test cases. In each test case, the first line contains a positive integer n (1≤n≤100,000), which is the number of horcruxes. The following n lines denote the sequence in which Topper places the horcruxes. 0 stands for white horcrux, and 1 stands for black horcrux.

    Output

    For each test case, output one line containing only the number of white horcruxes on the line after Topper places n horcruxes.

    Sample Input

    8
    1
    0
    1
    1
    0
    0
    0
    0
    8
    1
    0
    1
    1
    0
    0
    0
    1
    

    Sample Output

    6
    2
    分析:
    一开始用最笨的方法,超时了。后面改成了记录每一段连续的白色或者黑色的起始点和终止点。
    代码:
    #include<iostream>
    using namespace std;
    struct White{
        int begin;
        int end;
    }white[100005];
    struct Black{
        int begin;
        int end;
    }black[100005];
    
    int main(){
        cin.sync_with_stdio(false);
        int n;
        while(cin >> n){
            int a = 0;
            int b = 0;
            int temp;
            cin >> temp;
            if(temp == 0){
                white[a].begin = white[a].end = 1;
                a++;
            }
            else{
                black[b].begin = black[b].end = 1;
                b++;
            }
            int last = temp;
    //        cout << a << " " << b << endl;
    //        if(a != 0)
    //            cout << white[a - 1].begin << " " << white[a - 1].end << endl;
    //        if(b != 0)
    //            cout << black[b - 1].begin << " " << black[b - 1].end << endl;
            for(int i = 2;i <= n;i++){
                cin >> temp;
                if(i %  2 == 0){
                    if(temp == 0){
                        if(last == 0){
                            white[a - 1].end = i;
                        }
                        else{
                            if(a == 0){
                                white[a].begin = 1;
                                white[a].end = i;
                                b--;
                                a++;
                            }
                            else{
                                white[a - 1].end = i;
                                b--;
                            }
                        }
                    }
                    else{
                        if(last == 1){
                            black[b - 1].end = i;
                        }
                        else{
                            if(b == 0){
                                black[b].begin = 1;
                                black[b].end = i;
                                a--;
                                b++;
                            }
                            else{
                                black[b - 1].end = i;
                                a--;
                            }
                        }
                    }
                }
                else{
                    if(temp == 0 && last == 0){
                        white[a - 1].end = i;
                    }
                    else if(temp == 0 && last == 1){
                        white[a].begin = white[a].end = i;
                        a++;
                    }
                    else if(temp == 1 && last == 0){
                        black[b].begin = black[b].end = i;
                        b++;
                    }
                    else{
                        black[b - 1].end = i;
                    }
                }
                last = temp;
    //            cout << a << " " << b << endl;
    //            if(a != 0)
    //                cout << white[a - 1].begin << " " << white[a - 1].end << endl;
    //            if(b != 0)
    //                cout << black[b - 1].begin << " " << black[b - 1].end << endl;
            }
            int ans = 0;
            for(int i = 0;i < a;i++){
                ans += white[i].end - white[i].begin + 1;
            }
    //        cout << a << " " << b << endl;
            cout << ans << endl;
        }
        return 0;
    }
     
  • 相关阅读:
    等式
    Lemon 评测软件用法
    同花顺
    浅谈二分图的最大匹配和二分图的KM算法
    LCT总结
    5.30模拟赛
    树上斜率优化
    5.22 noip模拟赛
    KMP,HASH,Trie,AC自动机
    splay总结
  • 原文地址:https://www.cnblogs.com/tracy520/p/6690916.html
Copyright © 2011-2022 走看看