#include #include #include #include using namespace std; struct WordInfo { string word; int coordX, coordY; int length, height; }; struct TemplateInfo { int id; string wordTop, wordRight, wordDown, wordLeft; float weightWordTop = 0.0, weightWordDown = 0.0; float weightWordRight = 0.0, weightWordLeft = 0.0; }; ifstream inputStream("input.txt"); const string missingWord = "-"; int numberOfWords, numberOfTemplates, indexWantedWord; WordInfo wantedWord, wordTop, wordRight, wordDown, wordLeft; vector wordsInfo; vector templatesInfo; void ReadWords() { for( int index = 0; index < numberOfWords; ++index ) inputStream>> wordsInfo[index].word>> wordsInfo[index].coordX>> wordsInfo[index].coordY>> wordsInfo[index].length>> wordsInfo[index].height; } void ReadWordAndWeight(string& word, float& weight) { inputStream>> word; if( word != missingWord ) inputStream>> weight; } void ReadTemplates() { for( int index = 0; index < numberOfTemplates; ++index ) { inputStream>> templatesInfo[index].id; ReadWordAndWeight(templatesInfo[index].wordTop, templatesInfo[index].weightWordTop); ReadWordAndWeight(templatesInfo[index].wordRight, templatesInfo[index].weightWordRight); ReadWordAndWeight(templatesInfo[index].wordDown, templatesInfo[index].weightWordDown); ReadWordAndWeight(templatesInfo[index].wordLeft, templatesInfo[index].weightWordLeft); } } void ReadInput() { inputStream>> numberOfWords; wordsInfo.resize(numberOfWords); ReadWords(); inputStream>> numberOfTemplates; templatesInfo.resize(numberOfTemplates); ReadTemplates(); inputStream>> indexWantedWord; inputStream.close(); } void RemoveUnnecessaryWords() { wordsInfo.erase(remove_if(wordsInfo.begin(), wordsInfo.end(), [](WordInfo& word) -> bool { return ( (word.coordX != wantedWord.coordX && word.coordY != wantedWord.coordY) || (word.coordX == wantedWord.coordX && ( (word.coordY <= wantedWord.coordY && word.coordY > wantedWord.coordY - wantedWord.height) || ( word.coordY > wantedWord.coordY && word.coordY - word.height < wantedWord.coordY))) || (word.coordY == wantedWord.coordY && ( (word.coordX >= wantedWord.coordX && word.coordX < wantedWord.coordX + wantedWord.length) || (word.coordX < wantedWord.coordX && word.coordX + word.length > wantedWord.coordX )))); }), wordsInfo.end()); } bool CompareXThenY(const WordInfo& word1, const WordInfo& word2) { return word1.coordX < word2.coordX || ( word1.coordX == word2.coordX && word1.coordY < word2.coordY ); } bool CompareYThenX(const WordInfo& word1, const WordInfo& word2) { return word1.coordY < word2.coordY || ( word1.coordY == word2.coordY && word1.coordX < word2.coordX ); } void FindTopAndDownWord() { sort(wordsInfo.begin(), wordsInfo.end(), CompareXThenY); auto it = upper_bound(wordsInfo.begin(), wordsInfo.end(), wantedWord, CompareXThenY); wordTop = *it; wordDown = *--it; } void FindLeftAndRightWord() { sort(wordsInfo.begin(), wordsInfo.end(),CompareYThenX); auto it = upper_bound(wordsInfo.begin(), wordsInfo.end(), wantedWord, CompareYThenX); wordRight = *it; wordLeft = *--it; } int FindTemplateIdWithBiggestWeight() { int templateId; float biggestWeight = 0.0; for( int index = 0; index < numberOfTemplates; ++index ) { float weigth = 0.0; weigth += (templatesInfo[index].wordTop == wordTop.word ? templatesInfo[index].weightWordTop : 0.0) + (templatesInfo[index].wordRight == wordRight.word ? templatesInfo[index].weightWordRight : 0.0) + (templatesInfo[index].wordDown == wordDown.word ? templatesInfo[index].weightWordDown : 0.0) + (templatesInfo[index].wordLeft == wordLeft.word ? templatesInfo[index].weightWordLeft : 0.0); if( biggestWeight < weigth ) { biggestWeight = weigth; templateId = templatesInfo[index].id; } } return templateId; } int main() { ReadInput(); wantedWord = wordsInfo[indexWantedWord]; RemoveUnnecessaryWords(); FindTopAndDownWord(); FindLeftAndRightWord(); cout<< FindTemplateIdWithBiggestWeight(); return 0; }