Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
3705 - rectangles
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Exemplul 1: = Intrare 3 2 3 1 Ieșire 15 === Explicație === Sunt <code>3</code> rectangle-sequences: <code>(2; 3)</code>; <code>(2; 3; 1)</code>; <code>(3; 1)</code>. Ariile celor trei deptunghiuri ce le caracterizează sunt: <code>6</code>, <code>6</code>, <code>3</code>. == Exemplul 2: == Intrare 100000000000 2 3 1 Ieșire Datele nu corespund restrictiilor impuse == Rezolvare == <syntaxhighlight lang="python3" line="1"> MaxN = 1000010 mod = 10**9 + 7 v = [0] * MaxN left1 = [0] * MaxN left2 = [0] * MaxN right1 = [0] * MaxN right2 = [0] * MaxN def multiply(a, b, c, d): return ((a * b) % mod) * ((c * d) % mod) % mod def citeste_si_verifica_elemente(n): print(f"Introduceți {n} elemente (fiecare urmat de Enter):") v_temp = [] for i in range(1, n + 1): try: x = int(input(f"Elementul {i}: ")) if not (1 <= x <= 10**9): print("Datele nu corespund restricțiilor impuse") return None v_temp.append(x) except ValueError: print("Datele nu corespund restricțiilor impuse") return None return v_temp def main(): try: n = int(input("Introduceți lungimea șirului: ")) if not (1 <= n <= 10**6): print("Datele nu corespund restricțiilor impuse") return except ValueError: print("Datele nu corespund restricțiilor impuse") return v_input = citeste_si_verifica_elemente(n) if v_input is None: return v[1:n+1] = v_input for i in range(1, n+1): left1[i] = left2[i] = 0 right1[i] = right2[i] = n + 1 st1, st2, aux = [], [], [] for i in range(1, n+1): while st2 and v[i] > v[st2[-1]]: right2[st2[-1]] = i st2.pop() while st1 and v[i] > v[st1[-1]]: right1[st1[-1]] = i aux.append(st1.pop()) st1.append(i) while aux: st2.append(aux.pop()) st1.clear() st2.clear() for i in range(n, 0, -1): while st2 and v[i] >= v[st2[-1]]: left2[st2[-1]] = i st2.pop() while st1 and v[i] >= v[st1[-1]]: left1[st1[-1]] = i aux.append(st1.pop()) st1.append(i) while aux: st2.append(aux.pop()) sol = 0 for i in range(1, n+1): if left1[i] > 0: a = left1[i] - left2[i] b = right1[i] - i sol += multiply(a, b, v[i], v[left1[i]]) if right1[i] < n + 1: a = i - left1[i] b = right2[i] - right1[i] sol += multiply(a, b, v[i], v[right1[i]]) sol %= mod print(sol) if __name__ == "__main__": main() </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width