This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shoes.h"
#include <iostream>
using namespace std;
#define nmax 100000
int v[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( l = 0; l < n; l ++ ) {
if( v[l] == 0 )
continue;
for( i = l + 1; i < n; i++ )
if( v[l] + v[i] == 0 )
break;
// 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;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:67:24: warning: unused variable 'j' [-Wunused-variable]
67 | 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... |