<?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=1354_-_varf</id>
	<title>1354 - varf - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=1354_-_varf"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1354_-_varf&amp;action=history"/>
	<updated>2026-05-02T16:11:07Z</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=1354_-_varf&amp;diff=9820&amp;oldid=prev</id>
		<title>Oros Ioana Diana: Pagină nouă: = Enunț = Se consideră un şir &lt;code&gt;a&lt;/code&gt; cu &lt;code&gt;n&lt;/code&gt; numere naturale distincte: &lt;code&gt;a1, a2,..., an&lt;/code&gt;.  Eliminând &lt;code&gt;n-k&lt;/code&gt; numere din șirul a vom obține un subșir de lungime &lt;code&gt;k&lt;/code&gt; al șirului &lt;code&gt;a&lt;/code&gt;.  Definim subșir &lt;code&gt;vârf&lt;/code&gt; de lungime &lt;code&gt;k&lt;/code&gt; al șirului &lt;code&gt;a&lt;/code&gt; un subșir &lt;code&gt;x&lt;/code&gt; cu proprietatea că acesta conține un număr &lt;code&gt;xi&lt;/code&gt; (&lt;code&gt;1&lt;i&lt;k&lt;/code&gt;) astfel încât:  &lt;code&gt;x1&lt;/code&gt; &lt;...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1354_-_varf&amp;diff=9820&amp;oldid=prev"/>
		<updated>2024-05-18T19:11:57Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Enunț = Se consideră un şir &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; numere naturale distincte: &amp;lt;code&amp;gt;a1, a2,..., an&amp;lt;/code&amp;gt;.  Eliminând &amp;lt;code&amp;gt;n-k&amp;lt;/code&amp;gt; numere din șirul a vom obține un subșir de lungime &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; al șirului &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.  Definim subșir &amp;lt;code&amp;gt;vârf&amp;lt;/code&amp;gt; de lungime &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; al șirului &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; un subșir &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; cu proprietatea că acesta conține un număr &amp;lt;code&amp;gt;xi&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;1&amp;lt;i&amp;lt;k&amp;lt;/code&amp;gt;) astfel încât:  &amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt; &amp;lt;...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Enunț =&lt;br /&gt;
Se consideră un şir &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; numere naturale distincte: &amp;lt;code&amp;gt;a1, a2,..., an&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Eliminând &amp;lt;code&amp;gt;n-k&amp;lt;/code&amp;gt; numere din șirul a vom obține un subșir de lungime &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; al șirului &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Definim subșir &amp;lt;code&amp;gt;vârf&amp;lt;/code&amp;gt; de lungime &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; al șirului &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; un subșir &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; cu proprietatea că acesta conține un număr &amp;lt;code&amp;gt;xi&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;1&amp;lt;i&amp;lt;k&amp;lt;/code&amp;gt;) astfel încât:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;x1&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;&amp;lt; x2&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;&amp;lt; … &amp;lt; xi-1&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;&amp;lt; xi&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;&amp;gt; xi+1&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;&amp;gt; … &amp;gt; xk&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Cerința =&lt;br /&gt;
Să se genereze toate subşirurile &amp;lt;code&amp;gt;vârf&amp;lt;/code&amp;gt; ale şirului &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;, de lungime minim &amp;lt;code&amp;gt;3&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;varfIN.txt&amp;lt;/code&amp;gt; conține pe prima linie numărul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, iar pe a doua linie &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; numere naturale distincte separate prin câte un spațiu, reprezentând numerele din șirul &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Fișierul de ieșire &amp;lt;code&amp;gt;varfOUT.txt&amp;lt;/code&amp;gt; va conține pe fiecare linie câte un subșir &amp;lt;code&amp;gt;vârf&amp;lt;/code&amp;gt;, în ordinea lexicografică a pozițiilor ocupate în șirul &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; de termenii subșirului. Numerele din fiecare linie vor fi separate prin câte un spațiu. În cazul în care restricțiile nu sunt îndeplinite, se va afișa mesajul &amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
= Restricții și precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;4 ≤ n ≤ 11&amp;lt;/code&amp;gt;&lt;br /&gt;
* numerele de pe a doua linie a fișierului de intrare vor fi mai mici decât &amp;lt;code&amp;gt;20&amp;lt;/code&amp;gt;&lt;br /&gt;
* subșirurile &amp;lt;code&amp;gt;vârf&amp;lt;/code&amp;gt; vor fi scrise în fișier în ordinea lexicografică a pozițiilor ocupate în șirul &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; de termenii fiecărui subșir.&lt;br /&gt;
* dacă nu există niciun subșir &amp;lt;code&amp;gt;vârf&amp;lt;/code&amp;gt; atunci fișierul de ieșire &amp;lt;code&amp;gt;varf.out&amp;lt;/code&amp;gt; va conține pe prima linie valoarea &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;varfIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 4&lt;br /&gt;
 2 1 4 3&lt;br /&gt;
