Home > Blockchain >  Codeforces 768A: Wrong answer on test 10
Codeforces 768A: Wrong answer on test 10

Time:07-05

"Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come." — The Night's Watch oath.

With that begins the watch of Jon Snow. He is assigned the task to support the stewards.

This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him.

Can you find how many stewards will Jon support?

This is a link to the problem I'm trying to solve: https://codeforces.com/contest/768/problem/A

My solution basically sorts the list of values assigned to each steward, detects any items equal to the smallest or largest value of the list, and then removes the described values. This way we can find the number of items in the list that are neither the largest nor the smallest of all, by printing the length of our new list, and so we would get the answer to the problem.

Here's the code:

n = input()
a = list(map(int, input().split()))
a.sort()
b = list({i for i in a})
while b[0] in a:
    a.remove(b[0]) # removes the smallest value
while b[len(b)-1] in a:
    a.remove(b[len(b)-1]) # removes the largest value
print(len(a))

It passed 9 tests but printed a wrong answer to the 10th test. Here's the judgement protocol which includes the error: https://codeforces.com/contest/768/submission/162852310

Can anyone explain the problem with my code?

CodePudding user response:

You try to remove the smallest and largest values from your list. To get those values you create a new list b based on a set that is created from list a and use the first and last element from b.

The problem is that a set is unordered and therefore the resulting list is also unordered. This means that if you remove b[0] from a, this may not be the smallest value. The result might be different on different machines or platforms but here is an example from my machine: print(list({i for i in [2, 2, 3, 4, 1500, 1500]})) gives [1500, 2, 3, 4].

If you sort b, then everything will be fine.

n = input()
a = list(map(int, input().split()))
a.sort()
b = sorted({i for i in a})
while b[0] in a:
    a.remove(b[0]) # removes the smallest value
while b[-1] in a:
    a.remove(b[-1]) # removes the largest value
print(len(a))

But you don't even need to create the second list b, because you can get the smallest and largest value from the already sorted list a.

a = list(map(int, input().split()))
a.sort()
min_value, max_value = a[0], a[-1]
while min_value in a:
    a.remove(min_value)
while max_value in a:
    a.remove(max_value)
  • Related