Codeforces 555A Case of Matryoshkas set doll

Source: Internet
Author: User

Test instructions: There are n dolls, numbered 1 ~ N. A doll on a chain must be satisfied with a small number of dolls on a large numbered doll. Specify two operations: 1, remove the largest number of dolls. 2. Put the doll on a chain on a doll that is not set on any other chain (a bit around). For example, there is a chain 1→2→4→5, you can remove 5, then become two chains 1→2→4 and 5, or set the chain on 6, but 6 can not be set on any other chain, into 1→2→4→5→6. The title requires a minimum number of steps to put all the dolls on a chain.






It's actually quite simple to think about it. Each doll requires up to two steps--split and set. We can think of that. On the chain of the doll numbered 1, 1 does not need to be torn down. That is, you do not have to do anything. If 1 sets on 2, then 2 does not have to be torn down, and then if 2 sets on 3, then 3 do not have to dismantle ... And so on, the number of consecutive numbers on this chain starting from 1 is not to be demolished. Until a place is not contiguous, such as 1→2→3→4→6→7→8, of which 4 are on 6, then 5 must be on another chain, 6 (including 6) after all the dolls are removed, and then 5 to go up, and then 6 7 8 to go up. Thus, the number of consecutive numbers on this chain starting from 1 does not need to be dismantled. Once a discontinuity is encountered, all the back of this discontinuous doll is removed. For the other chains, we set the smallest doll as a, because it is not 1, so there is a smaller doll than it needs to be set on a, but the title of the doll when a can not be set on the other dolls, and a is the smallest doll on the chain, so this chain except a doll is all to be torn down. As a result, the other chains must be completely dismantled on each chain. and the smallest doll on each chain doesn't have to be torn apart, but it needs a set. For the doll to be removed, must be operated 2 times, that is, split + sets. For dolls that do not need to be torn apart, they need to be operated 1 times. For dolls that do not have to be disassembled or set, no action is required. This can be calculated directly. It can be proved that this is the minimum number of steps. On the one hand, this is at least the number of steps required, on the other hand, except for the number of consecutive numbers that have not been removed from 1, all the dolls left are split into individual. So directly in the order of the number of sets can be, do not need unnecessary steps, so this must be the minimum number of steps to operate.

The number of dolls numbered 1 on the chain starting from 1 consecutive numbers have CNT, then the answer is (n-cnt-(k-1)) + 1* (k-1) = (n-cnt)-K + 1 is a formula. So we just need to ask the CNT and then the formula can be calculated.







#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string > #include <algorithm> #include <stack> #include <queue> #include <vector> #include <map  > #include <set>using namespace std;const int MAX = 100005;int N, k;void Solve () {    int m, x, temp, cnt = 1, ans = 2*n-k + 1;    BOOL Flag;    while (k--)    {        scanf ("%d", &m);        scanf ("%d", &x);        if (x = = 1)            flag = true;        else            flag = false;        temp = x;        while (--m)        {            scanf ("%d", &x);            if (x! = temp + 1)                flag = false;            if (flag)                cnt++;            temp = x;        }    }    printf ("%d\n", ans-2*cnt);} int main () {while    (scanf ("%d%d", &n, &k)! = EOF)        solve ();    return 0;}



Codeforces 555A Case of Matryoshkas set of dolls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.