0

For example, i want to sort something like:

1 1 1 4 4 4 4 4 3 3 2 2 2

into:

1 2 3 4 1 2 3 4 1 2 4 4 4

then print out:

8

My erroneous code:

#include<bits/stdc++.h>

using namespace std;

int main()
{
    vector <long long unsigned> a,b,c;
    long long unsigned n,k,count=0,j=1;
    cin>>n;
    for(long long unsigned i=0;i<n;i++)
    {
        cin>>k;
        a.push_back(k);
    }
    for(long long unsigned i=0;i<a.size();i++)
    {
        b.push_back(a[i]);
        if(a[i]==a[i+1])
        {
            count=count+b.size()-1;
            b.clear();
        }
        if(i==a.size()-1)
            count=count+b.size()-1;
    }
    cout<<count;
    return 0;
}

The sorting is optional, I mainly need a good method for the counting process.

Thank you.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Heggman
  • 3
  • 2
  • 3
    Is there a typo there? did you mean for the second sequence to be 1 2 3 1... ? As it is I have no idea the relationship between first, second, and third sequence/number. – Nir Friedman May 30 '17 at 03:24
  • 2
    When is your homework assignment due? – Sam Varshavchik May 30 '17 at 03:24
  • You have to show effort at solving the problem. Stack overflow is not the place to ask others to write your code for you. – t3dodson May 30 '17 at 03:24
  • Nice question - but as an aside I would be keen on knowing where such a problem shows up - I never encountered something like that before. – BitTickler May 30 '17 at 03:25
  • It's 1 2 3 4 for the first 2 sequences, then 1 2 4 (ran out of 3), then 4 4. – Heggman May 30 '17 at 03:26
  • I'm sorry, will update the question with my vain efforts so far. – Heggman May 30 '17 at 03:28
  • You will probably end up using a hash-table (map) where the key is number and the value is the number of times it shows in the input sequence. Then you use that table to greedily build sequences. – BitTickler May 30 '17 at 03:30
  • 5
    See [Why should I not not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) –  May 30 '17 at 03:34
  • 1
    @InternetAussie You forgot to point out that using naked integral types (``unsigned long long`` etc) is also bad practice. ``uint64_t`` is the better choice... (from ``#include ``) – BitTickler May 30 '17 at 03:35
  • Thank you for the advice, I do know using "using namespace std;" and "#include " are not considered good practices, but since i thought (at first) that this should only be a few nice lines of codes for my own enlightenment, I put them in. – Heggman May 30 '17 at 03:39

1 Answers1

0

I'm not sure of an "Efficient" way of sorting a vector, or array, in the manner you'd like (i.e. 1,2,3,4,1,2,3,4,1,2,4,4). But for an exhaustive way you could...

Iterate through the unsorted vector (UV) and find the minimum value above the previous runs minimum value (initialize with zero for first round and each time there is no value above the searched value). When a minimum value is found record its index and after iterating through the vector remove the value at the stored index and add this value to the sorted vector (SV). Continue to iterate through the UV until it is empty, and each item has been added to the SV. Now iterate through the SV and add 1 each time the preceding value is less than the current value.

  1. Init prev-min value. and current-min, also storing its index each time its updated.
  2. Loop over the UV (unsorted vector).
  3. If a value is above prev-min but below current-min set as new current-min, and update the current min index.
  4. At the end of the vector loop you have your current-min and its index value.
  5. If the prev-min was the same as the current-min then set the prev-min back to zero and loop again (step 2).
  6. Add the current lowest value to the SV and remove the current-min from the UV.
  7. Set the prev-min as the current-min and repeat the loop (step 2).
  8. Once the UV is empty iterate through the SV.
  9. each time the current value is above the previous value add one to count.

This should produce the sorted vector with each consecutive value higher than the previous unless it was the highest, in which case the next lowest value is used.

amisam
  • 91
  • 6