답안 #109389

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109389 2019-05-06T10:32:39 Z JustasZ 고대 책들 (IOI17_books) C++14
12 / 100
61 ms 19316 KB
#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, s, val[maxn], nxt, L[maxn], R[maxn];
vector<vector<int> > edge;
vector<int>p;
vector<pair<int, pii> > edges;
ll ans;
int par[maxn];
int root(int v)
{
    return v == par[v] ? v : par[v] = root(par[v]);
}
bool unite(int a, int b)
{
    a = root(a), b = root(b);
    if(a == b)
        return false;
    par[b] = a;
    return true;
}
void clr()
{
    for(int i = 0; i < maxn; i++)
    {
        L[i] = mod;
        R[i] = -mod;
    }
    ans = 0;
    nxt = 0;
    edges.clear();
    memset(val, 0, sizeof val);
}
ll minimum_walk(vector<int>perm, int S)
{
    clr();
    s = S;
    p = perm;
    n = sz(p);
    set<int>one;
    for(int i = 0; i < n; i++)
    {
        if(!val[i])
        {
            int col = ++nxt, cur = i, cnt = 0, chk = 0;
            while(!val[cur])
            {
                //cout << "-- " << cur << " " << col << endl;
                L[col] = min(L[col], cur);
                R[col] = max(R[col], cur);
                if(cur == s)
                    chk = 1;
                cnt++;
                val[cur] = col;
                ans += abs(p[cur] - cur);
                cur = p[cur];
            }
            if(cnt == 1 && !chk)
                one.insert(col);
        }
    }
    vector<int>belong[nxt + 1];
    for(int i = 0; i < n; i++)
        belong[val[i]].pb(i);
    edge = vector<vector<int> > (n, vector<int>(n, mod));
    for(int i = 0; i < n; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
            if(val[i] != val[j])
                edge[val[i]][val[j]] = min(edge[val[i]][val[j]], j - i);
        }
    }
    for(int i = 1; i <= nxt; i++)
    {
        //cout << i << " " << L[i] << " " << R[i] << endl;
        for(int j = 1; j <= nxt; j++)
        {
            if(i == j)
                continue;
            for(int a : belong[i])
            {
                if((a < L[j] && p[a] > R[j]) || (a > R[j] && p[a] < L[j]))
                    edge[i][j] = edge[j][i] = 0;
            }
        }
    }
    for(int i = 1; i <= nxt; i++)
        par[i] = i;
    for(int i = 1; i <= nxt; i++)
        for(int j = i + 1; j <= nxt; j++)
            edges.pb({edge[i][j], {i, j}});
    sort(all(edges));
    for(auto aa : edges)
    {
        if(one.count(aa.y.x) || one.count(aa.y.y))
            continue;
        if(unite(aa.y.x, aa.y.y))
            ans += aa.x * 2;
    }
    return ans;
}
/*int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    vector<int>perm;
    for(int i = 0; i < 4; i++)
        perm.pb(i);
    do
    {
        for(int i = 0; i < 4; i++)
        {
            clr();
            cout << "permutaiton ";
            for(int a : perm)
                cout << a << " ";
            cout << endl;
            cout << "start: " << i << " " << minimum_walk(perm, i) << endl;
        }
    } while(next_permutation(all(perm)));
    return 0;
}*/
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12032 KB Output is correct
2 Correct 12 ms 12032 KB Output is correct
3 Correct 13 ms 12032 KB Output is correct
4 Correct 11 ms 12032 KB Output is correct
5 Correct 11 ms 12032 KB Output is correct
6 Correct 12 ms 12032 KB Output is correct
7 Correct 12 ms 12032 KB Output is correct
8 Correct 12 ms 12020 KB Output is correct
9 Correct 12 ms 12032 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 14 ms 12160 KB Output is correct
13 Correct 13 ms 12160 KB Output is correct
14 Correct 12 ms 12032 KB Output is correct
15 Correct 13 ms 12160 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12220 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12032 KB Output is correct
2 Correct 12 ms 12032 KB Output is correct
3 Correct 13 ms 12032 KB Output is correct
4 Correct 11 ms 12032 KB Output is correct
5 Correct 11 ms 12032 KB Output is correct
6 Correct 12 ms 12032 KB Output is correct
7 Correct 12 ms 12032 KB Output is correct
8 Correct 12 ms 12020 KB Output is correct
9 Correct 12 ms 12032 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 14 ms 12160 KB Output is correct
13 Correct 13 ms 12160 KB Output is correct
14 Correct 12 ms 12032 KB Output is correct
15 Correct 13 ms 12160 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12220 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Incorrect 16 ms 16000 KB 3rd lines differ - on the 1st token, expected: '338572', found: '338574'
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12032 KB Output is correct
2 Correct 12 ms 12032 KB Output is correct
3 Correct 13 ms 12032 KB Output is correct
4 Correct 11 ms 12032 KB Output is correct
5 Correct 11 ms 12032 KB Output is correct
6 Correct 12 ms 12032 KB Output is correct
7 Correct 12 ms 12032 KB Output is correct
8 Correct 12 ms 12020 KB Output is correct
9 Correct 12 ms 12032 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 14 ms 12160 KB Output is correct
13 Correct 13 ms 12160 KB Output is correct
14 Correct 12 ms 12032 KB Output is correct
15 Correct 13 ms 12160 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12220 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Incorrect 16 ms 16000 KB 3rd lines differ - on the 1st token, expected: '338572', found: '338574'
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 61 ms 19316 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12032 KB Output is correct
2 Correct 12 ms 12032 KB Output is correct
3 Correct 13 ms 12032 KB Output is correct
4 Correct 11 ms 12032 KB Output is correct
5 Correct 11 ms 12032 KB Output is correct
6 Correct 12 ms 12032 KB Output is correct
7 Correct 12 ms 12032 KB Output is correct
8 Correct 12 ms 12020 KB Output is correct
9 Correct 12 ms 12032 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 14 ms 12160 KB Output is correct
13 Correct 13 ms 12160 KB Output is correct
14 Correct 12 ms 12032 KB Output is correct
15 Correct 13 ms 12160 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12220 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Incorrect 16 ms 16000 KB 3rd lines differ - on the 1st token, expected: '338572', found: '338574'
20 Halted 0 ms 0 KB -