NC51033. Booksort
描述
The Leiden University Library has millions of books. When a student wants to borrow a certain book, he usually submits an online loan form. If the book is available, then the next day the student can go and get it at the loan counter. This is the modern way of borrowing books at the library.
There is one department in the library, full of bookcases, where still the old way of borrowing is in use. Students can simply walk around there, pick out the books they like and, after registration, take them home for at most three weeks.
Quite often, however, it happens that a student takes a book from the shelf, takes a closer look at it, decides that he does not want to read it, and puts it back. Unfortunately, not all students are very careful with this last step. Although each book has a unique identification code, by which the books are sorted in the bookcase, some students put back the books they have considered at the wrong place. They do put it back onto the right shelf. However, not at the right position on the shelf.
Other students use the unique identification code (which they can find in an online catalogue) to find the books they want to borrow. For them, it is important that the books are really sorted on this code. Also for the librarian, it is important that the books are sorted. It makes it much easier to check if perhaps some books are stolen: not borrowed, but yet missing.
Therefore, every week, the librarian makes a round through the department and sorts the books on every shelf. Sorting one shelf is doable, but still quite some work. The librarian has considered several algorithms for it, and decided that the easiest way for him to sort the books on a shelf, is by sorting by transpositions: as long as the books are not sorted,
One such sequence of steps is called a transposition.
The following picture may clarify the steps of the algorithm, where X denotes the first block of books, and Y denotes the second block.
Original situation: | |
After step 1: | |
After step 2: | |
After step 3: |
Of course, the librarian wants to minimize the work he has to do. That is, for every bookshelf, he wants to minimize the number of transpositions he must carry out to sort the books. In particular, he wants to know if the books on the shelf can be sorted by at most 4 transpositions. Can you tell him?
输入描述
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with one integer n with : the number of books on a certain shelf.One line with the n integers 1, 2, …, n in some order, separated by single spaces: the unique identification codes of the n books in their current order on the shelf.
输出描述
For every test case in the input file, the output should contain a single line, containing:
if the minimal number of transpositions to sort the books on their unique identification codes (in increasing order) is T ≤ 4, then this minimal number T;i
f at least 5 transpositions are needed to sort the books, then the message "5 or more".
示例1
输入:
3 6 1 3 4 6 2 5 5 5 4 3 2 1 10 6 8 5 3 4 7 2 9 1 10
输出:
2 3 5 or more
C++14(g++5.4) 解法, 执行用时: 15ms, 内存消耗: 408K, 提交时间: 2020-08-22 15:52:21
#include<bits/stdc++.h> using namespace std; int n; int a[20]; int check() { int tot=0; for(int i=1;i<n;i++) if(a[i]+1!=a[i+1]) tot++; return tot; } int f(int tot) { return ceil((double)tot/3); } bool dfs(int depth,int max_depth) { int tot=check(); if(!tot) return true; if(depth+f(tot)>max_depth) return false; int temp[20]; memcpy(temp,a,sizeof a); for(int len=1;len<=n;len++) { for(int l=1;l+len-1<=n;l++) { int r=l+len-1; for(int k=r+1;k<=n;k++) { int x=l; for(int i=r+1;i<=k;i++) a[x++]=temp[i]; for(int i=l;i<=r;i++) a[x++]=temp[i]; if(dfs(depth+1,max_depth)) return true; memcpy(a,temp,sizeof temp); } } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); int T;cin>>T; while(T--) { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; int f=0; int b[20]; memcpy(b,a,sizeof a); for(int i=0;i<=4;i++) { memcpy(a,b,sizeof b); if(dfs(0,i)) { cout<<i<<endl; f=1; break; } } if(!f) cout<<"5 or more\n"; } return 0; }
C++(clang++ 11.0.1) 解法, 执行用时: 7ms, 内存消耗: 384K, 提交时间: 2023-08-09 21:50:37
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #define int long long using namespace std; int t,n,a[21],dep; inline int gj(){ int cnt=0; for(int i=1;i<n;++i)if(a[i]+1!=a[i+1])++cnt; if(a[n]!=n)return cnt+1; return cnt; } inline void work(int l,int r,int t){ int b[21],p=r; for(int i=l;i<=t;++i){ b[i]=a[++p]; if(p==t)p=l-1; } for(int i=l;i<=t;++i)a[i]=b[i]; } inline int dfs(int now){ int cnt=gj(),c[21]; if(!cnt)return 1; if(3*now+cnt>3*dep)return 0; memcpy(c,a,sizeof(c)); for(int l=1;l<=n;++l) for(int r=l;r<=n;++r) for(int t=r+1;t<=n;++t){ work(l,r,t); if(dfs(now+1))return 1; memcpy(a,c,sizeof(a)); } return 0; } inline void booksort(){ scanf("%lld",&n); for(int i=1;i<=n;++i)scanf("%lld",&a[i]); for(dep=0;dep<=4;++dep){ if(dfs(0)){ printf("%lld\n",dep); return; } } puts("5 or more"); } signed main(){ for(scanf("%lld",&t);t--;)booksort(); }