제출 #1011142

#제출 시각아이디문제언어결과실행 시간메모리
1011142rythm_of_knightArranging Shoes (IOI19_shoes)C++14
10 / 100
1066 ms4696 KiB
#include "shoes.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
struct Fenwick{
    vector<int> tr;
    int sz;
    Fenwick(int N){
        tr.resize(N+1);
        sz=N;
    }
    void add(ll i, ll x){
        for (; i<=sz; i+=(-i&i)) tr[i]+=x;
    }
    ll get(ll i){
        ll sum=0;
        for (; i; i-=(i&-i)) sum+=tr[i];
        return sum;
    }
};
long long count_swaps(vector<int> s) {
	int n = s.size(), x;
	ll ans = 0, res = 0;
    vector <int> v = s;
    // cin >> n;
//    sgt st;
//    st.init(n);
    Fenwick tr(n+2);
	int pa = 0, ma = 0;
    for (int i = 0; i < n; i++) {
//        cin >> x;
        x = s[i];
        // cout << i << ' ' << x << ' ' << pa << ' ' << ma << "c\n";
        // x = v[i];
        if (x > 0) {
            if (ma) {
                ma--;
                int it = i, j = i;
                for (int k = j; k >= 0; k--) {
                    if (abs(s[k]) != x)
                        continue;
                    if (s[k] == x)
                        k--;
                    else
                        it = k;
                }
                // int l = it + tr.get(it+1), r = i;
                // int j = i;
                it++;
                // cout << it << ' ' << i << "p ";
                while (j > it)
                    swap(s[j], s[j - 1]), j--, res++;
                // ans += r - l - 1;
                // tr.add(l+1, 1);
                // tr.add(r+2, -1);
            } else {
                pa++;
            }
        } else {
            x = -x;
            if (pa) {
                pa--;
                int j = i, it = j;
                for (int k = j; k >= 1; k--) {
                    if (abs(s[k]) != x)
                        continue;
                    if (s[k] == x && s[k - 1] != -x)
                        it = k;
                }
                if (s[0] == x)
                    it = 0;
                while (j > it)
                    swap(s[j], s[j - 1]), j--, res++;
                // int l = it + tr.get(it+1), r = i;
                // ans += r - l;
                // tr.add(l+1, 1);
                // tr.add(r+2, -1);
            } else {
                ma++;
            }
        }
    }
	return res;
}

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

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:24:5: warning: unused variable 'ans' [-Wunused-variable]
   24 |  ll ans = 0, res = 0;
      |     ^~~
#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...