제출 #1014383

#제출 시각아이디문제언어결과실행 시간메모리
1014383KasymKArranging Shoes (IOI19_shoes)C++17
85 / 100
1037 ms3268 KiB
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ff first
#define ss second
#define all(v) v.begin(), v.end()

ll count_swaps(vector<int> v){
    int n = (int)v.size(), ok = 1;
    n >>= 1;
    for(int i = 0; i < n; ++i)
        ok &= (v[i] < 0);
    for(int i = 0; i < n; ++i)
        ok &= (-v[i] == v[i+n]);
    ll ans = 0;
    if(ok){
        n--;
        ans = n*1ll*(n+1)/2;
        return ans;
    }
    n <<= 1;
    ok = 1;
    for(int i = 1; i < n; ++i)
        ok &= (abs(v[0]) == abs(v[i]));
    if(ok){
        int ne_ = 1, ne__ = 1;
        for(int i = 0; i < n; ++i){
            ne_ = max(i+1, ne_);
            ne__ = max(i+1, ne__);
            if(!(i&1)){
                if (v[i] < 0)
                    continue;
                while(v[ne_] > 0)
                    ne_++;
                swap(v[i], v[ne_]);
                ans += (ne_-i);
            }
            if(i&1){
                if(v[i] > 0)
                    continue;
                while (v[ne__] < 0)
                    ne__++;
                swap(v[i], v[ne__]);
                ans += (ne__-i);
            }
        }
        return ans;
    }
    for(int i = 0; i < n-1; i+=2){
        if(-v[i] == v[i+1]){
            // it means these two shoes are the same
            if(v[i] > 0)
                swap(v[i], v[i+1]), ans++;
        }
        int ad;
        for(int j = i+1; j < n; ++j)
            if(-v[i] == v[j]){
                ad = j;
                break;
            }
        for(int j = ad; j > i+1; --j)
            swap(v[j], v[j-1]), ans++;
        if(v[i] > 0)
            swap(v[i], v[i+1]), ans++; // it means first shoe is right part
    }
    return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:62:21: warning: 'ad' may be used uninitialized in this function [-Wmaybe-uninitialized]
   62 |             swap(v[j], v[j-1]), ans++;
      |                     ^
#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...