이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <iostream>
#include <vector>
using namespace std;
#define nmax 200001
int v[nmax];
vector <int> fst[nmax], fdr[nmax];
int n;
int aint[4 * nmax];
void afiseaza( int node, int l, int r ) {
///cout << node << " " << l << " " << r << " " << aint[node] << "\n";
if( l == r )
return;
afiseaza( 2 * node, l, (l + r) / 2 );
afiseaza( 2 * node + 1, (l + r) / 2 + 1, r );
}
int query( int node, int l, int r, int ql, int qr ) {
if( r < ql || qr < l )
return 0;
if( ql <= l && r <= qr )
return aint[node];
return query( 2 * node, l, (l + r) / 2, ql, qr ) + query( 2 * node + 1, (l + r) / 2 + 1, r, ql, qr );
}
void update( int node, int l, int r, int qpoz, int val ) {
if( r < qpoz || qpoz < l )
return;
if( l == r )
aint[node] += val;
else {
update( 2 * node, l, (l + r) / 2, qpoz, val );
update( 2 * node + 1, (l + r) / 2 + 1, r, qpoz, val );
aint[node] = aint[2 * node] + aint[2 * node + 1];
}
}
//vreau distanta intre doua pozitii
//un interval se muta mai la dreapta
/*int find_pair( int l ) {
int i, j, rez = 0;
if( v[l] < 0 ) {
for( i = l + 1; i < n; i++ ) {
if( v[i] + v[l] == 0 )
break;
}
//toate pozitiile de la l la i cresc cu 1
//rez = query( 1, 1, n, 1, n ) - query( 1, 1, n, 1, i );
//update( 1, 1, n, i, 1 );
//pozitia l si pozitia i
v[i] = 0;
return rez;
} else if( v[j] > 0 ){
for( i = l + 1; i < n; i++ ) {
if( v[i] + v[l] == 0 )
break;
}
for( j = i; j > l + 1; j-- )
v[j] = v[j - 1];
v[i] = 0;
return i - l;
}
return 0;
}
*/
long long count_swaps( vector<int> s) {
int i, l, rez = 0, j;
n = s.size();
for( i = 0; i < n; i++ )
v[i] = s[i];
for( i = n - 1; i >= 0; i-- ) {
if( v[i] < 0 )
fst[-v[i]].push_back( i );
else
fdr[v[i]].push_back( i );
}
for( l = 0; l < n; l ++ ) {
if( v[l] == 0 )
continue;
if( v[l] < 0 ) {
fst[-v[l]].pop_back();
i = fdr[-v[l]].back();
fdr[-v[l]].pop_back();
} else {
fdr[v[l]].pop_back();
i = fst[v[l]].back();
fst[v[l]].pop_back();
}
// toate poz de la l la i cresc cu 1
///cout << "i " << i << " l " << l << "\n";
rez += i - l - 1 - ( query( 1, 1, n, 1, i ) - query( 1, 1, n, 1, l ) );
if( v[l] > 0 )
rez++;
update( 1, 1, n, i, 1 );
v[i] = 0;
/*cout << "rez " << rez << "\n";
cout << "aint\n";
afiseaza( 1, 1, n );
cout << "vector\n";
for( j = 0; j < n; j++ )
cout << v[j] << " ";
cout << "\n";*/
}
return rez;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:69:24: warning: unused variable 'j' [-Wunused-variable]
69 | int i, l, rez = 0, j;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |