You are on page 1of 3

Problema cutiilor-Implementare in C#

Proiectarea algoritmilor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Cutii2
{
class Program
{
public static void LIS(int[] w,int[] g, int n, int[] lung)
{
System.Console.WriteLine("LIS");
lung[n - 1] = 1;
for (int i = (n - 2); i >= 0; i--)
{
int max = 0;
int sum=0;
for (int j = (i + 1); j < n; j++)
{

if (g[i]>w[j])
{
if ((sum+w[j]) < g[i])
{
sum = sum + w[j];
if (max < lung[j])
{
max = lung[j];
}
}
}
}
lung[i] = max + 1;
}

}
public static void TiparesteLIS(int[] w,int[] g, int n, int[] lung)
{
System.Console.WriteLine("TiparesteLIS");
int max = lung[0];
int poz = 0;
for (int i = 1; i < n; i++)
{
if (max < lung[i])
{
max = lung[i];
poz = i;
}
}
System.Console.WriteLine("Lungimea maxima este:" + max);
System.Console.WriteLine("Subsecventa este:");

System.Console.Write(w[poz]+" "+g[poz]);

for (int i = (poz + 1); i < n; i++)


{
if (lung[i] == (max - 1) && w[i] <= g[poz])
{
System.Console.WriteLine();
System.Console.Write(w[i]+" "+g[i]);
poz = i;
max = max - 1;
if (max == 0) break;
}
}

}
static void Main(string[] args)
{
int n;
int[] a = new int[100];
int[] w = new int[100];
//greutatatea proprie
int[] g = new int[100];
//greutatea suportata
TextReader tr = new StreamReader("fis.txt");
string linie;
linie = tr.ReadLine();
string[] q = linie.Split(' ');
n = int.Parse(q[0]);
for (int i = 0; i < n; i++)
{
linie = tr.ReadLine();
string[] b=linie.Split();
w[i] = int.Parse(b[0]);
g[i]=int.Parse(b[1]);

}
int[] lung = new int[100];
LIS(w,g, n, lung);
TiparesteLIS(w,g, n, lung);

}
}
}

You might also like