Programozás‎ > ‎Feladatok‎ > ‎Pályázatok‎ > ‎Megoldás‎ > ‎

mb_palyazat.cs

Letöltés: mb_palyazat.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace Palyazatok
{
    class Program
    {
        static int n;
        static Munka[] munkak;
        static Munka[] munkakSorted;
        static int[] beosztas;
 
        static void Beolvas(string file)
        {
            string[] spllittedLine;
            StreamReader sr = new StreamReader(file);
 
            n = Convert.ToInt32(sr.ReadLine());
            munkak = new Munka[n];
 
            for (int i = 0; i < n; i++)
            {
                spllittedLine = sr.ReadLine().Split(' ');
                munkak[i] = new Munka(i, Convert.ToInt32(spllittedLine[1]), Convert.ToInt32(spllittedLine[0]) - 1);
            }
 
            sr.Close();
        }
 
        static void Kiir()
        {
            int sum = 0;
            for (int i = 0; i != beosztas.Length; i++)
            {
                if (beosztas[i] != -1)
                {
                    //Console.Write(beosztas[i] + 1 + " ");
                    sum += munkak[beosztas[i]].profit;
                }
            }
            Console.WriteLine(sum);
        }
 
        static void Munka()
        {
            munkakSorted = new Munka[n];
            for (int i = 0; i != n; i++) munkakSorted[i] = munkak[i];
            Array.Sort(munkakSorted);
 
            int done = 0;
            int indexCounter = 0;
            beosztas = new int[10000];
            for (int i = 0; i < beosztas.Length; i++) beosztas[i] = -1;
 
            while (done < n && indexCounter < n)
            {
                Munka munka = munkakSorted[indexCounter++];
                int lf = LastFree(munka.deadline);
                if (lf != -1)
                {
                    beosztas[lf] = munka.id;
                    done++;
                }
 
            }
        }
 
        static void Main(string[] args)
        {
            Beolvas("utemeza.be");
            Munka();
            Kiir();
            /*for (int i = 1; i <= 9; i++)
            {
                Console.WriteLine("Tesztadat: #" + i);
                Beolvas("utemez" + i + ".be");
                Munka();
                Kiir();
            }*/
            Console.ReadKey();
        }
 
        static int LastFree(int deadline)
        {
            for (int i = deadline; i >= 0; i--)
                if (beosztas[i] == -1) return i;
            return -1;
        }
    }
 
    struct Munka : IComparable<Munka>
    {
        public int id;
        public int profit;
        public int deadline;
 
        public Munka(int id, int profit, int deadline)
        {
            this.id = id;
            this.profit = profit;
            this.deadline = deadline;
        }
 
        #region IComparable Members
 
        public int CompareTo(Munka other)
        {
            if (this.profit == other.profit)
                return other.deadline - this.deadline;
            else
                return other.profit - this.profit;
        }
 
        #endregion
    }
}