Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.HashSet;
  11. import java.util.PriorityQueue;
  12. import java.util.Queue;
  13.  
  14. public class Main {
  15.  
  16. private static class Video implements Comparable<Video> {
  17. int size, id, totalRequested, deduct = 0;
  18. ArrayList<Integer> ep;
  19. ArrayList<Integer> req;
  20. HashSet<Cache> cs;
  21. Queue<Cache> cacheQ;
  22. private Video(int size, int id) {
  23. this.id = id;
  24. this.size = size;
  25. ep = new ArrayList<>();
  26. req = new ArrayList<>();
  27. totalRequested = 0;
  28. cs = new HashSet<>();
  29. cacheQ = new ArrayList<>();
  30. }
  31.  
  32. private void calculatetotalReq() {
  33. for(int e: req) {
  34. totalRequested += e;
  35. }
  36. }
  37.  
  38. @Override
  39. public int compareTo(Video o) {
  40. return (o.totalRequested - o.size) - (totalRequested - size - deduct);
  41. }
  42.  
  43. public String toString() {
  44. return "Id => " + id + " Total requested => " + Integer.toString(totalRequested) + " Size => " + size + " Caches => " +
  45. cs.toString();
  46. }
  47. }
  48.  
  49.  
  50. // array of latacy DC
  51. // private static class EndPoint {
  52. // int id, latencyDC;
  53. //
  54. // private EndPoint(int latencyDC, int id) {
  55. // this.latencyDC = latencyDC;
  56. // this.id = id;
  57. // }
  58. //
  59. // }
  60.  
  61. private static class Cache {
  62. int id, rem;
  63. ArrayList<Integer> eI;
  64. ArrayList<Integer> eL;
  65. ArrayList<Video> vs;
  66. //HashMap<Integer, Integer> idReq
  67. private Cache(int id) {
  68. this.id = id;
  69. eI = new ArrayList<>();
  70. eL = new ArrayList<>();
  71. vs = new ArrayList<>();
  72. rem = X;
  73. }
  74.  
  75. public String toString() {
  76. return Integer.toString(id);
  77. }
  78.  
  79. private boolean add(Video v) {
  80. if(v.size <= rem) {
  81. rem -= v.size;
  82. vs.add(v);
  83. return true;
  84. }
  85. return false;
  86. }
  87.  
  88.  
  89. }
  90.  
  91. private static int V, E, R, C, X;
  92. private static Video[] videos;
  93. private static Cache[] caches;
  94. private static int[] epLatency;
  95. private static HashMap<Integer, Video> map;
  96. private static HashSet<Cache>[] ep;
  97. public static void main(String[] args) throws IOException {
  98. System.out.println("start");
  99. // PrintWriter pw = new PrintWriter("zoo_out.txt");
  100. // FileReader in = new FileReader("me_at_the_zoo.in");
  101.  
  102. // PrintWriter pw = new PrintWriter("worth_out.txt");
  103. // FileReader in = new FileReader("videos_worth_spreading.in");
  104. //
  105. // PrintWriter pw = new PrintWriter("kit_out.txt");
  106. // FileReader in = new FileReader("kittens.in");
  107. //
  108. // PrintWriter pw = new PrintWriter("trend_out.txt");
  109. // FileReader in = new FileReader("trending_today.in");
  110.  
  111. // BufferedReader input = new BufferedReader(in);
  112. PrintWriter pw = new PrintWriter(System.out, true);
  113. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  114.  
  115. String[] line = input.readLine().split(" ");
  116.  
  117. V = Integer.parseInt(line[0]);
  118. E = Integer.parseInt(line[1]);
  119. R = Integer.parseInt(line[2]);
  120. C = Integer.parseInt(line[3]);
  121. X = Integer.parseInt(line[4]);
  122.  
  123. epLatency = new int[E];
  124. ep = new HashSet[E];
  125. for(int i = 0; i < E; i++)ep[i] = new HashSet<>();
  126. caches = new Cache[C];
  127. for(int id = 0; id < C; id++)caches[id] = new Cache(id);
  128.  
  129. videos = new Video[V];
  130. map = new HashMap<>();
  131.  
  132. line = input.readLine().split(" ");
  133. for(int id = 0; id < V; id++) {
  134. videos[id] = new Video(Integer.parseInt(line[id]), id);
  135. map.put(id, videos[id]);
  136. }
  137.  
  138. // Endpoints
  139. int idc, l, cnt;
  140. for(int id = 0; id < E; id++) {
  141. line = input.readLine().split(" ");
  142. epLatency[id] = Integer.parseInt(line[0]);
  143. cnt = Integer.parseInt(line[1]);
  144. for(int i = 0; i < cnt; i++) {
  145. line = input.readLine().split(" ");
  146. idc = Integer.parseInt(line[0]);
  147. l = Integer.parseInt(line[1]);
  148. caches[idc].eI.add(id);
  149. caches[idc].eI.add(l);
  150. ep[id].add(caches[idc]);
  151. }
  152.  
  153. }
  154.  
  155. //Requests
  156. int a, b, c;
  157. Video v;
  158. for(int i = 0; i < R; i++) {
  159. line = input.readLine().split(" ");
  160. a = Integer.parseInt(line[0]); //id
  161. b = Integer.parseInt(line[1]); // endppont
  162. c = Integer.parseInt(line[2]); // reqs
  163. v = map.get(a);
  164. v.ep.add(b);
  165. v.req.add(c);
  166. v.cs.addAll(ep[b]);
  167. v.cacheQ.addAll(ep[b]);
  168.  
  169. }
  170.  
  171. for(int i = 0; i < V; i++) {
  172. videos[i].calculatetotalReq();
  173. }
  174.  
  175. // sol 1
  176.  
  177. PriorityQueue<Video> pq = new PriorityQueue<>();
  178.  
  179. for(int i = 0; i < V; i++) {
  180. pq.add(videos[i]);
  181. }
  182.  
  183. // add condition for cache size and for adding v back to queue
  184. boolean filled = true;
  185. while(!pq.isEmpty() ) {
  186. //System.out.println(pq.peek());
  187. Video ved = pq.poll();
  188. // for(Cache cc: ved.cs) {
  189. // if(cc.add(ved)){
  190. // ved.
  191. // }
  192. // }
  193. Queue<Cache> cacheQ = pq.poll().cacheQ;
  194.  
  195. while(!cacheQ.poll().add(ved)){
  196.  
  197. }
  198. ved.deduct = 100000;
  199. pq.add(ved);
  200.  
  201. }
  202.  
  203. int used = 0;
  204. for(int i = 0; i < C; i++) {
  205. if(!caches[i].vs.isEmpty())used++;
  206. }
  207. pw.println(used);
  208.  
  209. for(int i = 0; i < C; i++) {
  210. if(caches[i].vs.isEmpty())continue;
  211. pw.print(Integer.toString(caches[i].id));
  212. for(Video vv: caches[i].vs) {
  213. pw.print(" " + vv.id);
  214. }
  215. pw.println();
  216. }
  217.  
  218.  
  219.  
  220.  
  221. pw.close();
  222. input.close();
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement