SlideShare a Scribd company logo
1 of 11
Metoda Greedy
Proiect realizat de
Bejan Mihai
Fie dată problema P cu spaţiul de soluţii:
S = {s1, s2, …, sN}
şi restricţiile R
P cere să se determine în S o submulţime Q* = {q1, q2,
…, qK}, astfel încât:
Q* = max {Q1, Q2, …, QR}, R=2N
C
C – criteriul de selecţie soluţiei.
Pentru obţinerea soluţiei Greedy
1. Se sortează elementele din S în măsura
descreşterii corespunderii criteriului C.
Se obţine şirul sortat: s*1, s*2, …, s*N.
2. Se consideră soluţia iniţial vidă B
3. Se adaugă consecutiv în B elementele s*1, s*2, …,
s*i, … atât timp cât nu se încalcă restricţiile R ale
problemei P.
Mai puţin formalizat, metoda poate fi descrisă astfel:
Din mulţimea de elemente S se alege acel element s*
care satisface cel mai bine criteriul C de includere în
soluţie. Dacă adăugarea s* la soluţia curentă nu
contravine restricţiilor R ale problemei, acesta se
include în soluţie. În caz contrar el se exclude din
mulţimea elementelor disponibile pentru adăugare.
Fie un set S de N (N<100) segmente pe axa 0X.
Pentru fiecare segment si este cunoscută abscisa
extremităţii stângi xi şi lungimea sa li.
Să se scrie un program, care va determina un subset cu
un număr maxim de segmente, S*⊆ S, astfel încât
segmentele din S* nu se vor intersecta între ele.
Criteriul C: cel mai “bun” segment este cel care se sfârşeşte
primul.
Restricţia R: Începutul segmentului care se adaugă trebuie să
aibă abscisa mai mare decât sfârşitul oricărui dintre
segmentele adăugate anterior în soluţie.
Structuri de date: fiecare segment va fi descris de un articol
de 2 componente – valorile extremităţilor, iar setul de
segmente – de un tablou de articole:
type segment = record st, dr : integer; end;
tablou = array [1..100] of segment;
1. Se sortează elementele din S după creşterea
coordonatei extremităţii drepte.
s*1, s*2, …, s*N.
Se formează soluţia iniţială B, din primul element
al şirului sortat (el nu poate încălca restricţia R)
k ⇐ 1, Bk ⇐ s*1.
2. Pentru fiecare i de la 2 la N se verifică condiţia:
Dacă s*i.st > bk.dr atunci
k ⇐ k+1, Bk ⇐ s*i
Datele se citesc din fişierul text data.in cu
următoarea structură: prima linie a fişierului
conţine un număr întreg N – numărul de
segmente. Următoarele N linii conţin cîte 2
numere întregi, separate prin spaţiu – abscisa
extremităţii stângi xi a segmentului i şi lungimea
lui li.
type segment=record st,dr : integer; end;
sets = array[1..100] of segment;
var a,b: sets;
n,k: integer;
procedure readdata(var x: sets; var n: integer);
var f: text;
i,r: integer;
begin
assign(f, 'data.in'); reset(f);
readln(f,n);
for i:=1 to n do
begin
readln(f, x[i].st, r);
x[i].dr:=x[i].st+r;
end;
close(f);
end;
procedure sort (var x:sets; n:integer);
var i,j: integer;
t: segment;
begin
for i:=1 to n -1 do
for j:=1 to n-i do
if x[j].dr>x[j+1].dr then
begin t:=x[j]; x[j]:=x[j+1]; x[j+1]:=t; end;
end;
procedure solve(var y:sets; x:sets; n:integer;
var k:integer);
var i: integer;
begin
y[1]:=x[1]; k:=1;
for i:=2 to n do
if x[i].st > y[k].dr then begin k:=k+1; y[k]:=x[i]; end;
end;
procedure print(x: sets;k:integer);
var i: integer;
begin
writeln(k);
for i:=1 to k do writeln(x[i].st, ' ',x[i].dr);
end;
begin
readdata(a,n);
sort(a,n);
solve(b,a,n,k);
print(b,k);
end.
Output >

