Submission #601789

#TimeUsernameProblemLanguageResultExecution timeMemory
601789Minindu2006Arranging Shoes (IOI19_shoes)C++14
85 / 100
1085 ms12436 KiB
#include "shoes.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
ll count_swaps(vector<int> s)
{
    ll n = s.size() / 2;
    if (n == 1)
        return s[0] > 0;
	set<int> st;
    int isb4 = 1;
    for (int i = 0; i < n; i++)
    {
        if (s[i] != -s[i + n] || s[i] > 0)
        {
            isb4 = 0;
            break;
        }
    }
    if (isb4)
        return n * (n - 1) / 2;
	for(int i=0;i<2*n;i++)
		st.insert(s[i]);
	

	if (st.size() == 2)
	{
		
		vector<int> l;
		for (int i = 0; i < 2 * n; i++)
			if (s[i] < 0)
				l.push_back(i);
		ll ans = 0, j = 0;
		for (int i = 0; i < 2 * n; i += 2)
			ans += abs(i - l[j]), j++;
		return ans;
	}
    ll pos = 0, ans = 0;
    while (pos < 2 * n)
    {
        if (s[pos] > 0)
        {
            int i;
            for (i = pos + 1; i < 2 * n; i++)
                if (s[i] == -s[pos])
                    break;
            int num = s[i];
            s.erase(s.begin() + i);
            s.insert(s.begin() + pos, num);
            ans += (i - pos);
        }
        else if (s[pos] + s[pos + 1] != 0)
        {
            int i;
            for (i = pos + 2; i < 2 * n; i++)
                if (s[i] == -s[pos])
                    break;
            int num = s[i];
            s.erase(s.begin() + i);
            s.insert(s.begin() + pos + 1, num);
            ans += (i - pos - 1);
        }
        // cout << s[pos] << " " << s[pos + 1] << '\n';
        pos += 2;
        
    }
    return ans;
}

Compilation message (stderr)

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:9:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
    9 |     if (n == 1)
      |     ^~
shoes.cpp:11:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   11 |  set<int> st;
      |  ^~~
shoes.cpp:21:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   21 |     if (isb4)
      |     ^~
shoes.cpp:23:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   23 |  for(int i=0;i<2*n;i++)
      |  ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...