#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef pair<int, int> pii;
typedef long long lld;
typedef long double ld;
template<typename Iter>
ostream& outIt(ostream& out, Iter b, Iter e){
for(Iter i = b; i != e; ++i)
out << (i == b?"":" ") << *i;
return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> p){
return out << '(' << p.ff << ", " << p.ss << ')';
}
template<typename T>
ostream& operator<<(ostream& out, vector<T> v){
return outIt(out << '[', all(v)) << ']';
}
vector<pii> cs;
vector<int> poss, dist, gone;
long long minimum_walk(vector<int> p, int s) {
lld ans = 0;
int pmin = INT_MAX, pmax = INT_MIN;
for(int i = 0; i < p.size(); i++)if(p[i] != i)ans += abs(p[i]-i), cs.pb({min(i, p[i]), max(i, p[i])}), pmin = min(pmin, i), pmax = max(pmax, i);
if(ans == 0)return 0;
sort(all(cs));
int r = cs.front().ff;
gone.resize(p.size()), poss.resize(p.size()), dist.resize(p.size());
poss[r] = 1;
bool fl = true;
for(int i = 0; i < cs.size(); i++){
if(cs[i].ff > s && s > r)fl = false;
if(cs[i].ff > r) poss[r] = poss[cs[i].ff] = 1;
ans += 2*max(0, cs[i].ff - r);
r = max(r, cs[i].ss);
}
poss[r] = 1;
//cout << poss << endl;
if(!fl)return ans;
for(int i = 0; i < p.size(); i++)dist[i] = abs(i-s);
//cout << dist << gone << endl;
for(int i = 0; i < p.size(); i++){
pii mins = {INT_MAX, -1};
for(int j = 0; j < p.size(); j++)if(!gone[j])mins = min(mins, {dist[j], j});
gone[mins.ss] = true;
//cout << mins << endl;
dist[p[mins.ss]] = min(dist[p[mins.ss]], mins.ff);
//if(poss[mins.ss])return ans + 2*mins.ff;
for(int j = 0; j < p.size(); j++){
dist[j] = min(dist[j], mins.ff + (poss[mins.ss] && poss[j] ? 0:abs(mins.ss - j)));
}
}
for(int i = 0; i < p.size(); i++)if(p[i] == i)dist[i] *= 0;
//cout << dist << endl;
return ans + 2 * (*max_element(all(dist)));
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:34:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0; i < p.size(); i++)if(p[i] != i)ans += abs(p[i]-i), cs.pb({min(i, p[i]), max(i, p[i])}), pmin = min(pmin, i), pmax = max(pmax, i);
| ~~^~~~~~~~~~
books.cpp:41:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i = 0; i < cs.size(); i++){
| ~~^~~~~~~~~~~
books.cpp:50:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 0; i < p.size(); i++)dist[i] = abs(i-s);
| ~~^~~~~~~~~~
books.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 0; i < p.size(); i++){
| ~~^~~~~~~~~~
books.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int j = 0; j < p.size(); j++)if(!gone[j])mins = min(mins, {dist[j], j});
| ~~^~~~~~~~~~
books.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int j = 0; j < p.size(); j++){
| ~~^~~~~~~~~~
books.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < p.size(); i++)if(p[i] == i)dist[i] *= 0;
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Incorrect |
0 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Incorrect |
0 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Incorrect |
0 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3420' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Incorrect |
0 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |