<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=3647_-_secvente4</id>
	<title>3647 - secvente4 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=3647_-_secvente4"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=3647_-_secvente4&amp;action=history"/>
	<updated>2026-05-01T06:48:38Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.universitas.ro/index.php?title=3647_-_secvente4&amp;diff=9058&amp;oldid=prev</id>
		<title>Miawinator: Pagină nouă: Se dau &lt;code&gt;n&lt;/code&gt; și &lt;code&gt;t&lt;/code&gt; două numere naturale nenule și &lt;code&gt;S&lt;/code&gt; un șir binar de &lt;code&gt;n&lt;/code&gt; elemente indexate de la &lt;code&gt;1&lt;/code&gt;. O interschimbare în acest șir constă în alegerea a doi indici &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;j&lt;/code&gt; (&lt;code&gt;1 ≤ i, j ≤ n&lt;/code&gt;) și schimbarea între ele a valorilor &lt;code&gt;S[i]&lt;/code&gt; și &lt;code&gt;S[j]&lt;/code&gt;. O subsecvență de lungime &lt;code&gt;t&lt;/code&gt; a șirului &lt;code&gt;S&lt;/code&gt; reprezintă &lt;code&gt;t&lt;/code&gt; elemente aflate...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=3647_-_secvente4&amp;diff=9058&amp;oldid=prev"/>
		<updated>2024-01-05T02:24:34Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: Se dau &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; două numere naturale nenule și &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; un șir binar de &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; elemente indexate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;. O interschimbare în acest șir constă în alegerea a doi indici &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;j&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;1 ≤ i, j ≤ n&amp;lt;/code&amp;gt;) și schimbarea între ele a valorilor &amp;lt;code&amp;gt;S[i]&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;S[j]&amp;lt;/code&amp;gt;. O subsecvență de lungime &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; a șirului &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; reprezintă &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; elemente aflate...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Se dau &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; două numere naturale nenule și &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; un șir binar de &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; elemente indexate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;. O interschimbare în acest șir constă în alegerea a doi indici &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;j&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;1 ≤ i, j ≤ n&amp;lt;/code&amp;gt;) și schimbarea între ele a valorilor &amp;lt;code&amp;gt;S[i]&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;S[j]&amp;lt;/code&amp;gt;. O subsecvență de lungime &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; a șirului &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; reprezintă &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; elemente aflate pe poziții consecutive în șirul &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Cerința =&lt;br /&gt;
Să se determine numărul minim de interschimbări ce trebuie realizate în șirul &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; pentru a obține două subsecvențe disjuncte de lungime &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; formate doar din elemente egale cu &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Pe prima linie a fișierului &amp;lt;code&amp;gt;input.txt&amp;lt;/code&amp;gt; se află separate printr-un spațiu numere &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;. Pe a doua linie se află &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; caractere &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Pe prima linie a fișierului de ieșire &amp;lt;code&amp;gt;output.txt&amp;lt;/code&amp;gt; se va afișa numărul minim de interschimbări necesare pentru obținerea a două subsecvențe disjuncte de lungime &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; formate doar din elemente egale cu &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;. Dacă acest lucru este imposibil se va afișa &amp;lt;code&amp;gt;-1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Restricții și precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;2 ≤ n ≤ 1.000.000&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;2 * t ≤ n&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exemplul 1 ==&lt;br /&gt;
input.txt:&lt;br /&gt;
&lt;br /&gt;
7 2&lt;br /&gt;
&lt;br /&gt;
1010101&lt;br /&gt;
&lt;br /&gt;
output.txt:&lt;br /&gt;
&lt;br /&gt;
2&lt;br /&gt;
&lt;br /&gt;
Explicație:&lt;br /&gt;
&lt;br /&gt;
Elementul de pe poziția &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; se interschimbă cu elementul de pe poziția &amp;lt;code&amp;gt;6&amp;lt;/code&amp;gt;, iar elementul de pe poziția &amp;lt;code&amp;gt;3&amp;lt;/code&amp;gt; se interschimbă cu elementul de pe poziția &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
input.txt:&lt;br /&gt;
&lt;br /&gt;
26 3&lt;br /&gt;
&lt;br /&gt;
00010100100100010111001001&lt;br /&gt;
&lt;br /&gt;
output.txt:&lt;br /&gt;
&lt;br /&gt;
1&lt;br /&gt;
&lt;br /&gt;
Explicație:&lt;br /&gt;
&lt;br /&gt;
O secvență convenabilă se situează între pozițiile &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;6&amp;lt;/code&amp;gt; și alta între pozițiile &amp;lt;code&amp;gt;18&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;20&amp;lt;/code&amp;gt;. Este nevoie de o singură interschimbare pentru a pune un element de &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; pe poziția &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 3 ==&lt;br /&gt;
input.txt:&lt;br /&gt;
&lt;br /&gt;
9999999999999999 3&lt;br /&gt;
&lt;br /&gt;
00010100100100010111001001&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
Input-ul nu convine conditiilor&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python3&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
def verificare(n,t):&lt;br /&gt;
    if not(2&amp;lt;=n&amp;lt;=1000000):&lt;br /&gt;
        print(&amp;quot;Input-ul nu convine conditiilor&amp;quot;)&lt;br /&gt;
        exit()&lt;br /&gt;
    if not(2*t&amp;lt;=n):&lt;br /&gt;
        print(&amp;quot;Input-ul nu convine conditiilor&amp;quot;)&lt;br /&gt;
        exit()&lt;br /&gt;