More Related Content

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Metodagreedy 100617034511-phpapp02

  • 2. Fie dată problema P cu spaţiul de soluţii: S = {s1, s2, …, sN} şi restricţiile R P cere să se determine în S o submulţime Q* = {q1, q2, …, qK}, astfel încât: Q* = max {Q1, Q2, …, QR}, R=2N C C – criteriul de selecţie soluţiei.
  • 3. Pentru obţinerea soluţiei Greedy 1. Se sortează elementele din S în măsura descreşterii corespunderii criteriului C. Se obţine şirul sortat: s*1, s*2, …, s*N. 2. Se consideră soluţia iniţial vidă B 3. Se adaugă consecutiv în B elementele s*1, s*2, …, s*i, … atât timp cât nu se încalcă restricţiile R ale problemei P.
  • 4. Mai puţin formalizat, metoda poate fi descrisă astfel: Din mulţimea de elemente S se alege acel element s* care satisface cel mai bine criteriul C de includere în soluţie. Dacă adăugarea s* la soluţia curentă nu contravine restricţiilor R ale problemei, acesta se include în soluţie. În caz contrar el se exclude din mulţimea elementelor disponibile pentru adăugare.
  • 5. Fie un set S de N (N<100) segmente pe axa 0X. Pentru fiecare segment si este cunoscută abscisa extremităţii stângi xi şi lungimea sa li. Să se scrie un program, care va determina un subset cu un număr maxim de segmente, S*⊆ S, astfel încât segmentele din S* nu se vor intersecta între ele.
  • 6. Criteriul C: cel mai “bun” segment este cel care se sfârşeşte primul. Restricţia R: Începutul segmentului care se adaugă trebuie să aibă abscisa mai mare decât sfârşitul oricărui dintre segmentele adăugate anterior în soluţie. Structuri de date: fiecare segment va fi descris de un articol de 2 componente – valorile extremităţilor, iar setul de segmente – de un tablou de articole: type segment = record st, dr : integer; end; tablou = array [1..100] of segment;
  • 7. 1. Se sortează elementele din S după creşterea coordonatei extremităţii drepte. s*1, s*2, …, s*N. Se formează soluţia iniţială B, din primul element al şirului sortat (el nu poate încălca restricţia R) k ⇐ 1, Bk ⇐ s*1. 2. Pentru fiecare i de la 2 la N se verifică condiţia: Dacă s*i.st > bk.dr atunci k ⇐ k+1, Bk ⇐ s*i
  • 8. Datele se citesc din fişierul text data.in cu următoarea structură: prima linie a fişierului conţine un număr întreg N – numărul de segmente. Următoarele N linii conţin cîte 2 numere întregi, separate prin spaţiu – abscisa extremităţii stângi xi a segmentului i şi lungimea lui li.
  • 9. type segment=record st,dr : integer; end; sets = array[1..100] of segment; var a,b: sets; n,k: integer; procedure readdata(var x: sets; var n: integer); var f: text; i,r: integer; begin assign(f, 'data.in'); reset(f); readln(f,n); for i:=1 to n do begin readln(f, x[i].st, r); x[i].dr:=x[i].st+r; end; close(f); end;
  • 10. procedure sort (var x:sets; n:integer); var i,j: integer; t: segment; begin for i:=1 to n -1 do for j:=1 to n-i do if x[j].dr>x[j+1].dr then begin t:=x[j]; x[j]:=x[j+1]; x[j+1]:=t; end; end; procedure solve(var y:sets; x:sets; n:integer; var k:integer); var i: integer; begin y[1]:=x[1]; k:=1; for i:=2 to n do if x[i].st > y[k].dr then begin k:=k+1; y[k]:=x[i]; end; end;
  • 11. procedure print(x: sets;k:integer); var i: integer; begin writeln(k); for i:=1 to k do writeln(x[i].st, ' ',x[i].dr); end; begin readdata(a,n); sort(a,n); solve(b,a,n,k); print(b,k); end. Output >