• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ZOJ1622 SWITCH解題報告

            Posted on 2010-09-20 09:31 李東亮 閱讀(332) 評論(0)  編輯 收藏 引用

             

            SWITCH

            題目描述如下:

            There are N lights in a line. Given the states (on/off) of the lights, your task is to determine at least how many lights should be switched (from on to off, or from off to on), in order to make the lights on and off alternatively.
            Input
            One line for each testcase.
            The integer N (1 <= N <= 10000) comes first and is followed by N integers representing the states of the lights ("1" for on and "0" for off).
            Process to the end-of-file.
            Output
            For each testcase output a line consists of only the least times of switches.
            Sample Input
            3 1 1 1
            3 1 0 1
            Sample Output
            1
            0

            分析:該題看似簡單但卻隱藏著陷阱,題目要求尋找的是最少的切換數,故從第二盞燈開始判斷處理得出的結論是不一定正確的。通過分析可以發現該題其實只存在兩種情況:奇數位置的燈開著或者偶數位置的燈開著。這樣可以直觀的處理該題:取奇數位置燈開著需要切換燈狀態數與偶數位置燈開著需切換燈狀態數的較小值。這樣的話需要掃描兩邊燈的狀態數組,開銷較大。進一步分析,設a為奇數位置的燈開著需要切換的燈數,b為偶數位置燈開著需要切換的燈數。其實a+b=n。這樣本題就只需要掃描一遍數組,且進一步優化后存儲燈狀態的數組也可以省了。具體代碼如下:

             

             1#include <stdio.h>
             2#include <stdlib.h>
             3
             4int main(void)
             5{
             6    int n;
             7    int prev;
             8    int tmp;
             9    int cnt;
            10    int a;
            11    while (scanf("%d"&n) == 1)
            12    {
            13        prev = -1;
            14        cnt = 0;
            15        a = n;
            16        while (n--)
            17        {
            18            scanf("%d"&tmp);
            19            if (tmp == prev)
            20            {
            21                if (tmp == 0)
            22                {
            23                    prev = 1;
            24                }

            25                else
            26                {
            27                    prev = 0;
            28                }

            29                ++cnt;
            30                continue;
            31            }

            32            prev = tmp;
            33        }

            34        if (cnt > a/2)
            35            cnt = a-cnt;
            36        printf("%d\n", cnt);
            37    }

            38    return 0;
            39}

            posts - 12, comments - 1, trackbacks - 0, articles - 1

            Copyright © 李東亮

            久久精品国产2020| 人妻精品久久无码专区精东影业| 亚洲人成网站999久久久综合| 色偷偷88欧美精品久久久| 久久天天躁狠狠躁夜夜av浪潮| 精品人妻伦九区久久AAA片69| 99久久免费国产精品特黄| 亚洲国产另类久久久精品 | 久久久久无码精品国产| 日韩欧美亚洲综合久久影院d3| 中文精品99久久国产| 日本强好片久久久久久AAA| 久久久久久亚洲AV无码专区| 7国产欧美日韩综合天堂中文久久久久 | 狠狠色丁香婷婷综合久久来| 91麻豆精品国产91久久久久久| 欧洲性大片xxxxx久久久| 久久亚洲AV成人出白浆无码国产| 99热成人精品免费久久| 午夜精品久久久久久久| 精品水蜜桃久久久久久久| 国产美女亚洲精品久久久综合| 日韩亚洲国产综合久久久| 久久国产精品成人片免费| 久久乐国产综合亚洲精品| 欧美一级久久久久久久大| 久久精品国产亚洲av麻豆小说| 久久久国产一区二区三区| 亚洲精品成人久久久| 伊人久久大香线蕉影院95| 久久精品国产99久久无毒不卡| 思思久久精品在热线热| 青青热久久国产久精品 | 婷婷久久综合九色综合绿巨人| 国产精品免费看久久久| 久久er99热精品一区二区| 日韩人妻无码一区二区三区久久99| 7国产欧美日韩综合天堂中文久久久久 | 久久久久久国产精品免费无码| 亚洲一级Av无码毛片久久精品| 久久精品亚洲男人的天堂 |