답안 #268107

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
268107 2020-08-16T08:49:04 Z shinjan Arranging Shoes (IOI19_shoes) C++14
0 / 100
1 ms 384 KB
#include <iostream>
#include <bits/stdc++.h>
#include "shoes.h"
#define maxN 100001
using namespace std;
int fen[maxN];
void add(int ind,int n)
{
    for(int i=ind;i<=n;i+=(i&(-i)))
    {
        fen[i]++;
    }
}
void brisi(int ind,int n)
{
    for(int i=ind;i<=n;i+=(i&(-i)))
    {
        fen[i]--;
    }
}
long long sum(int ind)
{
    long long ans=0;
    for(int i=ind;i>=1;i-=(i&(-i)))
    {
        ans+=fen[i];
    }
    return ans;
}
long long count_swaps(vector <int> s)
{
    int n=s.size();
    long long ans=0;
    queue<int> neg[n/2+1];
    queue<int> poz[n/2+1];
    for(int i=0;i<s.size();i++)
    {
        if(s[i]>0)
        {
            if(neg[s[i]].size()!=0)
            {
                int pos=neg[s[i]].front();
                ans-=(sum(i)-sum(pos+1));
                brisi(pos+1,n);
                ans+=(i-pos);
                neg[s[i]].pop();
            }
            else{
                poz[s[i]].push(i);
                add(i+1,n);
            }
        }
        if(s[i]<0)
        {
            if(poz[s[i]].size()!=0)
            {
                int pos=poz[s[i]].front();
                ans-=(sum(i)-sum(pos+1));
                brisi(pos+1,n);
                ans+=(i-pos+1);
                poz[s[i]].pop();
            }
            else{
                neg[s[i]].push(i);
                add(i+1,n);
            }
        }
    }
    return ans;
}

Compilation message

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i=0;i<s.size();i++)
      |                 ~^~~~~~~~~
shoes.cpp:55:30: warning: array subscript -1 is below array bounds of 'std::queue<int> [(<anonymous> + 1)]' [-Warray-bounds]
   55 |             if(poz[s[i]].size()!=0)
      |                ~~~~~~~~~~~~~~^~
shoes.cpp:64:31: warning: array subscript -1 is below array bounds of 'std::queue<int> [(<anonymous> + 1)]' [-Warray-bounds]
   64 |                 neg[s[i]].push(i);
      |                 ~~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 380 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -