Advertisement
fireLUFFY

Bulbs

Oct 7th, 2021 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5.  
  6. void n2(int& ans,string& s)
  7. {
  8.     if(s[0]=='1' && s[1]=='1')
  9.     {
  10.         ans+=2;
  11.         s[0]='0',s[1]='0';
  12.     }
  13.     else if(s[0]=='1' && s[1]=='0')
  14.     {
  15.         ans+=3;
  16.         s[0]='0';
  17.     }
  18.     else if(s[0]=='0' && s[1]=='1')
  19.     {
  20.         ans+=1;
  21.         s[1]='0';
  22.     }
  23. }
  24.  
  25. void func(int ind, string& s, bool flip, int& ans,int pos)
  26. {
  27.     if(ind==(n-3))
  28.     {
  29.         if(s[ind]=='1' && s[ind+1]=='1' && s[ind+2]=='1')
  30.         {
  31.             if(!flip)
  32.             {
  33.                 ans+=5;
  34.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  35.             }
  36.             else
  37.             {
  38.                 ans+=2;
  39.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  40.             }
  41.         }
  42.         else if(s[ind]=='1' && s[ind+1]=='1' && s[ind+2]=='0')
  43.         {
  44.             if(!flip)
  45.             {
  46.                 ans+=4;
  47.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  48.             }
  49.             else
  50.             {
  51.                 ans+=3;
  52.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  53.             }
  54.         }
  55.         else if(s[ind]=='1' && s[ind+1]=='0' && s[ind+2]=='1')
  56.         {
  57.             if(!flip)
  58.             {
  59.                 ans+=4;
  60.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  61.             }
  62.             else
  63.             {
  64.                 ans+=3;
  65.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  66.             }
  67.         }
  68.         else if(s[ind]=='1' && s[ind+1]=='0' && s[ind+2]=='0')
  69.         {
  70.             if(!flip)
  71.             {
  72.                 ans+=7;
  73.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  74.             }
  75.             else
  76.             {
  77.                 ans+=0;
  78.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  79.             }
  80.         }
  81.         else if(s[ind]=='0' && s[ind+1]=='1' && s[ind+2]=='1')
  82.         {
  83.             if(!flip)
  84.             {
  85.                 ans+=2;
  86.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  87.             }
  88.             else
  89.             {
  90.                 ans+=5;
  91.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  92.             }
  93.         }
  94.         else if(s[ind]=='0' && s[ind+1]=='1' && s[ind+2]=='0')
  95.         {
  96.             if(!flip)
  97.             {
  98.                 ans+=3;
  99.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  100.             }
  101.             else
  102.             {
  103.                 ans+=4;
  104.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  105.             }
  106.         }
  107.         else if(s[ind]=='0' && s[ind+1]=='0' && s[ind+2]=='1')
  108.         {
  109.             if(!flip)
  110.             {
  111.                 ans+=1;
  112.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  113.             }
  114.             else
  115.             {
  116.                 ans+=6;
  117.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  118.             }
  119.         }
  120.         else
  121.         {
  122.             if(!flip)
  123.             {
  124.                 ans+=0;
  125.                 s[ind]='0',s[ind+1]='0',s[ind+2]='0';
  126.             }
  127.             else
  128.             {
  129.                 ans+=7;
  130.                 s[ind]='1',s[ind+1]='0',s[ind+2]='0';
  131.             }
  132.         }
  133.  
  134.         return;
  135.     }
  136.  
  137.     if(flip)
  138.     {
  139.         ans+=1;
  140.         if((pos+1 == ind+1) && (ind+1 != n-3))
  141.         {
  142.             if(s[ind+1]=='1')
  143.                 func(ind+1,s,false,ans,pos);
  144.             else
  145.             {
  146.                 func(ind+1,s,true,ans,pos);
  147.                 s[ind+1]='1';
  148.             }
  149.         }
  150.         else if((pos+1 == ind+1) && (ind+1 == n-3))
  151.             func(ind+1,s,true,ans,pos);
  152.         else
  153.         {
  154.             func(ind+1,s,false,ans,pos);
  155.             s[ind+1]='0';
  156.         }
  157.     }
  158.     else
  159.     {
  160.         if(ind+1 == n-3)
  161.             func(ind+1,s,false,ans,pos);
  162.         else
  163.         {
  164.             if(s[ind+1]=='1')
  165.             {
  166.                 func(ind+1,s,true,ans,pos);
  167.                 s[ind+1]='0';
  168.             }
  169.             else
  170.                 func(ind+1,s,false,ans,pos);
  171.         }
  172.     }
  173.     return;
  174. }
  175.  
  176. main()
  177. {
  178.     string s;cin>>s;
  179.     n=s.length();int ans=0;
  180.  
  181.     if(n==1)
  182.     {
  183.         if(s[0]=='1')
  184.             ans++;
  185.         cout<<ans<<endl;
  186.     }
  187.     else if(n==2)
  188.     {
  189.         n2(ans,s);
  190.         cout<<ans<<endl;
  191.     }
  192.     else
  193.     {
  194.         for(int i=0;i<n-2;i++)
  195.         {
  196.             if(i==(n-3) && s[i]=='1')
  197.             {
  198.                 func(i,s,false,ans,i);
  199.                 s[i]='0';
  200.             }
  201.             if(s[i]=='1')
  202.             {
  203.                 func(i,s,true,ans,i);
  204.                 s[i]='0';
  205.             }
  206.              cerr<<s<<endl;
  207.              cerr<<"ans: "<<ans<<endl;
  208.         }
  209.         cout<<ans<<endl;
  210.     }
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement