Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Optimizacija
- {
- class Video
- {
- public int Index { get; set; }
- public int Size { get; set; }
- public Video(int index, int size)
- {
- Index = index;
- Size = size;
- }
- }
- class Program
- {
- static public int TotalVideosCount;
- static public int EndPointsCount;
- static public int RequestsCount;
- static public int CachesCount;
- static public int CacheSize;
- static public string inputFile = @"videos_worth_spreading.in";
- static public Video[] Videos;
- static void Main(string[] args)
- {
- Read();
- WriteTo(@"results1.txt");
- Read();
- WriteTo(@"results2.txt");
- Read();
- WriteTo(@"results3.txt");
- Read();
- WriteTo(@"results4.txt");
- Read();
- WriteTo(@"results5.txt");
- Read();
- WriteTo(@"results6.txt");
- Read();
- WriteTo(@"results7.txt");
- Read();
- WriteTo(@"results8.txt");
- Read();
- WriteTo(@"results9.txt");
- }
- static void WriteTo(string path)
- {
- Random rnd = new Random();
- using (StreamWriter writer = new StreamWriter(path))
- {
- writer.WriteLine(CachesCount);
- for (int i = 0; i < CachesCount; i++)
- {
- writer.Write(i);
- Video[] videos = new Video[TotalVideosCount];
- int videosInCache = 0;
- int sum = 0;
- while (sum < CacheSize - CacheSize/5)
- {
- int nextVideo = 0;
- //randomai---------------------------------------------------------------------------------
- nextVideo = rnd.Next(0, TotalVideosCount);
- if (sum + Videos[nextVideo].Size < CacheSize)
- {
- videos[videosInCache++] = Videos[nextVideo];
- RemoveVideo(nextVideo);
- }
- else
- {
- nextVideo = rnd.Next(0, TotalVideosCount);
- if (sum + Videos[nextVideo].Size < CacheSize)
- {
- videos[videosInCache++] = Videos[nextVideo];
- RemoveVideo(nextVideo);
- }
- else
- {
- nextVideo = rnd.Next(0, TotalVideosCount);
- if (sum + Videos[nextVideo].Size < CacheSize)
- {
- videos[videosInCache++] = Videos[nextVideo];
- RemoveVideo(nextVideo);
- }
- else
- {
- nextVideo = rnd.Next(0, TotalVideosCount);
- if (sum + Videos[nextVideo].Size < CacheSize)
- {
- videos[videosInCache++] = Videos[nextVideo];
- RemoveVideo(nextVideo);
- }
- else
- {
- nextVideo = rnd.Next(0, TotalVideosCount);
- if (sum + Videos[nextVideo].Size < CacheSize)
- {
- videos[videosInCache++] = Videos[nextVideo];
- RemoveVideo(nextVideo);
- }
- }
- }
- }
- }
- //randomu pabaiga+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- sum = VideosSizeSum(videosInCache, videos);
- }
- for (int f = 0; f < videosInCache; f++)
- {
- writer.Write(" " + videos[f].Index);
- }
- writer.WriteLine();
- }
- }
- }
- static void RemoveVideo(int index)
- {
- TotalVideosCount--;
- for (int i = index; i < TotalVideosCount; i++)
- {
- Videos[i] = Videos[i + 1];
- }
- }
- static int VideosSizeSum(int count, Video[] videos)
- {
- int sum = 0;
- for (int i = 0; i < count; i++)
- {
- sum += videos[i].Size;
- }
- return sum;
- }
- static void Read()
- {
- string line = null;
- string[] values;
- using (StreamReader reader = new StreamReader(inputFile))
- {
- line = reader.ReadLine();
- values = line.Split(' ');
- TotalVideosCount = int.Parse(values[0]);
- EndPointsCount = int.Parse(values[1]);
- RequestsCount = int.Parse(values[2]);
- CachesCount = int.Parse(values[3]);
- CacheSize = int.Parse(values[4]);
- Videos = new Video[TotalVideosCount];
- line = reader.ReadLine();
- values = line.Split(' ');
- for (int i = 0; i < values.Length; i++)
- {
- Videos[i] = new Video(i, int.Parse(values[i]));
- }
- }
- }
- static int TimeSaved(int centerLatency, int cacheLatency, int requests)
- {
- return (centerLatency - cacheLatency) * requests;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement