Codeforces 555A Case of Matryoshkas doll

Source: Internet
Author: User
Tags cmath

Codeforces 555A Case of Matryoshkas doll

N dolls numbered 1 ~ N. The doll on a chain must meet the requirement that the doll with a small number be placed on the doll with a large number. Two operations are required: 1. Remove the doll with the largest number. 2. Place a doll on a chain on a doll that is not on any other chain (a little winding ). For example, if there is a chain 1 → 2 → 4 → 5, you can remove 5, and then turn it into two chains 1 → 2 → 4 and 5, or set this chain to 6, but 6 cannot be mounted on any other chain, and it becomes 1 → 2 → 4 → 5 → 6. The question requires that all dolls be put on a chain with the minimum number of steps.

 

 

 

 

 

It is quite simple to think clearly. Each doll requires a maximum of two steps-split and set. We can think like this. On the chain where the doll numbered 1 is located, 1 does not need to be removed. That is, no operation is required. If 1 set is on 2, 2 does not need to be split, and if 2 sets are on 3, 3 does not need to be split ...... similarly, numbers starting from 1 on this chain do not need to be split. Until a place is not continuous, for example, 1 → 2 → 3 → 4 → 6 → 7 → 8, 4 of which are on 6, then 5 must be on another chain, remove all dolls after 6 (including 6), connect 5 to 6, and then connect 6 to 8. From this point of view, numbers starting from 1 on this chain do not need to be split. In the event of a non-continuous occurrence, all the following items should be removed from the non-continuous occurrence of the doll. For other chains, we set the doll with the smallest number as A, because it is not 1, so there are dolls smaller than it need to be placed on, however, the subject stipulates that A cannot be set on other dolls, and A is the smallest doll on this chain, so all the dolls except A on this chain should be removed. From this point of view, all other chains must be completely detached. The smallest doll on each chain does not need to be split, but requires a set. For dolls to be split, you must perform the operation twice, that is, split + sets. For dolls that do not need to be split but need to be set, you need to perform the operation once. For dolls that do not need to be split or set, no operation is required. In this way, you can directly calculate. It can be proved that this is definitely the least number of operation steps. On the one hand, this is the minimum number of steps required. On the other hand, except for the continuous number not removed from 1, all the remaining dolls are split into a single one. Therefore, you can directly set the numbers one by one without any additional steps. Therefore, this must be the operation with the minimum number of steps.

The chain where the doll numbered 1 is located has a total of cnt numbers starting from 1, and the answer is 2*(n-cnt-(k-1 )) + 1 * (k-1) = 2 * (n-cnt)-k + 1 is the formula. So we only need to find the cnt and use the formula to calculate it.

 

 

 

 

 

 

 

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       #include 
       
        #include 
        
         #include
         #include 
          
           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;}
          
        
       
      
     
    
   
  
 

 

Zookeeper

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.