Codeforces | Solutions to Codeforces Problems | Learning library
kandi X-RAY | Codeforces Summary
kandi X-RAY | Codeforces Summary
Solutions to Codeforces Problems
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 Codeforces
Codeforces Key Features
Codeforces Examples and Code Snippets
Community Discussions
Trending Discussions on Codeforces
QUESTION
I am trying to participate in online Codeforces contests using Kotlin.
My understanding is I should use Kotlin script if my code is contained within a single file.
If I run the following file locally (version 1.6.10
):
ANSWER
Answered 2022-Mar-10 at 18:17I haven't found a way to do this with Kotlin script files, but you can also use normal .kt
files without having any classes in the file (my understanding is that Kotlin magically turns them into Java class bytecode/files):
QUESTION
Here is the task: https://codeforces.com/problemset/problem/492/B And here is my code:
...ANSWER
Answered 2022-Mar-05 at 12:26The n
integers are on a single line, and your code handles n
line separated inputs. Change it to
QUESTION
from bs4 import BeautifulSoup
import requests
import random
id_url = "https://codeforces.com/profile/akash77"
id_headers = {
"User-Agent": 'Mozilla/5.0(Windows NT 6.1Win64x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 87.0 .4280 .141 Safari / 537.36 '}
id_page = requests.get(id_url, headers=id_headers)
id_soup = BeautifulSoup(id_page.content, 'html.parser')
id_soup = id_soup.find('svg')
print(id_soup)
...ANSWER
Answered 2022-Jan-11 at 06:40svg tag is not included in the source code, it is rendered by Javascript.
QUESTION
There are multiple approaches to solve the codeforces problem F. Consecutive Subsequence:
You are given an integer array of length 𝑛
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to [𝑥,𝑥+1,…,𝑥+𝑘−1] for some value 𝑥 and length 𝑘.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array [5,3,1,2,4] the following arrays are subsequences: [3], [5,3,1,2,4], [5,1,4], but the array [1,3] is not.
The Python code for one approach I tried that does not use dynamic programming is shown below. To avoid repetition, the if statement checks that the number one lower than the current number is not present in the array before because then it could be included to increase the subsequence's length.
...ANSWER
Answered 2022-Jan-08 at 07:09I feel like a recursive function is easier to understand and follow. Basically you want to only check if your current step is better than before, if not, move forward one. I am sure the sorted part could be made faster as well, as it is clearly not necessary to sort completely to conclude we have a non-inceasing sequence. I leave that to you to improve:
QUESTION
I'm trying prove the correctness of my algorithm.
This is the problem in codeforces: https://codeforces.com/contest/1428/problem/C
Here's my code in C++ which was accepted:
...ANSWER
Answered 2021-Dec-26 at 09:47Great write-up.
This may be more commentary than the "formal proof" you might be seeking.
Here's something to consider: You don't need the ans
variable at all. You simply print top
when the inner for-loop completes. When the inner for-loop completes, I would assert that ans==top
anyway.
Hence, this simplification:
QUESTION
In this programming problem, the input is an n
×m
integer matrix. Typically, n
≈ 105 and m
≈ 10. The official solution (1606D, Tutorial) is quite imperative: it involves some matrix manipulation, precomputation and aggregation. For fun, I took it as an STUArray implementation exercise.
I have managed to implement it using STUArray, but still the program takes way more memory than permitted (256MB). Even when run locally, the maximum resident set size is >400 MB. On profiling, reading from stdin seems to be dominating the memory footprint:
Functions readv
and readv.readInt
, responsible for parsing integers and saving them into a 2D list, are taking around 50-70 MB, as opposed to around 16 MB = (106 integers) × (8 bytes per integer + 8 bytes per link).
Is there a hope I can get the total memory below 256 MB? I'm already using Text
package for input. Maybe I should avoid lists altogether and directly read integers from stdin to the array. How can we do that? Or, is the issue elsewhere?
ANSWER
Answered 2021-Dec-05 at 11:40Contrary to common belief Haskell is quite friendly with respect to problems like that. The real issue is that the array
library that comes with GHC is total garbage. Another big problem is that everyone is taught in Haskell to use lists where arrays should be used instead, which is usually one of the major sources of slow code and memory bloated programs. So, it is not surprising that GC takes a long time, it is because there is way too much stuff being allocation. Here is a run on the supplied input for the solution provided below:
QUESTION
My two submissions for a programming problem differ in just one expression (where anchors
is a nonempty list and (getIntegrals n)
is a state monad):
Submission 1. replicateM (length anchors - 1) (getIntegrals n)
Submission 2. sequenceA $ const (getIntegrals n) <$> tail anchors
The two expressions' equivalence should be easy to see at compile time itself, I guess. And yet, comparatively the sequenceA
one is slower, and more importantly, takes up >10x memory:
(with "Memory limit exceeded on test 4" error for the second entry, so it might be even worse).
Why is it so?
It is becoming quite hard to predict which optimizations are automatic and which are not!
EDIT: As suggested, pasting Submission 1 code below. In this interactive problem, the 'server' has a hidden tree of size n
. Our code's job is to find out that tree, with minimal number of queries of the form ? k
. Loosely speaking, the server's response to ? k
is the row corresponding to node k
in the adjacency distance matrix of the tree. Our choices of k
are: initially 1
, and then a bunch of nodes obtained from getAnchors
.
ANSWER
Answered 2021-Nov-09 at 22:52The problem here is related to inlining. I do not understand it completly, but here is what I understand.
InliningFirst we find that copy&pasting the definition of replicateM
into the Submission 1 yields the same bad performance as Submission 2 (submission). However if we replace the INLINABLE
pragma of replicateM
with a NOINLINE
pragma things work again (submission).
The INLINABLE
pragma on replicateM
is different from an INLINE
pragma, the latter leading to more inlining than the former. Specifically here if we define replicateM
in the same file Haskells heuristic for inlining decides to inline, but with replicateM
from base it decides against inlining in this case even in the presence of the INLINABLE
pragma.
sequenceA
and traverse
on the other hand both have INLINE
pragmas leading to inlining. Taking a hint from the above experiment we can define a non-inlinable sequenceA
and indead this makes Solution 2 work (submission).
QUESTION
These are my answers for a codeforces problem and I don't know why the first snippet gives a wrong answer.
The second is accepted though.
I want to know if there is a problem with the judgment test cases because they seem to give the same output.
The problem says the following:
Given the boundaries of 2 intervals. Print the boundaries of their intersection.
Note: Boundaries mean the two ends of an interval which are the starting number and the ending number.
Input: Only one line contains two intervals [l1,r1], [l2,r2] where (1≤l1,l2,r1,r2≤109), (l1≤r1,l2≤r2).
It's guaranteed that l1≤r1 and l2≤r2.
Output: If there is an intersection between these 2 intervals print its boundaries , otherwise print -1.
Snippet 1
...ANSWER
Answered 2021-Nov-02 at 22:12In the first program you are checking only one condition
QUESTION
this is the problem enter link description here
the problem in function prime but it think it is true , and i can not find solution
i submit it in codeforces but it give me .Wrong answer on test 5
:-
the input :
...ANSWER
Answered 2021-Sep-21 at 01:19This for loop
QUESTION
I want to know why str += "A"
and str = str + "A"
have different performances.
In practice,
...ANSWER
Answered 2021-Aug-28 at 11:28i think compiler translates str += "A" to str = str + "A" so the result have to be same.
No, operator+=
is a separate function. It's not like in Python - x += y
does not translate to x = x + y
. That's a good thing because it allows the optimization which makes the performance difference you observed. I doubt the assembly is the same, it's two separate functions with different implementations.
operator+=
- No complexity, but reasonably it will be amortized O(1) per the copied character.operator+
O(n) at best obviously.
In x = x+y
the right side is evaluated first and so the compiler must allocate a new string object that is then moved into x
and the old string in x
is thus wastefully discarded. Under as-if rule the compiler is free to replace x = x+y
with x+=y
but it's hard to tell when or even if it does that.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Codeforces
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