codeforces 解题报告 556C. Case of Matryoshkas

http://codeforces.com/problemset/problem/556/C

解题思路:

1. n个套娃,编号1~n。分成k组。

2.两种操作如下:

  • 从一组套娃里拿出最外层的一个
  • 把一个相对大的套娃套在一个相对小的外面

3.有编号1的套娃组,需要找到最大连续套娃(1号套娃到某个套娃),这个连续套娃作为一个整体不需要任何操作

4.其他组的最小套娃不需要取出操作,只要套操作

5.剩余的套娃都需要取出和套两个操作

6.总操作数就是答案

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(),k = sc.nextInt();
        int ans = 0;
        for(int i = 1;i <= k;i++) {
            int x = sc.nextInt();
            int sum = 0,q;
            boolean tag = true;
            for(int j = 1;j <= x;j++) {
                q = sc.nextInt();
                if(tag == true && q == j) {
                    sum++;
                } else {
                    tag = false;
                }
            }
            if(sum == 0) {
                ans += 2 * x - 1;
            } else {
                int res = x - sum + 1;
                ans += 2 * (res - 1);
            }
        }
        System.out.println(ans);
    }
}

猜你喜欢

转载自blog.csdn.net/a912952381/article/details/81033012