&lt;br /&gt;
with open(&amp;quot;input.txt&amp;quot;, &amp;quot;r&amp;quot;) as f, open(&amp;quot;output.txt&amp;quot;, &amp;quot;w&amp;quot;) as g:&lt;br /&gt;
    n, k = map(str, f.readline().split())&lt;br /&gt;
    s = f.readline()&lt;br /&gt;
    n = int(n)&lt;br /&gt;
    k = int(k)&lt;br /&gt;
    verificare(n,k)&lt;br /&gt;
&lt;br /&gt;
    v = [int(ch) for ch in s]&lt;br /&gt;
    st = [0] * (n + 1)&lt;br /&gt;
    dr = [0] * (n + 1)&lt;br /&gt;
    &lt;br /&gt;
    sum_value = sum(v)&lt;br /&gt;
&lt;br /&gt;
    if sum_value &amp;lt; 2 * k:&lt;br /&gt;
        g.write(&amp;quot;-1&amp;quot;)&lt;br /&gt;
        exit()&lt;br /&gt;
&lt;br /&gt;
    sum_value = 0&lt;br /&gt;
&lt;br /&gt;
    for i in range(1, n + 1):&lt;br /&gt;
        sum_value += v[i - 1]&lt;br /&gt;
        if i - k &amp;gt;= 1:&lt;br /&gt;
            sum_value -= v[i - k - 1]&lt;br /&gt;
            st[i] = max(sum_value, st[i - 1])&lt;br /&gt;
&lt;br /&gt;
        if i == k:&lt;br /&gt;
            st[i] = sum_value&lt;br /&gt;
&lt;br /&gt;
    sum_value = 0&lt;br /&gt;
&lt;br /&gt;
    for i in range(n, 0, -1):&lt;br /&gt;
        sum_value += v[i - 1]&lt;br /&gt;
        if i + k &amp;lt;= n:&lt;br /&gt;
            sum_value -= v[i + k - 1]&lt;br /&gt;
            dr[i] = max(sum_value, dr[i + 1])&lt;br /&gt;
&lt;br /&gt;
        if i == n - k + 1:&lt;br /&gt;
            dr[i] = sum_value&lt;br /&gt;
&lt;br /&gt;
    sol = 2e9&lt;br /&gt;
&lt;br /&gt;
    for i in range(k, n - k + 1):&lt;br /&gt;
        sol = min(sol, 2 * k - st[i] - dr[i + 1])&lt;br /&gt;
&lt;br /&gt;
    g.write(str(sol))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Miawinator</name></author>
	</entry>
</feed>