0

Problem-http://codeforces.com/contest/454/problem/B

Problem Synopsis- A given integer sequence is to be changed into ascending order by applying shifting operations(each element is shifted by 1 place to its right, the last element becomes the first). Find minimum number of operations to make the sequence ascending. Print -1 if it is not possible.

I'm getting a TIME_LIMIT_EXCEEDED for test case 6 in the above link. I know that there are more efficient solutions for this problem but if possible, I would like to make changes in my solution to make it run within the time limit. Would that be possible? If yes, how can I do that?

My solution:

#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>

typedef long long LL;

using namespace std;

bool checkAsc(vector<int> x, int N)
{
    for(int i=0;i<(N-1);i++)
    {
        if(x[i+1]<x[i])
            return false;
    }

    return true;
}

vector <int> shiftRight(vector <int> &x, int N)
{
    int temp=x[N-1];

    for(int i=(N-1);i>=1;i--)
    {
        x[i]=x[i-1];
    }

    x[0]=temp;

    vector <int> y=x;

    return y;
}

bool ifPossible(vector <int> x, int N)
{
    for(int i=1;i<(N-1);i++)
    {
        if((x[i]>x[i-1])&&(x[i]>x[i+1])&&(x[i+1]>x[i-1]))
            return false;
    }

    return true;
}

int main()
{
    int n, turns=0;
    vector <int> a(100000);

    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];

    if(!ifPossible(a, n))
        cout<<"-1";

    else
    {
        while(!checkAsc(a, n))
        {
            shiftRight(a, n);
            turns++;
        }

        cout<<turns;
    }

    return 0;
}

Checker's log:

Test: #6, time: 1000 ms., memory: 784 KB, exit code: -1, checker exit code: 0, verdict: TIME_LIMIT_EXCEEDED Input 99998

99997 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15...

vishal_khanna
  • 57
  • 1
  • 9
  • A synopsis of the problem, rather than just a link, would be a good idea for the same reason that link-only answers are discouraged: links rot over time and if that link dies, the usefulness of this question for future users will die with it. – Emmet Aug 02 '14 at 08:37

1 Answers1

0

First, fix your algorithm, for {3, 2, 1}, you have infinite loop (instead of -1).

From your code, pass vector by const ref instead of value to avoid useless copies. your shiftRight may use vector.insert(v.back()); vector.pop_back() which is same complexity, but use some faster method (as memcpy); and change return type to void(as the result is not used) and so discard y and its copy.

But your algorithm is still O(N²) whereas it can be done in O(N)

int get_unicorn_shift(const std::vector<int>& v)
{
    auto mid = std::is_sorted_until(v.begin(), v.end());
    if (mid == v.end()) {
        return 0;
    }
    auto end = std::is_sorted_until(mid, v.end());
    if (end != v.end() || v.front() < v.back()) {
        return -1;
    }
    return end - mid;
}

Live example

Jarod42
  • 203,559
  • 14
  • 181
  • 302