답안 #987173

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987173 2024-05-22T08:08:06 Z AdamGS 고대 책들 (IOI17_books) C++17
50 / 100
508 ms 85668 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e9+7;
const int LIM=1e6+7;
ll F[LIM], mi[LIM], ma[LIM], l, r;
set<ll>S;
ll fnd(ll x) {
  if(F[x]==x) return x;
  return F[x]=fnd(F[x]);
}
void uni(int a, int b) {
  if(fnd(a)==fnd(b)) return;
  ma[fnd(a)]=max(ma[fnd(a)], ma[fnd(b)]);
  mi[fnd(a)]=min(mi[fnd(a)], mi[fnd(b)]);
  F[fnd(b)]=fnd(a);
}
ll solve(ll aktl, ll aktr, ll aktmi, ll aktma) {
  if(aktl>aktmi) {
    --aktl;
    aktmi=min(aktmi, mi[fnd(aktl)]);
    aktma=max(aktma, ma[fnd(aktr)]);
    return solve(aktl, aktr, aktmi, aktma);
  }
  if(aktr<aktma) {
    ++aktr;
    aktmi=min(aktmi, mi[fnd(aktr)]);
    aktma=max(aktma, ma[fnd(aktr)]);
    return solve(aktl, aktr, aktmi, aktma);
  }
  if(aktl==l && aktr==r) return 0;
  ll xl=aktl, xr=aktr, xmi=aktmi, xma=aktma, xans=0;
  while(xl>l) {
    --xl;
    if(xl<xmi) xans+=2;
    xmi=min(xmi, mi[fnd(xl)]);
    xma=max(xma, ma[fnd(xl)]);
    if(xma>xr) break;
  }
  if(xma==xr && aktl!=l || aktr==r) return solve(xl, xr, xmi, xma)+xans;
  ll yl=aktl, yr=aktr, ymi=aktmi, yma=aktma, yans=0;
  while(yr<r) {
    ++yr;
    if(yr>yma) yans+=2;
    ymi=min(ymi, mi[fnd(yr)]);
    yma=max(yma, ma[fnd(yr)]);
    if(ymi<yl) break;
  }
  if(ymi==yl && aktr!=r) return solve(yl, yr, ymi, yma)+yans;
  return solve(yl, yr, ymi, yma)+min(xans, yans);
}
ll minimum_walk(vector<int>p, int s) {
  ll n=p.size();
  rep(i, n) {
    F[i]=ma[i]=mi[i]=i;
    S.insert(i);
  }
  rep(i, n) uni(i, p[i]);
  vector<pair<pair<ll,ll>,pair<ll,ll>>>V;
  l=0; r=n-1;
  while(l<s && p[l]==l) ++l;
  while(r>s && p[r]==r) --r;
  ll ans=0;
  rep(i, n) ans+=abs(p[i]-i);
  return ans+solve(s, s, mi[fnd(s)], ma[fnd(s)]);
}

Compilation message

books.cpp: In function 'll solve(ll, ll, ll, ll)':
books.cpp:46:14: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   46 |   if(xma==xr && aktl!=l || aktr==r) return solve(xl, xr, xmi, xma)+xans;
      |      ~~~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 448 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 452 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 448 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 452 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 544 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 448 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 452 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 544 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 508 ms 85240 KB Output is correct
31 Correct 424 ms 85436 KB Output is correct
32 Correct 272 ms 85240 KB Output is correct
33 Correct 283 ms 85440 KB Output is correct
34 Correct 283 ms 85240 KB Output is correct
35 Correct 287 ms 85244 KB Output is correct
36 Correct 290 ms 85244 KB Output is correct
37 Correct 298 ms 85368 KB Output is correct
38 Correct 315 ms 85668 KB Output is correct
39 Correct 302 ms 85236 KB Output is correct
40 Correct 310 ms 85488 KB Output is correct
41 Correct 369 ms 85356 KB Output is correct
42 Correct 320 ms 85272 KB Output is correct
43 Correct 293 ms 85352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB 3rd lines differ - on the 1st token, expected: '9800', found: '9802'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 448 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 452 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 544 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 508 ms 85240 KB Output is correct
31 Correct 424 ms 85436 KB Output is correct
32 Correct 272 ms 85240 KB Output is correct
33 Correct 283 ms 85440 KB Output is correct
34 Correct 283 ms 85240 KB Output is correct
35 Correct 287 ms 85244 KB Output is correct
36 Correct 290 ms 85244 KB Output is correct
37 Correct 298 ms 85368 KB Output is correct
38 Correct 315 ms 85668 KB Output is correct
39 Correct 302 ms 85236 KB Output is correct
40 Correct 310 ms 85488 KB Output is correct
41 Correct 369 ms 85356 KB Output is correct
42 Correct 320 ms 85272 KB Output is correct
43 Correct 293 ms 85352 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Incorrect 1 ms 348 KB 3rd lines differ - on the 1st token, expected: '9800', found: '9802'
46 Halted 0 ms 0 KB -