AtCoder Grand Contest #026 A - Colorful Slimes 2

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

  • 2≤N≤1002≤N≤100
  • 1≤ai≤N1≤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);
    }

}

原文地址:https://www.cnblogs.com/8023spz/p/9374713.html

时间: 2024-08-30 11:00:51

AtCoder Grand Contest #026 A - Colorful Slimes 2的相关文章

AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划

原文链接 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ 个 $'a'$ 和 $n$ 个 $'b'$ . 现在,让你按照原顺序取出一些字符,按照原顺序组成新的字符串,输出所有满足条件的字符串中字典序最大的?(字典序: $b>a>""$) 条件限制:当且仅当取了原序列的第 $i$ 个 $'a'$ 时,原序列的第 $i$ 个 $'b'$ 也被取了. $n\leq 3000$ 题解 代码 #include <bits/stdc++.h

Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划

原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a_i\) 为"奇数位上的数":\(i\) 为偶数时, \(a_i\) 为"偶数位上的数".定义左.右两端的数分别表示 \(a_1\) 和 \(a_n\). 考虑第一步: 首先,如果 A 取了左右某一个端点,那么他必然能取走和他取的点奇偶性相同的所有点. 然后,我们考虑

AtCoder Grand Contest 011

AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\(n\)个乘客到达了飞机场,现在他们都要坐车离开机场.第\(i\)个乘客到达的时间是\(T_i\),一个乘客必须在\([T_i,T_i+k]\)时刻做到车,否则他会生气.一辆车最多可以坐\(C\)个人.问最少安排几辆车可以让所有人都不生气. 题解 从前往后贪心即可. #include<iostream

AtCoder Grand Contest 016

AtCoder Grand Contest 016 A - Shrinking 你可以进行一个串的变换,把一个长度为\(n\)的串\(S\)可以变成长度为\(n-1\)的串\(T\),其中\(T_i\)要么是\(S_i\)要么是\(S_{i+1}\). 现在问你最少进行多少次这个操作,能够使最终得到的\(T\)只由一个字符构成. \(|S|\le 100\) 首先枚举最终字符是哪一个.那么首先在\(S\)末尾加上一个这个字符,那么这个最小步数等于对于所有位置而言,离它最近的枚举的字符到这个位置的

AtCoder Grand Contest 025 Problem D

www.cnblogs.com/shaokele/ AtCoder Grand Contest 025 Problem D Time Limit: 2 Sec Memory Limit: 1024 MB Description Takahashi is doing a research on sets of points in a plane. Takahashi thinks a set \(S\) of points in a coordinate plane is a good set w

AtCoder Grand Contest 024 Problem E(动态规划)

www.cnblogs.com/shaokele/ AtCoder Grand Contest 024 Problem E Time Limit: 2 Sec Memory Limit: 1024 MB Description Find the number of the possible tuples of sequences (\(A_0,A_1,-,A_N\)) that satisfy all of the following conditions, modulo \(M\): ? Fo

AtCoder Grand Contest 014

AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中有一个人的饼干数量是奇数的时候停止,求会进行几次这样子的操作,或者会永远进行下去. 首先无解的情况一定是三个数都是相等的偶数. 否则直接暴力模拟就行了.(盲猜答案不会很大) 证明一下答案的范围:不妨令\(A\le B\le C\),那么最大值和最小值之间的差就是\(C-A\),那么执行完一次操作之后

【Atcoder Grand Contest 020 E】 Encoding Subsets

Atcoder Grand Contest 020 E 题意:给一个\(0-1\)字符串,如果其中有一段重复,就可以表示成\((\)这一块的表示\(\times\)出现次数\()\). 问这个字符串的所有子集中有多少种表示方法. 思路:考虑\(dp(s)\)表示字符串\(s\)的答案. 那么我们得考虑第一个表示成的位置是什么. ①第一位就是表示的第一位,不参与循环.那么转移到\(dp(s.substr(1))\),并且如果这位是\(1\),那么乘上\(2\),因为这位可能是\(0\). ②一个前

Atcoder Grand Contest 018 E - Sightseeing Plan

Atcoder Grand Contest 018 E - Sightseeing Plan 枚举从第二个矩形的 \((x_1,y_1)\) 进入,\((x_2,y_2)\) 出来,那么中间可以选的点的数量是 \(x_2+y_2-x_1-x_2+1\) ,也就是说对于每一条合法路线,从 \((x_1,y_1)\) 进入的贡献为 \(-x_1-x_2\) ,从 \((x_2,y_2)\) 出来的贡献为 \(x_2+y_2+1\) ,枚举一下第二个矩形边界上的点,我们只需要分别计算某个点到第一个矩形