VTAR - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: pols_agyi_pols
Tester: kingmessi
Editorialist: iceknight1093

DIFFICULTY:

Easy

PREREQUISITES:

Greedy algorithms

PROBLEM:

You’re given an array A. Each element of A is either -1 or a positive integer.
You can rearrange the positive elements of A among themselves, however you wish.

After rearrangement, the following will happen for each person i:

  • If A_i \gt 0, person i will vote for person A_i.
  • Otherwise, let f_v denote the number of indices \lt i such that A_i = v.
    • If there’s a unique v for which f_v is maximum, person i will vote for this v.
    • Otherwise, person i won’t vote.

You’re given X.
Is it possible to make X the unique winner of the election?

EXPLANATION:

Let’s call indices with A_i \gt 0 “fixed” indices, while those with A_i = -1 will be called “flexible” indices.

Observe that the first person to vote for X must be at a fixed index - because a flexible index is never going to vote for X when X has 0 votes (and so cannot be the unique maximum).

In particular, if X does not appear among the non-zero elements of A at all, it’s impossible for X to ever receive any votes.
This means X certainly cannot win, so we can immediately say the answer is No.

So, let’s consider the situation where we do have some copies of X to use.
It’s in our best interest to make as many of the -1's vote for X as possible.
Thus, naturally, it’s optimal to just place all copies of X as quickly as we can: if there are c_X copies of X in total we just place them in the leftmost c_X positions that initially contained positive elements.

Now, we need to arrange the other elements in the remaining positions.
Again, it’s ideal if we’re able to make as many people vote for X as possible.
This means we should try to avoid making any other value reach the frequency of X; since as long as X has the highest frequency, we can ensure that the -1 votes will continue to go to X.

The simplest way to ensure this goes on for as long as possible, is to ‘balance’ the frequencies of the other elements.
That is,

  • First, place one copy of each remaining element.
  • Only after that, place the second copy of each remaining element.
  • Only after that, place the third copy of each remaining element.
    \vdots

It’s not hard to see that this is optimal in both maximizing the number of votes X receives, as well as minimizing the number of votes anybody else receives.


Once the optimal arrangement has been built, tally the total votes received by everyone, and then check if X is the unique winner.

Since A_i \le 100, this entire algorithm can be implemented in \mathcal{O}(N\cdot 100) time by storing appropriate frequency arrays and iterating across them when needed.
Alternately, a data structure like a priority queue or set allows for a complexity of \mathcal{O}(N\log N).

TIME COMPLEXITY:

\mathcal{O}(N\log N) or \mathcal{O}(100\cdot N) per testcase.

CODE:

Editorialist's code (C++)
// #include <bits/allocator.h>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include "bits/stdc++.h"
using namespace std;
using ll = long long int;
mt19937_64 RNG(chrono::high_resolution_clock::now().time_since_epoch().count());

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

    int t; cin >> t;
    while (t--) {
        int n, x; cin >> n >> x;
        vector a(n, 0);
        for (int &y : a) cin >> y;

        array<int, 101> have{};
        for (int i = 0; i < n; ++i) {
            if (a[i] != -1) {
                ++have[a[i]];
            }
        }

        if (have[x] == 0) {
            cout << "No\n";
            continue;
        }

        array<int, 101> votes{}, freq{};
        for (int i = 0; i < n; ++i) {
            if (a[i] == -1) {
                int mx = ranges::max(freq);
                int ct = 0, who = 0;
                for (int j = 0; j <= 100; ++j) {
                    if (freq[j] == mx) {
                        ++ct;
                        who = j;
                    }
                }
                if (ct == 1) ++votes[who];
            }
            else {
                if (have[x] > 0) {
                    a[i] = x;
                    --have[x];
                }
                else {
                    int mn = -1;
                    for (int j = 0; j <= 100; ++j) {
                        if (have[j] == 0) continue;
                        if (mn == -1 or freq[j] < freq[mn]) mn = j;
                    }

                    a[i] = mn;
                    --have[mn];
                }
                
                ++votes[a[i]];
                ++freq[a[i]];
            }
        }

        int mx = ranges::max(votes);
        int ct = 0, who = 0;
        for (int j = 0; j <= 100; ++j) {
            if (votes[j] == mx) {
                ++ct;
                who = j;
            }
        }
        if (ct == 1 and who == x) cout << "Yes\n";
        else cout << "No\n";
    }
}

