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, sr, sc; cin >> r; cin >> c; int p[r][c]; bool v

CCC '24 J4 - Troublesome Keys

#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main() { string ps; string ds; cin >> ps; cin >> ds;

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int, int> b){ return a.first < b.first; } bool colcom(pair<int,

Comments


bottom of page