zoukankan      html  css  js  c++  java
  • (贪心) Vacations 求休息的最少天数

    Description

    Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:

    1. on this day the gym is closed and the contest is not carried out;
    2. on this day the gym is closed and the contest is carried out;
    3. on this day the gym is open and the contest is not carried out;
    4. on this day the gym is open and the contest is carried out.

    On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).

    Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.

    Input

    The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations.

    The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where:

    • ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out;
    • ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out;
    • ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out;
    • ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.

    Output

    Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:

    • to do sport on any two consecutive days,
    • to write the contest on any two consecutive days.

    Sample Input

    Input

    4
    1 3 2 0

    Output

    2

    Input

    7
    1 3 3 2 1 2 3

    Output

    0

    Input

    2
    2 2

    Output

    1

    用k记录每天的状态,b[i]记录从第一天到第i天休息的天数,每一天的状态只与前一天有关,尽量保证能工作就不休息

     1 // 0 健身房关门   没比赛
     2 // 1 健身房关门   有比赛 
     3 // 2 健身房开门   没比赛 
     4 // 3 健身房开门   有比赛
     5 #include <cstdio>
     6 int n,a[150],k,i,b[150];
     7 int main()
     8 {
     9 
    10     while(scanf("%d",&n)!=EOF)
    11     {
    12         b[0]=0;
    13         k=0;
    14         for(int i=1;i<=n;i++)
    15         {
    16             scanf("%d",&a[i]);
    17         }
    18         for(int i = 1 ; i  <= n ; i++)
    19         {
    20             if(a[i] == 0) 
    21             {
    22                 b[i]=b[i-1]+1;
    23                 k=0;
    24             }
    25             else
    26             {
    27                 if(a[i] == 3) 
    28                 {
    29                     b[i]=b[i-1];
    30                     k=3-k;
    31                 }
    32                 else
    33                 {
    34                     if(a[i] == k)
    35                     {
    36                         b[i]=b[i-1]+1;
    37                         k=0;
    38                     }
    39                     else
    40                     {
    41                         b[i]=b[i-1];
    42                         k=a[i];
    43                     }
    44                 }
    45             }
    46         }
    47         printf("%d
    ",b[n]);
    48     }
    49 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    深入理解javascript的this关键字
    很简单的JQuery网页换肤
    有关垂直居中
    层的半透明实现方案
    常用meta整理
    web前端页面性能优化小结
    关于rem布局以及sprit雪碧图的移动端自适应
    mysql入过的坑
    日期格式化函数
    基于iframe父子页面传值的方法。
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5699171.html
Copyright © 2011-2022 走看看