Codeforces round #259 div2 B Question (KMP)

Source: Internet
Author: User
Tags integer numbers

Link: http://codeforces.com/contest/454/problem/ B


B. Little Pony and sort by shifttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

One day, Twilight Sparkle is interested in how to sort a sequence of IntegersA1 ,?A2 ,?...,?ANIn non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:

A1 ,? A2 ,?...,? A N? →? A N,? A1 ,? A2 ,?...,? A N? -? 1.

Help Twilight Sparkle to calculate: What is the minimum number of operations that she needs to sort the sequence?

Input

The first line contains an integerN(2? ≤?N? ≤? (105). The second line containsNInteger numbersA1 ,?A2 ,?...,?AN(1? ≤?AI? ≤? 105 ).

Output

If it's impossible to sort the sequence output-1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.

Sample test (s) Input
22 1
Output
1
Input
31 3 2
Output
-1
Input
21 2
Output
0

Question: give you n numbers and ask you to sort the N numbers into non-decreasing sequences. However, the sorting rule is that only the last number of sequences can be placed at the top of each sequence. Calculates the minimum number of times required to form a non-decreasing sequence. If not, output-1.

I had a magical idea of using KMP to solve this problem...


First, let's give a train of thought: assuming that the sequence can be sorted into a non-decreasing sequence, and the task can be executed up to n times. Then let it execute n times first.

Then I can change the length of the original sequence to 2 * n. If the sequence is 4 1 2 3 before execution, I can assume that it is 4 1 2 3 4 1 2 3.

Then, use the KMP algorithm to locate the position 1 2 3 4 (back-to-back of course), record the position d, and n-d is the number of times it is executed. If it does not match, it cannot be the same as before.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define maxn 100005int ans[maxn], v[maxn], va[maxn + maxn];int next[maxn];int n, d;using namespace std;void getnext() {int j = 0;int k = -1;next[0] = -1;while(j < n) {if(k == -1 || v[j] == v[k]) {++k;++j;next[j] = k;} else k = next[k];}}void kmp() {int i = 0, j = 0;while(i < n + n) {if(j == -1 || va[i] == v[j]) {++i;++j;} else j = next[j];if(j == n) d =  i - j,j = next[j];}}int main() {while(scanf("%d", &n) != EOF) {for(int i = 0; i < n; ++i) {scanf("%d", &ans[i]);v[i] = ans[i];}d = -1;sort(v, v + n);for(int i = 0; i < n; ++i) va[i] = va[i + n] = ans[i];/**printf("ans = \n");for(int i = 0;i < n;++i) printf("%d ",ans[i]);printf("\n");for(int i = 0;i < n;++i) printf("%d ",v[i]);printf("\n");for(int i = 0;i < n+n;++i) printf("%d ",va[i]);printf("\n");**/getnext();kmp();if(d == -1)printf("%d\n",d);else            printf("%d\n",n - d);}return 0;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.