HihoCoder1873 (binary split)

Topic: http://hihocoder.com/problemset/problem/1873

Solution: The idea of ​​using divide and conquer, will gradually break down big problems into small problems:

  

First of all, we can be understood as a problem, my current standing pos point, m kinds of programs need to reach the finish line, so that we can discuss Category:

       <1> m is an even number: Program Number = m / 2

                 Pos + 1 based on the position of a transfer gate to pos + 3; to build a portal pos + pos + 1 in place of 2, from Scheme 3 pos pos + to a number of two kinds;

                 The problem is broken down into: I am now standing pos + 3 points required m \ 2 kinds of programs to reach the terminal;
      <2> m is odd:

                       Let m being an even number, the transfer gate 199 to place a point at pos + 1;

                        Then jump pos + 2 on the point, the question becomes: 2 points in pos +, m-1 kinds of programs need to reach the end.

Code:

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 205;
int main()
{
    ll m;
    while(cin>>m){
        int pos = 0,cnt = 0,a[maxn];
        memset(a,-1,sizeof(a));
        if(m==0){
            cout<<"2"<<endl;
            cout<<"1 1"<<endl;
            cout<<"2 1"<<endl;
            continue;
        }
        while(m!=0){
            if(m==1){
                a[pos+1] = 199; 
                a[pos+2] = pos+2;
                cnt += 2;  
                break;
            }else{
                if(m%2==0){
                    a[pos+1] = pos + 3;
                    a[pos+2] = pos + 1; 
                    pos += 3; m /= 2; 
                    cnt += 2;
                }else{
                    a[pos+1] = 199;
                    pos += 2;   m--;  
                    cnt++;
                }
            }
            
            
        }
        cout<<cnt<<endl;
        for(int i=0;i<199;i++){
            if(a[i]==-1) continue;
            cout<<i<<" "<<a[i]<<endl; 
        }
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Lemon1234/p/11655561.html