CSES - Aalto Competitive Programming 2024 - wk8 - Mon - Results
Submission details
Task:Optimal sort
Sender:fabiank
Submission time:2024-10-28 17:13:01 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.00 sdetails
#22ACCEPTED0.00 sdetails
#23ACCEPTED0.00 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#27ACCEPTED0.00 sdetails
#28ACCEPTED0.00 sdetails
#29ACCEPTED0.00 sdetails
#30ACCEPTED0.00 sdetails
#31ACCEPTED0.00 sdetails
#32ACCEPTED0.00 sdetails
#33ACCEPTED0.00 sdetails
#34ACCEPTED0.00 sdetails
#35ACCEPTED0.00 sdetails
#36ACCEPTED0.00 sdetails
#37ACCEPTED0.00 sdetails
#38ACCEPTED0.00 sdetails
#39ACCEPTED0.00 sdetails
#40ACCEPTED0.00 sdetails
#41ACCEPTED0.00 sdetails
#42ACCEPTED0.00 sdetails
#43ACCEPTED0.00 sdetails
#44ACCEPTED0.00 sdetails
#45ACCEPTED0.00 sdetails
#46ACCEPTED0.00 sdetails
#47ACCEPTED0.00 sdetails
#48ACCEPTED0.00 sdetails
#49ACCEPTED0.00 sdetails
#50ACCEPTED0.00 sdetails
#51ACCEPTED0.00 sdetails
#52ACCEPTED0.00 sdetails
#53ACCEPTED0.00 sdetails
#54ACCEPTED0.00 sdetails
#55ACCEPTED0.00 sdetails
#56ACCEPTED0.00 sdetails
#57ACCEPTED0.00 sdetails
#58ACCEPTED0.00 sdetails
#59ACCEPTED0.00 sdetails
#60ACCEPTED0.00 sdetails
#61ACCEPTED0.10 sdetails
#62ACCEPTED0.10 sdetails
#63ACCEPTED0.10 sdetails
#64ACCEPTED0.10 sdetails
#65ACCEPTED0.10 sdetails
#66ACCEPTED0.10 sdetails
#67ACCEPTED0.10 sdetails
#68ACCEPTED0.10 sdetails
#69ACCEPTED0.10 sdetails
#70ACCEPTED0.10 sdetails

Compiler report

input/code.cpp: In function 'long long int ford_fulkerson(std::vector<std::vector<long long int> >&, int, int)':
input/code.cpp:137:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         for (int i = 1; i < path_reversed.size(); i++)
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = a; i < b; i++)

// Type Aliases for 1D and 2D vectors with initialization
#define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val
#define ll long long
#define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val))              // 2D vector of ints (n x m), initialized to val
#define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val

using namespace std;

void print_vector(vector<int> &x)
{
    for (int v : x)
    {
        cout << v << " ";
    }
    cout << "\n";
}

void print_matrix(vector<vector<int>> &matrix)
{
    cout << "\n"
         << "----------------" << "\n";
    for (vector<int> row : matrix)
    {
        print_vector(row);
    }
    cout << "\n"
         << "----------------" << "\n";
}

int calc_max_digit(int n)
{
    int max_digit = 0;
    while (n > 0 && max_digit < 9)
    {
        int digit = n % 10;
        if (digit > max_digit)
        {
            max_digit = digit;
        }
        n /= 10;
    }
    return max_digit;
}

// edges as edge list for outgoing node as pairs (end, cost)
vector<ll> dijkstras(int start_point, vector<vector<pair<int, int>>> edges)
{
    int n = edges.size();
    vector<bool> processed(n, false);
    vector<ll> distances(n, LLONG_MAX);
    distances[start_point] = 0;
    priority_queue<pair<ll, int>> pq;
    pq.push({0, start_point});
    while (!pq.empty())
    {
        int curr = pq.top().second;
        pq.pop();
        if (processed[curr])
        {
            continue;
        }
        processed[curr] = true;
        ll distance = distances[curr];

        for (pair<int, int> edge : edges[curr])
        {

            if (distance + edge.second < distances[edge.first])
            {
                distances[edge.first] = distance + edge.second;
                pq.push({-distances[edge.first], edge.first});
            }
        }
    }
    return distances;
}

int bfs_edmondson_karp(const vector<vector<ll>> &connections,
                       const int source, const int target, vector<int> &path_reversed)
{
    int n = connections.size();

    queue<pair<int, ll>> queue;
    queue.push({source, LLONG_MAX});
    vector<int> predecessor(n, -2);
    predecessor[source] = -1;

    while (!queue.empty())
    {
        int current = queue.front().first;
        ll current_bottleneck = queue.front().second;
        queue.pop();

        if (current == target)
        {
            while (current != -1)
            {
                path_reversed.push_back(current);
                current = predecessor[current];
            }
            return current_bottleneck;
        }

        for (int edge_end = 0; edge_end < n; edge_end++)
        {
            ll edge_cap = connections[current][edge_end];
            if (edge_cap > 0 && predecessor[edge_end] == -2)
            {
                predecessor[edge_end] = current;
                queue.push({edge_end, min(current_bottleneck, edge_cap)});
            }
        }
    }

    return -1;
}

ll ford_fulkerson(vector<vector<ll>> &residual_graph, const int source, const int target)
{
    ll flow = 0;

    while (true)
    {
        vector<int> path_reversed;
        int path_capacity = bfs_edmondson_karp(residual_graph, source, target, path_reversed);

        if (path_capacity < 0)
        {
            break;
        }

        flow += path_capacity;
        for (int i = 1; i < path_reversed.size(); i++)
        {
            int edge_end = path_reversed[i - 1];
            int edge_start = path_reversed[i];
            // reduce forwards edge
            residual_graph[edge_start][edge_end] -= path_capacity;
            assert(residual_graph[edge_start][edge_end] >= 0);
            // add to backwards edge
            residual_graph[edge_end][edge_start] += path_capacity;
            assert(residual_graph[edge_end][edge_start] >= 0);
        }
    }
    return flow;
}

bool dfs(int n, const vector<vector<int>> snakes, vector<bool> &visited, vector<int> path, int start, int target)
{
    if (start == target)
    {
        path.push_back(target);
        return true;
    }
    for (int i = n; n >= 1; n--)
    {
        if (!visited[i] && !snakes[start][i])
        {
            if (dfs(n, snakes, visited, path, i, target))
            {
                path.push_back(start);
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int n;
    cin >> n;

    vector<int> a(n);
    vector<int> b(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        b[i] = a[i];
    }

    sort(b.begin(), b.end());

    int result = 0;

    // position in sorted array
    unordered_map<int, int> map;
    for (int i = 0; i < n; i++)
    {
        map.insert({b[i], i});
    }

    vector<bool> visited(n);
    for (int i = 0; i < n; i++)
    {
        if (!visited[i])
        {

            int j = map.find(a[i])->second;
            while (i != j)
            {
                result++;
                visited[j] = true;
                j = map.find(a[j])->second;
            }
        }
    }
    cout << result << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
1
14 

correct output
0

user output
0

Test 2

Verdict: ACCEPTED

input
2
18 1 

correct output
1

user output
1

Test 3

Verdict: ACCEPTED

input
2
3 8 

correct output
0

user output
0

Test 4

Verdict: ACCEPTED

input
3
6 13 8 

correct output
1

user output
1

Test 5

Verdict: ACCEPTED

input
3
8 6 14 

correct output
1

user output
1

Test 6

Verdict: ACCEPTED

input
3
19 11 5 

correct output
1

user output
1

Test 7

Verdict: ACCEPTED

input
4
17 15 16 13 

correct output
1

user output
1

Test 8

Verdict: ACCEPTED

input
4
16 8 12 19 

correct output
2

user output
2

Test 9

Verdict: ACCEPTED

input
4
13 18 5 15 

correct output
3

user output
3

Test 10

Verdict: ACCEPTED

input
4
20 18 11 17 

correct output
3

user output
3

Test 11

Verdict: ACCEPTED

input
5
17 12 15 13 11 

correct output
2

user output
2

Test 12

Verdict: ACCEPTED

input
5
15 20 9 1 19 

correct output
4

user output
4

Test 13

Verdict: ACCEPTED

input
5
1 11 9 19 4 

correct output
2

user output
2

Test 14

Verdict: ACCEPTED

input
5
17 2 15 6 12 

correct output
4

user output
4

Test 15

Verdict: ACCEPTED

input
5
4 18 20 11 19 

correct output
3

user output
3

Test 16

Verdict: ACCEPTED

input
5
2 5 8 17 18 

correct output
0

user output
0

Test 17

Verdict: ACCEPTED

input
5
19 5 7 17 18 

correct output
4

user output
4

Test 18

Verdict: ACCEPTED

input
5
2 5 9 7 16 

correct output
1

user output
1

Test 19

Verdict: ACCEPTED

input
5
8 20 1 18 5 

correct output
2

user output
2

Test 20

Verdict: ACCEPTED

input
5
10 8 11 3 1 

correct output
4

user output
4

Test 21

Verdict: ACCEPTED

input
10
8 18 15 13 6 12 9 17 2 11 

correct output
6

user output
6

Test 22

Verdict: ACCEPTED

input
10
5 7 1 19 9 20 8 3 2 15 

correct output
9

user output
9

Test 23

Verdict: ACCEPTED

input
10
5 11 10 7 4 14 19 1 9 13 

correct output
8

user output
8

Test 24

Verdict: ACCEPTED

input
10
17 12 2 3 18 15 9 6 11 1 

correct output
9

user output
9

Test 25

Verdict: ACCEPTED

input
10
18 13 20 12 4 5 19 15 14 11 

correct output
7

user output
7

Test 26

Verdict: ACCEPTED

input
10
18 8 17 2 10 13 16 5 19 20 

correct output
7

user output
7

Test 27

Verdict: ACCEPTED

input
10
3 5 18 19 17 2 1 20 8 7 

correct output
8

user output
8

Test 28

Verdict: ACCEPTED

input
10
9 7 16 5 20 2 10 15 11 6 

correct output
7

user output
7

Test 29

Verdict: ACCEPTED

input
10
17 8 11 20 18 5 16 9 1 13 

correct output
8

user output
8

Test 30

Verdict: ACCEPTED

input
10
5 10 11 2 1 7 3 9 18 8 

correct output
9

user output
9

Test 31

Verdict: ACCEPTED

input
100
567896598 831327091 483130123 ...

correct output
95

user output
95

Test 32

Verdict: ACCEPTED

input
100
253498609 556362063 442153869 ...

correct output
95

user output
95

Test 33

Verdict: ACCEPTED

input
100
833824080 874174469 536879893 ...

correct output
96

user output
96

Test 34

Verdict: ACCEPTED

input
100
55262494 701594610 766185388 6...

correct output
98

user output
98

Test 35

Verdict: ACCEPTED

input
100
385222461 258813232 585406893 ...

correct output
95

user output
95

Test 36

Verdict: ACCEPTED

input
100
528427003 520690782 556647080 ...

correct output
92

user output
92

Test 37

Verdict: ACCEPTED

input
100
845689539 115595477 723524474 ...

correct output
95

user output
95

Test 38

Verdict: ACCEPTED

input
100
566683130 399845016 457901037 ...

correct output
98

user output
98

Test 39

Verdict: ACCEPTED

input
100
401801721 431563805 933290530 ...

correct output
95

user output
95

Test 40

Verdict: ACCEPTED

input
100
941909102 224913296 697438766 ...

correct output
97

user output
97

Test 41

Verdict: ACCEPTED

input
200
444811497 903431470 720091934 ...

correct output
191

user output
191

Test 42

Verdict: ACCEPTED

input
200
336536656 534840541 835789078 ...

correct output
194

user output
194

Test 43

Verdict: ACCEPTED

input
200
395789335 866736363 824087376 ...

correct output
195

user output
195

Test 44

Verdict: ACCEPTED

input
200
238325464 289089504 743727937 ...

correct output
192

user output
192

Test 45

Verdict: ACCEPTED

input
200
718880754 431102449 403271411 ...

correct output
197

user output
197

Test 46

Verdict: ACCEPTED

input
200
97225921 989421484 860498340 2...

correct output
194

user output
194

Test 47

Verdict: ACCEPTED

input
200
296729043 504301807 26318971 8...

correct output
193

user output
193

Test 48

Verdict: ACCEPTED

input
200
903692950 776814822 973264570 ...

correct output
196

user output
196

Test 49

Verdict: ACCEPTED

input
200
290066783 874736248 463312759 ...

correct output
192

user output
192

Test 50

Verdict: ACCEPTED

input
200
973107823 90258367 557823872 1...

correct output
194

user output
194

Test 51

Verdict: ACCEPTED

input
1000
592912152 847045692 284064425 ...

correct output
990

user output
990

Test 52

Verdict: ACCEPTED

input
1000
407810678 616563051 942898335 ...

correct output
990

user output
990

Test 53

Verdict: ACCEPTED

input
1000
618641025 671935409 944035992 ...

correct output
993

user output
993

Test 54

Verdict: ACCEPTED

input
1000
366766968 558964810 539824828 ...

correct output
994

user output
994

Test 55

Verdict: ACCEPTED

input
1000
307827860 66173733 860833950 9...

correct output
994

user output
994

Test 56

Verdict: ACCEPTED

input
1000
249869917 995683003 692483481 ...

correct output
991

user output
991

Test 57

Verdict: ACCEPTED

input
1000
955687818 719718081 43478262 6...

correct output
990

user output
990

Test 58

Verdict: ACCEPTED

input
1000
734196441 465297605 360643915 ...

correct output
992

user output
992

Test 59

Verdict: ACCEPTED

input
1000
851978986 549468516 638503725 ...

correct output
988

user output
988

Test 60

Verdict: ACCEPTED

input
1000
310014536 901327171 160589368 ...

correct output
992

user output
992

Test 61

Verdict: ACCEPTED

input
100000
614666693 269733332 88642683 5...

correct output
99989

user output
99989

Test 62

Verdict: ACCEPTED

input
100000
272940982 811925955 61591592 5...

correct output
99992

user output
99992

Test 63

Verdict: ACCEPTED

input
100000
620599476 742651279 318143272 ...

correct output
99989

user output
99989

Test 64

Verdict: ACCEPTED

input
100000
848936553 838116035 936109023 ...

correct output
99983

user output
99983

Test 65

Verdict: ACCEPTED

input
100000
480849086 599714294 875965742 ...

correct output
99984

user output
99984

Test 66

Verdict: ACCEPTED

input
100000
888068198 739874627 942196434 ...

correct output
99987

user output
99987

Test 67

Verdict: ACCEPTED

input
100000
169827791 713679537 698075748 ...

correct output
99986

user output
99986

Test 68

Verdict: ACCEPTED

input
100000
636712086 376507123 906440322 ...

correct output
99987

user output
99987

Test 69

Verdict: ACCEPTED

input
100000
300349845 13933235 833947124 8...

correct output
99985

user output
99985

Test 70

Verdict: ACCEPTED

input
100000
200888519 710864746 665880694 ...

correct output
99985

user output
99985