Kattis | repo contains solutions to all of the Kattis problems | Email library
kandi X-RAY | Kattis Summary
kandi X-RAY | Kattis Summary
This repo contains solutions to all of the Kattis problems I have completed. If you have any questions regarding a solution, feel more than free to email me at mikepf97@gmail.com
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Kattis
Kattis Key Features
Kattis Examples and Code Snippets
Community Discussions
Trending Discussions on Kattis
QUESTION
I have struggled with the problem "Perket" from Kattis: https://open.kattis.com/problems/perket
I succeed locally when i manually write the samples, getting the correct output. I get "runtime error" when i try to submit to Kattis though. Does any of you see the mistake? Is my program too slow for the requirements? Thank you very much and have a nice day.
...ANSWER
Answered 2022-Mar-15 at 10:12I went ahead, and did a lot of black-box bisecting to debug the issue.
It turns out the validation breaks (with a Run Time Error) on math.prod
.
math.prod
was introduced in Python 3.8, so the website is likely using an older version of Python 3. You should try and create your own version of math.prod
(or find another way to solve the problem).
Found the Python version, from https://open.kattis.com/help/python3 :
For Python 3, we use PyPy version Python 3.6.9 (7.3.1+dfsg-4, Apr 22 2020, 05:15:29) with the following flags: {files}.
For the record, CPython 3.6 has recently reached end of life (in December 2021). I'm not sure about the PyPy's version, but version 3.6 can't be downloaded from PyPy's website; only later versions.
QUESTION
I'm trying to learn C#, so I'm doing assignments on open.kattis.com. I have three variables called A, B and C and have to print them in a given order. A is the smallest variable, B is the middle variable and C is the largest variable.
I give the variables 1, 2, 5 and the order ABC, so the output print is 1 2 5 I give the variables 1, 10, 22 and the order CAB, so the output is 22 1 10
My question however is to improve my code. Right now I'm checking the input-order in a switch-case and was wondering if there is a better way.
My submission can be seen here:
...ANSWER
Answered 2022-Mar-14 at 14:56Let's generalize the solution. So you have data array:
QUESTION
I'm tring to do this kattis problem (Sort of Sorting), but my code only gets past the first trial. I've tried with my own sample cases and I don't see why it shouldn't work. Can someone check my code and tell me what I'm doing wrong?
...ANSWER
Answered 2022-Feb-18 at 14:28The key
argument to sort
(which is already stable) does what you are looking for.
QUESTION
I am stuck with the Pot task from Kattis where I should make a program which calculates following:
X = number1^pow1 + number2^pow2 + … + numberN^powN
X = P1 + P2 + .... + PN
For example my program should convert X = 212 + 1253 to X = 21^2 + 125^3.
This is Kattis sample input and output:
Sample Input: 2, 212, 1253
Sample Output: 1953566
But my code prints out the wrong answer, it prints out 1954378.
Here is my code:
...ANSWER
Answered 2021-Dec-08 at 21:00As Already explained in my comment the issue is of value assignment.
QUESTION
I have implemented a Disjoint Set Union in C++ for a Kattis problem. However, my solution is inefficient, as I get TLA (Time Limit Exceeded) for the first hidden test case. Can anyone spot an inefficiency with my code?
I have based my implementation on this article. In particular, I set the parent of the parent of the smaller set to the parent of the bigger set (when making an union of the sets). According to the mentioned article, this should be as efficient as using the rank:
...Both optimizations are equivalent in terms of time and space complexity. So in practice you can use any of them.
ANSWER
Answered 2022-Feb-08 at 01:21This seems to be a Kattis-specific IO problem. Adding ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
to the start of main, and changing both occurrences of endl
to '\n'
causes it to pass in .2 seconds.
I'll also mention that including is frowned upon here, but the only cause of your error was the IO. Your implementation of Union Find is correct and runs in the standard inverse Ackermann time bounds.
QUESTION
My code exceeds the time limit at test case 23. I have used dfs to traverse the connected houses. I did everything I could to optimize from my side. Kattis problem: where's my internet?
...ANSWER
Answered 2021-Nov-12 at 14:27The problem is not in your algorithm. It is just that input and output in Java is too slow for this online judge.
The solution is to use more efficient input and output code:
- For input, use a custom Fast Scanner. See this page for several options.
- For output, write the result in a StringBuilder, and then output this StringBuilder with a single
System.out
.
I was able to pass all the tests on your problem just by applying these two optimisations.
QUESTION
I have spent significant amount of time to figure out how to create algorithm for this problem. So basically, we can calculate the height of triangle with given 3 segments. But what if two or more segments are added? It is going to form a new triangle stick to the other. But I could not calculate the height. Thanks for any help.
...ANSWER
Answered 2021-Oct-30 at 18:26Given the base A we can construct (on the clockwise side) two new bases as:
we have
u^2 + h^2 = C^2
v^2 + h^2 = B^2
A = u + v
then given the A vector we can find the opposite vertex and make two new ones bases.
Using brute force (we can use some heuristic but the problem states that n
will be less than 9, then:
QUESTION
I'm solving Kattis' bokforing problem and one of the test cases fails due to execution time being too long (> 2 sec). Can anyone give me any advice on how I can improve?
...ANSWER
Answered 2021-Oct-16 at 09:32First of all I copied recommended IO classes from FAQ to the solution, removed double loop (there is no need to loop twice - reading inputs first and then processing them) and then the main trick was to use Dictionary
instead of array so there is no need to manually clear it/set amount to all items in it every time:
QUESTION
I am once again trying to solve a problem to practice for the upcoming USACO contest, and am having trouble meeting the time limit of 1 second: https://open.kattis.com/contests/v4xrif/problems/knigsoftheforest
All moose are knigs of the forest, but your latest moose-friend, Karl-Älgtav, is more interesting than most. In part because of his fondness of fermented blueberries, and in part because of the tribe he lives in. Each year his tribe holds a tournament to determine that year’s alpha-moose. The winner gets to lead the tribe for a year, and then permanently leaves the tribe. The pool of contenders stays constant over the years, apart from the old alpha-moose being replaced by a newcomer in each tournament.
Karl-Älgtav has recently begun to wonder when it will be his turn to win, and has asked you to help him determine this. He has supplied a list of the strength of each of the other moose in his tribe that will compete during the next n−1 years, along with their time of entry into the tournament. Assuming that the winner each year is the moose with greatest strength, determine when Karl-Älgtav becomes the alpha-moose.
Input The first line of input contains two space separated integers k (1≤k≤100000) and n (1≤n≤100000), denoting the size of the tournament pool and the number of years for which you have been supplied sufficient information.
Next is a single line describing Karl-Älgtav, containing the two integers y (2011≤y≤2011+n−1) and p (0≤p≤2^31−1). These are his year of entry into the tournament and his strength, respectively.
Then follow n+k−2 lines describing each of the other moose, in the same format as for Karl-Älgtav.
Note that exactly k of the moose will have 2011 as their year of entry, and that the remaining n−1 moose will have unique years of entry.
You may assume that the strength of each moose is unique.
Output The year Karl-Älgtav wins the tournament, or unknown if the given data is insufficient for determining this.
Sample Input 1
...
ANSWER
Answered 2021-Sep-26 at 19:07So basically I modified my second approach (see question) so that the keys and values reversed, which made the dictionary search much faster! Code:
QUESTION
I am trying to solve this problem with Python, and it has a CPU time limit of 1 second and a memory limit of 1024 megabytes: https://open.kattis.com/contests/v4xrif/problems/grandpabernie
Over the years, Grandpa Bernie has traveled all over the world. He doesn’t travel that much anymore, but he loves to tell his grandchildren stories from all these trips. He’ll tell them the story from when he went to Israel for the first time, or when he went to Greece for the third time.
His memory works in a funny way. He can easily remember his k:th trip to a particular country, but he’ll have a hard time remembering in which year he went on that trip. Given a list of all the trips Grandpa Bernie went on, can you answer a number of queries asking in which year he went on his k:th trip to a particular country?
Input The input consists of:
one line with one integer n (1≤n≤100000), the number of trips Grandpa Bernie went on;
n lines each containing the name s (1≤|s|≤20) of a country and an integer y (1≤y≤1000000) representing a trip to country s that Grandpa Bernie went on in year y;
one line with one integer q (1≤q≤100000), the number of queries;
q lines each containing the name s of a country and an integer k representing a query for the k:th time Grandpa Bernie went to country s.
Each country name only consists of letters from the English alphabet. It is also guaranteed that, for each query asking for the k:th trip to country s, k is at least 1 and no greater than the number of times Grandpa Bernie went to country s. In particular, it is guaranteed that Grandpa Bernie has visited country s at least once.
Output For each query for the k:th trip Grandpa Bernie went to a country s, output a single line containing the year in which Grandpa Bernie went on that trip.
Sample Input 1
...ANSWER
Answered 2021-Sep-25 at 17:43The slowest part of you program is going to be the repeated sorting each time you query.
To speed things up a bit-
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Kattis
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page