Submission

Status:

PPPPPPPPPPPPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: solarsunny

Problemset: Chord and Inversions

Language: cpp

Time: 0.024 second

Submitted On: 2025-10-06 18:32:01

#include <bits/stdc++.h>
using namespace std;

string notes[12] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

pair<int,bool> convertnotestoindex(string a) {
    bool major = true;
    if(a[a.size()-1] == 'm') {
        a.pop_back();
        major = false;
    }
    for(int i=0; i<12; i++) {
        if(notes[i] == a) {
            return make_pair(i,major);
        }
    }
    return make_pair(0,false);
}

string convertindextonotes(int a) {
    return notes[a];
}

int distancebetweennotes(int a, int b) {
    return min((a-b+12)%12,(b-a+12)%12);
}

void chord(string a, int *c) {
    auto p = convertnotestoindex(a);
    int root = p.first;
    bool isMajor = p.second;
    c[0] = root;
    c[1] = ((isMajor ? 4:3)+root)%12;
    c[2] = (7+root)%12;
}

void chordinversion(int* c, int w) {
    int temp1,temp2;
    for(int i=0; i<w; i++) {
        temp1 = c[1];
        temp2 = c[2];
        c[2] = c[0];
        c[0] = temp1;
        c[1] = temp2;
    }
    return;
}

int distancebetweenchords(int *c, int *d) {
    return distancebetweennotes(c[0], d[0]) + 
           distancebetweennotes(c[1], d[1]) + 
           distancebetweennotes(c[2], d[2]);
}

void output(int *c) {
    cout << convertindextonotes(c[0]) << " " << 
    convertindextonotes(c[1]) << " " << convertindextonotes(c[2]) << "\n";
    return;
}

void copy(int *dest, int *source) {
    dest[0] = source[0];
    dest[1] = source[1];
    dest[2] = source[2];
    return;
}

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
    int n;
    cin >> n;
    int w;
    cin >> w;
    int start[n];
    string x;
    cin >> x;
    int c[3];
    chord(x,c);
    chordinversion(c,w);
    output(c);
    for(int i=1; i<n; i++) {
        cin >> x;
        int bestchord[3];
        int c2[3];
        chord(x,c2);
        int m = distancebetweenchords(c,c2);
        copy(bestchord,c2);
        chordinversion(c2,1);
        int d= distancebetweenchords(c,c2);
        if(d < m) {
            copy(bestchord,c2);
            m=d;
        }
        chordinversion(c2,1);
        d= distancebetweenchords(c,c2);
        if(d < m) {
            copy(bestchord,c2);
            m=d;
        }
        output(bestchord);
        copy(c,bestchord);
    }
    return 0;
}