Submission #1058681

# Submission time Handle Problem Language Result Execution time Memory
1058681 2024-08-14T12:11:35 Z shenfe1 Ancient Books (IOI17_books) C++17
0 / 100
4 ms 26588 KB
#include <bits/stdc++.h>
#include "books.h"

#pragma GCC optimize("Ofast")

using namespace std;

#define pb push_back
#define pii pair<int,int>
#define sz(s) (int)s.size()
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define ll long long

const int MAX=1e6+10;

int n;
vector<int> g[MAX];
bool use[MAX];
vector<int> vec;
int pref[MAX];

ll minimum_walk(vector<int> p,int s){
    n=sz(p);
    ll ans=0;
    for(int i=0;i<n;i++){
        ans+=abs(i-p[i]);
    }
    vector<vector<int>> x;
    vector<pii> seg;
    for(int i=0;i<n;i++){
        if(!use[i]){
            vec.clear();
            int v=i;
            while(!use[v]){
                vec.pb(v);
                use[v]=1;
                v=p[v];
            }
            sort(all(vec));
            seg.pb({vec[0],vec.back()});
            x.pb(vec);
        }
    }
    int L,R;
    for(vector<int> v:x){
        for(int f:v){
            if(f==s){
                L=v[0],R=v.back();
            }
        }
    }
    while(R-L+1!=n){
        // cout<<L<<" "<<R<<"\n";
        bool ok=0;
        for(int i=0;i<sz(seg);i++){
            auto [l,r]=seg[i];
            for(int pos:x[i]){
                if(L<=pos&&pos<=R){
                    if(l<L||r>R)ok=1;
                    L=min(L,l);
                    R=max(R,r);
                }
            }
        }
        if(!ok){
            if(L-1>=0){
                L--;
                ans+=2;
            }
            if(R+1<n){
                R++;
                ans+=2;
            }
        }
    }

    return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:54:12: warning: 'R' may be used uninitialized in this function [-Wmaybe-uninitialized]
   54 |     while(R-L+1!=n){
      |           ~^~
books.cpp:54:12: warning: 'L' may be used uninitialized in this function [-Wmaybe-uninitialized]
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26460 KB Output is correct
2 Correct 4 ms 26460 KB Output is correct
3 Correct 4 ms 26460 KB Output is correct
4 Correct 4 ms 26588 KB Output is correct
5 Incorrect 4 ms 26460 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26460 KB Output is correct
2 Correct 4 ms 26460 KB Output is correct
3 Correct 4 ms 26460 KB Output is correct
4 Correct 4 ms 26588 KB Output is correct
5 Incorrect 4 ms 26460 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26460 KB Output is correct
2 Correct 4 ms 26460 KB Output is correct
3 Correct 4 ms 26460 KB Output is correct
4 Correct 4 ms 26588 KB Output is correct
5 Incorrect 4 ms 26460 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 26460 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3864'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26460 KB Output is correct
2 Correct 4 ms 26460 KB Output is correct
3 Correct 4 ms 26460 KB Output is correct
4 Correct 4 ms 26588 KB Output is correct
5 Incorrect 4 ms 26460 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -