top of page

CCC '21 J4 - Arranging Books--C++ version

#include <iostream> #include <string> using namespace std; int main(){ string whole; cin >> whole; int L = 0; int M = 0; int S = 0; for(int i = 0; i< whole.size(); i++){ //get L. M. S if(whole[i] == 'L'){ L++; } if(whole[i] == 'M'){ M++; } if(whole[i] == 'S'){ S++; } } int B1[3] = {0}; int B2[3] = {0}; int B3[3] = {0}; for(int i = 0; i< L; i++){ //get B1 L M S if(whole[i] == 'L'){ B1[0]++; } if(whole[i] == 'M'){ B1[1]++; } if(whole[i] == 'S'){ B1[2]++; } } for(int i = L; i< L+M; i++){ //get B2 L M S if(whole[i] == 'L'){ B2[0]++; } if(whole[i] == 'M'){ B2[1]++; } if(whole[i] == 'S'){ B2[2]++; } } for(int i = L+M; i< whole.size(); i++){ //get B3 L M S if(whole[i] == 'L'){ B3[0]++; } if(whole[i] == 'M'){ B3[1]++; } if(whole[i] == 'S'){ B3[2]++; } } int swap = 0; int a = min(B1[1], B2[0]); //B1 B2 L---M swap += a; B1[1] -= a; B2[0] -= a; a = min(B1[2], B3[0]); //B1 B3 L---S swap += a; B1[2] -= a; B3[0] -= a; a = min(B2[2], B3[1]); //B2 B3 M----S swap += a; B2[2] -= a; B3[1] -= a; a = 2*B1[1]+ 2*B1[2]; //two step swap: SLM or MSL swap += a; cout<<swap<<endl; }

Recent Posts

See All
CCC '24 J5 - Harvest Waterloo

#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack> using namespace std; int main() { int r, c,...

 
 
 
CCC '24 J4 - Troublesome Keys

s1 = input() s2 = input() silly = '' silly_o = '' quiete = '-' i = 0 j = 0 while i < len(s1) and j < len(s2): if s1[i] != s2[j]: if...

 
 
 
CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int,...

 
 
 

Comentários


One-time Donations
I hope I can solve all CCC problems!,
Please e-transfer cccottawa862008@gmail.com

Subscribe to AIOICode newsletter

I'm a title. ​Click here to edit me.

Thanks for submitting!

  • Twitter
  • Facebook
  • Linkedin

© 2023 by AIOICode.

bottom of page