Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Optimizacija
  9. {
  10.  
  11. class Video
  12. {
  13. public int Index { get; set; }
  14. public int Size { get; set; }
  15.  
  16. public Video(int index, int size)
  17. {
  18. Index = index;
  19. Size = size;
  20. }
  21. }
  22. class Program
  23. {
  24. static public int TotalVideosCount;
  25. static public int EndPointsCount;
  26. static public int RequestsCount;
  27. static public int CachesCount;
  28. static public int CacheSize;
  29. static public string inputFile = @"videos_worth_spreading.in";
  30. static public Video[] Videos;
  31.  
  32. static void Main(string[] args)
  33. {
  34. Read();
  35. WriteTo(@"results1.txt");
  36. Read();
  37. WriteTo(@"results2.txt");
  38. Read();
  39. WriteTo(@"results3.txt");
  40. Read();
  41. WriteTo(@"results4.txt");
  42. Read();
  43. WriteTo(@"results5.txt");
  44. Read();
  45. WriteTo(@"results6.txt");
  46. Read();
  47. WriteTo(@"results7.txt");
  48. Read();
  49. WriteTo(@"results8.txt");
  50. Read();
  51. WriteTo(@"results9.txt");
  52.  
  53. }
  54.  
  55. static void WriteTo(string path)
  56. {
  57. Random rnd = new Random();
  58. using (StreamWriter writer = new StreamWriter(path))
  59. {
  60. writer.WriteLine(CachesCount);
  61. for (int i = 0; i < CachesCount; i++)
  62. {
  63. writer.Write(i);
  64. Video[] videos = new Video[TotalVideosCount];
  65. int videosInCache = 0;
  66. int sum = 0;
  67. while (sum < CacheSize - CacheSize/5)
  68. {
  69. int nextVideo = 0;
  70. //randomai---------------------------------------------------------------------------------
  71. nextVideo = rnd.Next(0, TotalVideosCount);
  72. if (sum + Videos[nextVideo].Size < CacheSize)
  73. {
  74. videos[videosInCache++] = Videos[nextVideo];
  75. RemoveVideo(nextVideo);
  76. }
  77. else
  78. {
  79. nextVideo = rnd.Next(0, TotalVideosCount);
  80. if (sum + Videos[nextVideo].Size < CacheSize)
  81. {
  82. videos[videosInCache++] = Videos[nextVideo];
  83. RemoveVideo(nextVideo);
  84. }
  85. else
  86. {
  87. nextVideo = rnd.Next(0, TotalVideosCount);
  88. if (sum + Videos[nextVideo].Size < CacheSize)
  89. {
  90. videos[videosInCache++] = Videos[nextVideo];
  91. RemoveVideo(nextVideo);
  92. }
  93. else
  94. {
  95. nextVideo = rnd.Next(0, TotalVideosCount);
  96. if (sum + Videos[nextVideo].Size < CacheSize)
  97. {
  98. videos[videosInCache++] = Videos[nextVideo];
  99. RemoveVideo(nextVideo);
  100. }
  101. else
  102. {
  103. nextVideo = rnd.Next(0, TotalVideosCount);
  104. if (sum + Videos[nextVideo].Size < CacheSize)
  105. {
  106. videos[videosInCache++] = Videos[nextVideo];
  107. RemoveVideo(nextVideo);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. //randomu pabaiga+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  114. sum = VideosSizeSum(videosInCache, videos);
  115. }
  116. for (int f = 0; f < videosInCache; f++)
  117. {
  118. writer.Write(" " + videos[f].Index);
  119. }
  120. writer.WriteLine();
  121. }
  122. }
  123. }
  124.  
  125. static void RemoveVideo(int index)
  126. {
  127. TotalVideosCount--;
  128. for (int i = index; i < TotalVideosCount; i++)
  129. {
  130. Videos[i] = Videos[i + 1];
  131. }
  132. }
  133.  
  134. static int VideosSizeSum(int count, Video[] videos)
  135. {
  136. int sum = 0;
  137. for (int i = 0; i < count; i++)
  138. {
  139. sum += videos[i].Size;
  140. }
  141. return sum;
  142. }
  143.  
  144. static void Read()
  145. {
  146. string line = null;
  147. string[] values;
  148. using (StreamReader reader = new StreamReader(inputFile))
  149. {
  150.  
  151. line = reader.ReadLine();
  152. values = line.Split(' ');
  153. TotalVideosCount = int.Parse(values[0]);
  154. EndPointsCount = int.Parse(values[1]);
  155. RequestsCount = int.Parse(values[2]);
  156. CachesCount = int.Parse(values[3]);
  157. CacheSize = int.Parse(values[4]);
  158. Videos = new Video[TotalVideosCount];
  159.  
  160. line = reader.ReadLine();
  161. values = line.Split(' ');
  162. for (int i = 0; i < values.Length; i++)
  163. {
  164. Videos[i] = new Video(i, int.Parse(values[i]));
  165. }
  166. }
  167. }
  168.  
  169. static int TimeSaved(int centerLatency, int cacheLatency, int requests)
  170. {
  171. return (centerLatency - cacheLatency) * requests;
  172. }
  173.  
  174.  
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement