CSES - Aalto Competitive Programming 2024 - wk8 - Homework C++ - Results
Submission details
Task:Counting ones
Sender:fabiank
Submission time:2024-10-27 21:12:18 +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.24 sdetails
#8ACCEPTED0.24 sdetails
#9ACCEPTED0.24 sdetails
#10ACCEPTED0.24 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;
}

ll 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;
        ll 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;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

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

    int max_result = -1;
    pair<int, int> result_pair;
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            int result = __popcount((long long)a[i] * a[j]);
            if (result > max_result)
            {
                max_result = result;
                result_pair = {a[i], a[j]};
            }
        }
    }

    cout << max_result << "\n";
    cout << result_pair.first << " " << result_pair.second << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
5
42 100 73 94 1 

correct output
9
42 73

user output
9
42 73

Test 2

Verdict: ACCEPTED

input
5
44 19 3 94 55 

correct output
8
19 94

user output
8
19 94

Test 3

Verdict: ACCEPTED

input
10
551 71 709 840 291 122 511 570...

correct output
15
893 438

user output
15
893 438

Test 4

Verdict: ACCEPTED

input
10
968 901 548 173 973 856 715 61...

correct output
14
715 698

user output
14
715 698

Test 5

Verdict: ACCEPTED

input
100
221994 55181 870733 831328 206...

correct output
29
396737 649459

user output
29
396737 649459

Test 6

Verdict: ACCEPTED

input
100
892861 947477 331980 209407 82...

correct output
29
825210 578859

user output
29
825210 578859

Test 7

Verdict: ACCEPTED

input
10000
76308292 227339075 779918796 3...

correct output
50
978548139 589096701

user output
50
978548139 589096701

Test 8

Verdict: ACCEPTED

input
10000
873429404 968540665 239439572 ...

correct output
49
661547021 435157931

user output
49
661547021 435157931

Test 9

Verdict: ACCEPTED

input
10000
10374159 364461027 501874596 4...

correct output
50
313852607 889662850

user output
50
313852607 889662850

Test 10

Verdict: ACCEPTED

input
10000
771320644 298761159 20751947 4...

correct output
49
516789275 257079921

user output
49
516789275 257079921