&amp;lt;code&amp;gt;varfOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 2 4 3 &lt;br /&gt;
 1 4 3 &lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
Se pot construi doar două subșiruri &amp;lt;code&amp;gt;vârf&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;(2,4,3)&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;(1,4,3)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2: ==&lt;br /&gt;
&amp;lt;code&amp;gt;varfIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 3&lt;br /&gt;
 2 1 4 3&lt;br /&gt;
&amp;lt;code&amp;gt;varfOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 Datele nu corespund restrictiilor impuse&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
def afisare(k, fout, x, b):&lt;br /&gt;
    for i in range(1, k + 1):&lt;br /&gt;
        fout.write(str(x[b[i]]) + &amp;quot; &amp;quot;)&lt;br /&gt;
    fout.write(&amp;#039;\n&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def solutie(k, x, b):&lt;br /&gt;
    if k &amp;gt;= 3:&lt;br /&gt;
        st, dr = 1, k&lt;br /&gt;
        while x[b[st]] &amp;lt; x[b[st + 1]]:&lt;br /&gt;
            st += 1&lt;br /&gt;
        while x[b[dr]] &amp;lt; x[b[dr - 1]]:&lt;br /&gt;
            dr -= 1&lt;br /&gt;
        if st != dr:&lt;br /&gt;
            return False&lt;br /&gt;
        if st == 1 or dr == k or st == k or dr == 1:&lt;br /&gt;
            return False&lt;br /&gt;
    else:&lt;br /&gt;
        return False&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def bkt(k, n, x, b, fout, ok):&lt;br /&gt;
    for i in range(b[k - 1] + 1, n + 1):&lt;br /&gt;
        b[k] = i&lt;br /&gt;
        if k &amp;gt;= 3 and solutie(k, x, b):&lt;br /&gt;
            afisare(k, fout, x, b)&lt;br /&gt;
            ok[0] = 1&lt;br /&gt;
        bkt(k + 1, n, x, b, fout, ok)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;varfIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as fin:&lt;br /&gt;
        n = int(fin.readline().strip())&lt;br /&gt;
        x = [0] + list(map(int, fin.readline().strip().split()))&lt;br /&gt;
&lt;br /&gt;
    if not (4 &amp;lt;= n &amp;lt;= 11) or any(num &amp;gt;= 20 for num in x[1:]):&lt;br /&gt;
        with open(&amp;quot;varfOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fout:&lt;br /&gt;
            fout.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    b = [0] * 30&lt;br /&gt;
    ok = [0]&lt;br /&gt;
    with open(&amp;quot;varfOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fout:&lt;br /&gt;
        bkt(1, n, x, b, fout, ok)&lt;br /&gt;
        if ok[0] == 0:&lt;br /&gt;
            fout.write(&amp;quot;0&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Oros Ioana Diana</name></author>
	</entry>
</feed>