Ejemplo n.º 1
0
        protected override ProblemOutput Solve(ProblemInput input)
        {
            var dictionary = input.Photos.GroupBy(photo => photo.IsVertical).ToDictionary(photos => photos.Key);
            IEnumerable <Photo> vertical   = dictionary.ContainsKey(true) ? dictionary[true] : (IEnumerable <Photo>)Array.Empty <Photo>();
            IEnumerable <Photo> horizontal = dictionary.ContainsKey(false) ? dictionary[false] : (IEnumerable <Photo>)Array.Empty <Photo>();

            var slides = horizontal.Select(photo => new Slide(new List <Photo> {
                photo
            }))
                         .Concat(VerticalUnifier.GetUnified(vertical.ToList(), NumbersGenerator)).ToList();
            ProblemOutput res = new ProblemOutput();

            res.Slides = new List <Slide>();

            HashSet <int> notPaired = new HashSet <int>(Enumerable.Range(0, slides.Count));
            //List<int> notPairedList = new List<int>(input.Photos.Select(x => x.Index).ToList());

            var last = NumbersGenerator.Next(slides.Count);

            notPaired.Remove(last);
            res.Slides.Add(slides[last]);

            bool[] lastTags = new bool[input.TagCount];

            for (int i = 0; i < slides.Count - 1; i++)
            {
                long bestScore = 0;
                int  pairId    = 0;
                foreach (var tagIndex in slides[last].TagsIndexes)
                {
                    lastTags[tagIndex] = true;
                }

                for (int j = 0; j < 10; j++)
                {
                    int nextId = this.NumbersGenerator.Next(0, slides.Count);

                    while (!notPaired.Contains(nextId) || nextId == last)
                    {
                        nextId = this.NumbersGenerator.Next(0, slides.Count);
                    }

                    long myScore = Calcutaor.CalculatePhotosScore(slides[last], slides[nextId]);

                    if (bestScore < myScore || pairId == 0)
                    {
                        bestScore = myScore;
                        pairId    = nextId;
                    }
                }

                last = pairId;
                Array.Clear(lastTags, 0, lastTags.Length);
                notPaired.Remove(pairId);
                res.Slides.Add(slides[pairId]);
            }

            return(res);
            // TODO: add consideration for (1) vertical slides, (2) order between pairs.
        }
Ejemplo n.º 2
0
        protected ProblemOutput Solve2(ProblemInput input)
        {
            var dictionary = input.Photos.GroupBy(photo => photo.IsVertical).ToDictionary(photos => photos.Key);
            IEnumerable <Photo> vertical   = dictionary.ContainsKey(true) ? dictionary[true] : (IEnumerable <Photo>)Array.Empty <Photo>();
            IEnumerable <Photo> horizontal = dictionary.ContainsKey(false) ? dictionary[false] : (IEnumerable <Photo>)Array.Empty <Photo>();

            var slides = horizontal.Select(photo => new Slide(new List <Photo> {
                photo
            }))
                         .Concat(VerticalUnifier.GetUnified(vertical.ToList(), NumbersGenerator)).ToList();
            Dictionary <string, List <Slide> > tagToImages = new Dictionary <string, List <Slide> >();

            // HashSet<string> tags = new HashSet<string>();
            foreach (var image in slides)
            {
                foreach (var tag in image.Tags)
                {
                    //  tags.Add(tag);
                    if (tagToImages.ContainsKey(tag))
                    {
                        tagToImages[tag].Add(image);
                    }
                    else
                    {
                        tagToImages.Add(tag, new List <Slide>()
                        {
                            image
                        });
                    }
                }
            }

            ProblemOutput res = new ProblemOutput();

            res.Slides = new List <Slide>();
            HashSet <int> takenPhotos = new HashSet <int>();

            int count     = 1;
            int index     = 1;
            int lastTaken = 0;

            var first = GetFirstSlide(takenPhotos, slides);

            res.Slides.Add(first);
            takenPhotos.Add(first.Images[0].Index);
            if (first.Images.Count == 2)
            {
                takenPhotos.Add(first.Images[1].Index);
            }

            while (count < input.Photos.Length && index < slides.Count)
            {
                count++;
                var   photo     = res.Slides[res.Slides.Count - 1];
                Slide nextSlide = GetNextSlide(tagToImages, takenPhotos, slides, photo);
                if (nextSlide == null)
                {
                    var randomSlide = GetFirstSlide(takenPhotos, slides);
                    if (randomSlide == null)
                    {
                        break;
                    }
                    takenPhotos.Add(randomSlide.Images[0].Index);
                    if (randomSlide.Images.Count == 2)
                    {
                        takenPhotos.Add(randomSlide.Images[1].Index);
                    }
                    res.Slides.Add(randomSlide);
                }
                else
                {
                    takenPhotos.Add(nextSlide.Images[0].Index);
                    if (nextSlide.Images.Count == 2)
                    {
                        takenPhotos.Add(nextSlide.Images[1].Index);
                    }
                    res.Slides.Add(nextSlide);
                }
            }

            return(res);
        }