Topic 1
https://practice.contest.atcoder.jp/tasks/practice_2
Two, analysis
Here are three sets of test cases.
The first group N = 26, Q = 1000. Because n * n < Q, bubble sorting can be used. Refer to the official Sample Code for specific codes.
The second group, N = 26, Q = 100, can be processed by inserting sorting method because the number of queries is only 100. Before inserting a piece of data, first use the binary search method to find the inserted position, which can improve the sorting speed. But it needs to ask 26log226 times, and still can't pass all the data. This requires us to do a memory processing for the answers of each query, record the quality relationship of the ball, and then according to the quality relationship, we can get the ordered sequence, so as to reduce the time complexity.
In the third group, N=5, Q=7. First, compare the 0 th and 1 th, 2 th and 3 th, 1 th and 3 th, then find the position of the 5th, finally find the position relationship of the 2nd and 0 th, 1 th and 5 th.
Three, code
#include<bits/stdc++.h> using namespace std; char s[29], ans[29]; int cmp['Z'+5]['Z'+5]; int cnt = 0; // Compare two characters. If a > B returns true, if a < B returns false bool cmp_char(const char a, const char b) { char cp; if(cmp[a][b] == -1) { printf("? %c %c\n", a, b); fflush(stdout); scanf(" %c", &cp); if(cp == '>') { cmp[a][b] = true; cmp[b][a] = false; return true; } else { cmp[a][b] = false; cmp[b][a] = true; return false; } } return cmp[a][b]; } // This method is called to sort when N is 5 void sort_N5(char c) { if(cmp_char(c, s[1])) { if(cmp_char(c, s[2])) { s[3] = c; } else { s[3] = s[2]; s[2] = c; } } else { if(cmp_char(c, s[0])) { s[3] = s[2]; s[2] = s[1]; s[1] = c; } else { s[3] = s[2]; s[2] = s[1]; s[1] = s[0]; s[0] = c; } } } // When N is 26, this method is called for sorting void sort_N26(char c) { int l = 0, r = cnt; while(l < r) { int mid = (l + r) >> 1; if(cmp_char(c, ans[mid])) { l = mid + 1; } else { r = mid; } } cnt++; // When the new c is larger than all the original characters, the if condition is true // For example, the original order is "AB", and the newly added "C" is larger than "B", that is, when "AB" becomes "ABC" if(cmp_char(c, ans[r])) { r++; } for(int i = cnt; i >= r + 1; i--) { ans[i] = ans[i-1]; } ans[r] = c; } int main() { int N, Q; scanf("%d%d", &N, &Q); for(int i = 0; i < 26; i++) { s[i] = (char)(i + 'A'); } s[N] = '\0'; memset(cmp, -1, sizeof(cmp)); if(N == 26) // Q=1000, or Q=100 { cnt = 0; ans[0] = s[0]; ans[N] = '\0'; for(int i = 1; i < N; i++) { sort_N26(s[i]); } printf("! %s\n", ans); } else // N=5, Q=7 { if(cmp_char(s[0], s[1])) { swap(s[0], s[1]); } if(cmp_char(s[2], s[3])) { swap(s[2], s[3]); } if(cmp_char(s[1], s[3])) { swap(s[0], s[2]); swap(s[1], s[3]); } /* * So far, s [0] < s [1], s [2] < s [3], s [1] < s [3] * There are two more questions: * s[5]Where to put; s[2] and s[0], s[1] who is big and who is small */ char temp = s[2]; if(cmp_char(s[4], s[1])) { // If s[4] > s [1], then compare s[4] with s[3] if(cmp_char(s[4], s[3])) { // s[4]>s[1], s[4]>s[3] // Fill in 0, 1, 2, and 5 to make it easy to call sort < n5() s[2] = s[3]; } else { // s[1]<s[4]<s[3] s[2] = s[4]; s[4] = s[3]; } } else { // If s[4] < s [1], then compare s[4] with s[0] if(cmp_char(s[4], s[0])) { // s[0]<s[4]<s[1] s[2] = s[1]; s[1] = s[4]; s[4] = s[3]; } else { // s[4]<s[0]<s[1] s[2] = s[1]; s[1] = s[0]; s[0] = s[4]; s[4] = s[3]; } } sort_N5(temp); printf("! %s\n", s); } return 0; }
Four, reference
https://www.cnblogs.com/TheRoadToAu/p/8463454.html
More content, please pay attention to WeChat official account.