|
|
вернуться в форумWA #6 Послано Arlen 2 апр 2021 07:24 Hello. My program passes all tests from the forum, but I still get WA6. I do brute force First, I go through all the elements that are to the right of the current one, then everything that is to the left can anyone have any tests? here is y code is needed: #include <iostream> using namespace std; int n, result = 0, sizeHelpArray = 0, sizeAnswerArray; int line[100][2], helpArray[100][2], answerArray[100][2]; //initialization and sorting on the left border void Init() { int i, j; cin >> n; for (i = 0; i < n; i++) { cin >> line[i][0] >> line[i][1]; if (line[i][0] > line[i][1]) swap(line[i][0], line[i][1]); } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (line[j][0] < line[i][0]) { swap(line[i][0], line[j][0]); swap(line[i][1], line[j][1]); } if(line[j][0] == line[i][0]) if (line[j][1] < line[i][1]) { swap(line[i][0], line[j][0]); swap(line[i][1], line[j][1]); } } } } //adding a segment to the temporary answer void AddInHelpArray(int index) { sizeHelpArray++; helpArray[sizeHelpArray][0] = line[index][0]; helpArray[sizeHelpArray][1] = line[index][1]; } //to record the best current result void copyInAnswerArray() { int i; for (i = 0; i < sizeHelpArray + 1; i++) { answerArray[i][0] = helpArray[i][0]; answerArray[i][1] = helpArray[i][1]; } } void sortAnswerArray() { for (int i = 0; i <= sizeAnswerArray; i++) { for (int j = i + 1; j <= sizeAnswerArray; j++) { if (answerArray[j][0] < answerArray[i][0]) { swap(answerArray[i][0], answerArray[j][0]); swap(answerArray[i][1], answerArray[j][1]); } if (answerArray[j][0] == answerArray[i][0]) if (answerArray[j][1] < answerArray[i][1]) { swap(answerArray[i][0], answerArray[j][0]); swap(answerArray[i][1], answerArray[j][1]); } } } } void Solve() { int i, x, tmp = 1, currentPoint; //starting from the first element .. for (x = 0; x < n; x++) { sizeHelpArray = 0; tmp = 1; memset(helpArray, 0, sizeof(helpArray)); //I write the current element to an auxiliary array helpArray[sizeHelpArray][0] = line[x][0]; helpArray[sizeHelpArray][1] = line[x][1]; currentPoint = line[x][1]; //Checking items to the right of the current one for (i = x + 1; i < n; i++) { if (line[i][0] >= currentPoint) { tmp++; currentPoint = line[i][1]; AddInHelpArray(i); } }
currentPoint = line[x][0]; int border; border = line[x][0]; bool first = true; //Checking items to the left of the current one for (int j = 0; j < x; j++) { if (line[j][1] <= currentPoint && first) { tmp++; currentPoint = line[j][1]; AddInHelpArray(j); first = false; } else if (first == false && line[j][0] >= currentPoint && line[j][1] <= border) { tmp++; currentPoint = line[j][1]; AddInHelpArray(j); } } //If the result is better than the previous one, then I save it. if (tmp > result) { result = tmp; copyInAnswerArray(); sizeAnswerArray = sizeHelpArray; } } sortAnswerArray(); cout << result << endl; for (int i = 0; i <= sizeAnswerArray; i++) cout << answerArray[i][0] << " " << answerArray[i][1] << endl; } int main() { Init(); Solve(); return 0; } Edited by author 02.04.2021 07:26 Edited by author 02.04.2021 07:26 Edited by author 02.04.2021 07:37 Edited by author 02.04.2021 07:37 |
|
|