For the test case
1
40 4
9 9 9 3 -1 -1 1 -1 1 2 4 2 9 9 9 -1 10 1 10 -1 10 -1 10 10 9 6 8 1 7 8 -1 9 10 7 -1 2 4 4 3 5

Your answer is NO but the rearrangement
{4, 4, 4, 5, -1, -1, 6, -1, 3, 3, 7, 7, 8, 8, 2, -1, 2, 1, 1, -1, 10, -1, 10, 9, 9, 2, 1, 10, 9, 1, -1, 10, 9, 10, -1, 9, 10, 9, 9, 9}

gives answer as YES or am i missing something

4 Likes

Is the author forget the tie vote rule?
1

10 1

1 -1 -1 2 3 4 2 -1 -1 3

if we rerange the sequence like this:
1 -1 -1 2 3 2 3 -1 -1 4
then the answer must be yes, but your garbage solution puts no

7 Likes

A simple case for counter-example.

20 1
1 2*(-1) 2 2 2 3 3 3 10*(-1) 1

with this arrangement, 1 has 4 votes and both 2 and 3 has 3 votes.

But if you put 2/3 on the last position of 1, the 10 guys with -1 will vote either 2 or 3, which makes 1 not the winner.

4 Likes

I believe that under the correct interpretation of the problem, the approach is as follows:

  1. First, we prioritize assigning k to the available positions in the sequence.
  2. Let the frequency of k be f_k. We then iteratively assign the remaining numbers \min(f_i, f_k - 1) times.
  3. Next, we select the two numbers with the highest frequencies; let’s call them x and y.
  4. We attempt to distribute x and y evenly into the subsequent positions. If there are not enough of them, or if there is a parity mismatch, we try substituting with other numbers.
  5. Finally, the last
    element placed is x.

I am not sure if this solution is correct, as I do not have the proper test data.

My counter-example to the greedy idea:
1
14 1
1 -1 -1 2 3 4 2 -1 -1 -1 -1 -1 3 4

Seems that it’s important when to put the maximum prefix value as the number of -1s is basically a weight, and you could not assign it arbitrarily to an element.

1 Like

The answer should be yes

I think in the question they asked for any rearrangment right? Not always k should win? Isn’t this. Am I wrong?

Correct. But the editorial is saying putting all X at the beginning is the best strategy. While my counter example is showing that it’s not.

1 Like

test case:
1
19 6
1 1 6 -1 4 -1 4 8 -1 5 5 6 5 4 -1 1 1 8 8
for this test case the answer is Yes, nut editorial code and logic says it is not. possible arrangement:
6 6 8 -1 1 -1 4 5 -1 8 1 4 5 8 -1 1 4 5 1
This is a test case from the contest only, I only switched 1s and 8s. proving that order in which we start placing elements does matter.

2 Likes

Exactly… I got the same case while using Debug My Code button
My code produces

4 4 4 6 -1 -1 5 -1 8 7 3 2 1 10 9 -1 8 7 3 -1 2 -1 1 10 9 2 1 10 9 1 -1 10 9 10 -1 9 10 9 9 9 

for which the answer is YES

1 Like

Why has ratings changed if no WA were rejudged or contest made unrated?

1 Like

Isn’t the answer for this case is YES ?
4 4 4 6 -1 -1 5 -1 8 7 3 2 1 10 9 -1 8 7 3 -1 2 -1 1 10 9 2 1 10 9 1 -1 10 9 10 -1 9 10 9 9 9

Are testcases wrong ?

1 Like

why should
1
40 4
9 9 9 3 -1 -1 1 -1 1 2 4 2 9 9 9 -1 10 1 10 -1 10 -1 10 10 9 6 8 1 7 8 -1 9 10 7 -1 2 4 4 3 5
be no ?? if it’s a mistake, does that mean they just run the editorial code on random numbers and just compare the result to us without a proper checker ?

For those who’re confused with the editorial or the testcases: Don’t worry, the editorial and the testcases are completely wrong.

3 Likes

@iceknight1093 @pols_agyi_pols @kingmessi please help here. Seems like there is an issue with problem Scam.

1 Like

The official solution and several “correct” solutions all report this test case “No” which seems to be “yes”:
1
16 9
9 -1 -1 1 2 3 4 5 1 2 -1 6 3 4 5 6

1 Like