#include "books.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define ll long long
#define pi pair < ll,ll >
#define rep(i,a,b) for(int i = a;i < b;i++)
#define mp(a,b) make_pair(a,b)
#define N 1000003
#define INF 1e9+7
using namespace std;
vector < pi > graph[N];
int id[N];
bool vis[N];
ll abso(ll x)
{
return (x < 0) ? -x : x;
}
ll dijkstra_weight(int n,int source)
{
ll d[n];
ll w[n];
rep(i,0,n)
d[i] = INF;
d[source] = 0;
w[source]=0;
priority_queue < pi > pq;
pq.push(mp(0,source));
while(!pq.empty())
{
pi cur = pq.top();
pq.pop();
cur.first*=-1;
if(cur.first > d[cur.second])
continue;
rep(i,0,graph[cur.second].size())
{
int v = graph[cur.second][i].first;
ll wv = graph[cur.second][i].second;
// cout <<"in " << v << endl;
if(d[v] > cur.first + wv)
{
d[v]=cur.first+wv;
w[v]=wv;
pq.push(mp(-d[v],v));
}
}
}
ll res = 0;
rep(i,0,n)
{
res += w[i];
}
return 2*res;
}
ll get_cycle_cost(vector < int > c)
{
if(c.size()==1)
return 0;
ll res = 0;
rep(i,1,c.size())
{
res += abso(c[i]-c[i-1]);
}
res += abso(c[0]-c[c.size()-1]);
return res;
}
ll get_distance(pi a,pi b)
{
return max((ll)0,max(a.first,b.first)-min(a.second,b.second));
}
void calculate_weights(int n,const vector < int > &p)
{
rep(i,0,n)
{
rep(j,i+1,n)
{
if(!vis[i] || !vis[j] || id[i] == id[j])
continue;
int dist = get_distance(mp(i,p[i]),mp(j,p[j]));
graph[id[i]].push_back(mp(id[j],dist));
graph[id[j]].push_back(mp(id[i],dist));
}
}
return;
}
long long minimum_walk(std::vector<int> p, int s) // for 1st sub
{
int n = p.size();
rep(i,0,n)
{
graph[i].clear();
vis[i]=false;
id[i] = -1;
}
vector < vector < int > > cycles;
ll ans = 0;
rep(i,0,n)
{
if(p[i] == i || vis[i])
continue;
int cur = i;
int ind = cycles.size();
cycles.push_back(vector < int > ());
while(!vis[cur])
{
id[cur] = ind;
vis[cur] = true;
cycles[ind].push_back(cur);
cur = p[cur];
}
ans += get_cycle_cost(cycles[ind]);
}
if(!vis[s])
{
vis[s]=true;
int ind = cycles.size();
cycles.push_back(vector < int > ());
cycles[ind].push_back(s);
id[s] = ind;
}
calculate_weights(n,p);
ans += dijkstra_weight(cycles.size(),id[s]);
return ans;
}
Compilation message
books.cpp: In function 'long long int dijkstra_weight(int, int)':
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
47 | rep(i,0,graph[cur.second].size())
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
books.cpp:47:9: note: in expansion of macro 'rep'
47 | rep(i,0,graph[cur.second].size())
| ^~~
books.cpp: In function 'long long int get_cycle_cost(std::vector<int>)':
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
80 | rep(i,1,c.size())
| ~~~~~~~~~~~~
books.cpp:80:5: note: in expansion of macro 'rep'
80 | rep(i,1,c.size())
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23720 KB |
Output is correct |
4 |
Correct |
14 ms |
23732 KB |
Output is correct |
5 |
Correct |
18 ms |
23668 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23708 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
15 ms |
23756 KB |
Output is correct |
13 |
Correct |
14 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23672 KB |
Output is correct |
15 |
Correct |
16 ms |
23668 KB |
Output is correct |
16 |
Correct |
14 ms |
23724 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
20 ms |
23724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23720 KB |
Output is correct |
4 |
Correct |
14 ms |
23732 KB |
Output is correct |
5 |
Correct |
18 ms |
23668 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23708 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
15 ms |
23756 KB |
Output is correct |
13 |
Correct |
14 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23672 KB |
Output is correct |
15 |
Correct |
16 ms |
23668 KB |
Output is correct |
16 |
Correct |
14 ms |
23724 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
20 ms |
23724 KB |
Output is correct |
19 |
Correct |
36 ms |
38644 KB |
Output is correct |
20 |
Correct |
32 ms |
34716 KB |
Output is correct |
21 |
Correct |
15 ms |
23716 KB |
Output is correct |
22 |
Correct |
31 ms |
28388 KB |
Output is correct |
23 |
Correct |
27 ms |
30392 KB |
Output is correct |
24 |
Correct |
30 ms |
34608 KB |
Output is correct |
25 |
Correct |
32 ms |
35848 KB |
Output is correct |
26 |
Correct |
42 ms |
41500 KB |
Output is correct |
27 |
Correct |
40 ms |
42532 KB |
Output is correct |
28 |
Correct |
40 ms |
41820 KB |
Output is correct |
29 |
Correct |
24 ms |
30016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23720 KB |
Output is correct |
4 |
Correct |
14 ms |
23732 KB |
Output is correct |
5 |
Correct |
18 ms |
23668 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23708 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
15 ms |
23756 KB |
Output is correct |
13 |
Correct |
14 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23672 KB |
Output is correct |
15 |
Correct |
16 ms |
23668 KB |
Output is correct |
16 |
Correct |
14 ms |
23724 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
20 ms |
23724 KB |
Output is correct |
19 |
Correct |
36 ms |
38644 KB |
Output is correct |
20 |
Correct |
32 ms |
34716 KB |
Output is correct |
21 |
Correct |
15 ms |
23716 KB |
Output is correct |
22 |
Correct |
31 ms |
28388 KB |
Output is correct |
23 |
Correct |
27 ms |
30392 KB |
Output is correct |
24 |
Correct |
30 ms |
34608 KB |
Output is correct |
25 |
Correct |
32 ms |
35848 KB |
Output is correct |
26 |
Correct |
42 ms |
41500 KB |
Output is correct |
27 |
Correct |
40 ms |
42532 KB |
Output is correct |
28 |
Correct |
40 ms |
41820 KB |
Output is correct |
29 |
Correct |
24 ms |
30016 KB |
Output is correct |
30 |
Execution timed out |
2120 ms |
839636 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
27852 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23720 KB |
Output is correct |
4 |
Correct |
14 ms |
23732 KB |
Output is correct |
5 |
Correct |
18 ms |
23668 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23708 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
15 ms |
23756 KB |
Output is correct |
13 |
Correct |
14 ms |
23756 KB |
Output is correct |
14 |
Correct |
13 ms |
23672 KB |
Output is correct |
15 |
Correct |
16 ms |
23668 KB |
Output is correct |
16 |
Correct |
14 ms |
23724 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
20 ms |
23724 KB |
Output is correct |
19 |
Correct |
36 ms |
38644 KB |
Output is correct |
20 |
Correct |
32 ms |
34716 KB |
Output is correct |
21 |
Correct |
15 ms |
23716 KB |
Output is correct |
22 |
Correct |
31 ms |
28388 KB |
Output is correct |
23 |
Correct |
27 ms |
30392 KB |
Output is correct |
24 |
Correct |
30 ms |
34608 KB |
Output is correct |
25 |
Correct |
32 ms |
35848 KB |
Output is correct |
26 |
Correct |
42 ms |
41500 KB |
Output is correct |
27 |
Correct |
40 ms |
42532 KB |
Output is correct |
28 |
Correct |
40 ms |
41820 KB |
Output is correct |
29 |
Correct |
24 ms |
30016 KB |
Output is correct |
30 |
Execution timed out |
2120 ms |
839636 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |