zoukankan      html  css  js  c++  java
  • AtCoder Grand Contest #026 A

    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 200200 points

    Problem Statement

    Takahashi lives in another world. There are slimes (creatures) of 1000010000 colors in this world. Let us call these colors Color 1,2,...,100001,2,...,10000.

    Takahashi has NN slimes, and they are standing in a row from left to right. The color of the ii-th slime from the left is aiai. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.

    Takahashi can change the color of one slime to any of the 1000010000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?

    Constraints

    • 2N1002≤N≤100
    • 1aiN1≤ai≤N
    • All values in input are integers.

    Input

    Input is given from Standard Input in the following format:

    NN
    a1a1 a2a2 ...... aNaN
    

    Output

    Print the minimum number of spells required.


    Sample Input 1 Copy

    Copy
    5
    1 1 2 2 2
    

    Sample Output 1 Copy

    Copy
    2
    

    For example, if we change the color of the second slime from the left to 44, and the color of the fourth slime to 55, the colors of the slimes will be 1,4,2,5,21,4,2,5,2, which satisfy the condition.


    Sample Input 2 Copy

    Copy
    3
    1 2 1
    

    Sample Output 2 Copy

    Copy
    0
    

    Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.


    Sample Input 3 Copy

    Copy
    5
    1 1 1 1 1
    

    Sample Output 3 Copy

    Copy
    2
    

    For example, if we change the colors of the second and fourth slimes from the left to 22, the colors of the slimes will be 1,2,1,2,11,2,1,2,1, which satisfy the condition.


    Sample Input 4 Copy

    Copy
    14
    1 2 2 3 3 3 4 4 4 4 1 2 3 4
    

    Sample Output 4 Copy

    Copy
    4

    对于连续相同的只需要把一半的换颜色就可以了。
    代码:
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int a = 0;
            int b = 0;
            int c = 0;
            int ans = 0;
            for(int i = 0;i < n;i ++) {
                b = in.nextInt();
                if(a == b)c ++;
                else {
                    ans += c / 2;
                    c = 1;
                }
                a = b;
            }
            ans += c / 2;
            System.out.println(ans);
        }
    
    }
  • 相关阅读:
    递归函数底层原理浅析
    lambda expression & mutable
    命令mv
    printf的参数
    程序结构之静态本地变量
    汇编.align指令
    程序结构之全局变量
    命令touch
    更改gcc默认版本,实现gcc版本升降级
    命令chmod
  • 原文地址:https://www.cnblogs.com/8023spz/p/9374713.html
Copyright © 2011-2022 走看看