Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.PriorityQueue;
- import java.util.Queue;
- public class Main {
- private static class Video implements Comparable<Video> {
- int size, id, totalRequested, deduct = 0;
- ArrayList<Integer> ep;
- ArrayList<Integer> req;
- HashSet<Cache> cs;
- Queue<Cache> cacheQ;
- private Video(int size, int id) {
- this.id = id;
- this.size = size;
- ep = new ArrayList<>();
- req = new ArrayList<>();
- totalRequested = 0;
- cs = new HashSet<>();
- cacheQ = new ArrayList<>();
- }
- private void calculatetotalReq() {
- for(int e: req) {
- totalRequested += e;
- }
- }
- @Override
- public int compareTo(Video o) {
- return (o.totalRequested - o.size) - (totalRequested - size - deduct);
- }
- public String toString() {
- return "Id => " + id + " Total requested => " + Integer.toString(totalRequested) + " Size => " + size + " Caches => " +
- cs.toString();
- }
- }
- // array of latacy DC
- // private static class EndPoint {
- // int id, latencyDC;
- //
- // private EndPoint(int latencyDC, int id) {
- // this.latencyDC = latencyDC;
- // this.id = id;
- // }
- //
- // }
- private static class Cache {
- int id, rem;
- ArrayList<Integer> eI;
- ArrayList<Integer> eL;
- ArrayList<Video> vs;
- //HashMap<Integer, Integer> idReq
- private Cache(int id) {
- this.id = id;
- eI = new ArrayList<>();
- eL = new ArrayList<>();
- vs = new ArrayList<>();
- rem = X;
- }
- public String toString() {
- return Integer.toString(id);
- }
- private boolean add(Video v) {
- if(v.size <= rem) {
- rem -= v.size;
- vs.add(v);
- return true;
- }
- return false;
- }
- }
- private static int V, E, R, C, X;
- private static Video[] videos;
- private static Cache[] caches;
- private static int[] epLatency;
- private static HashMap<Integer, Video> map;
- private static HashSet<Cache>[] ep;
- public static void main(String[] args) throws IOException {
- System.out.println("start");
- // PrintWriter pw = new PrintWriter("zoo_out.txt");
- // FileReader in = new FileReader("me_at_the_zoo.in");
- // PrintWriter pw = new PrintWriter("worth_out.txt");
- // FileReader in = new FileReader("videos_worth_spreading.in");
- //
- // PrintWriter pw = new PrintWriter("kit_out.txt");
- // FileReader in = new FileReader("kittens.in");
- //
- // PrintWriter pw = new PrintWriter("trend_out.txt");
- // FileReader in = new FileReader("trending_today.in");
- // BufferedReader input = new BufferedReader(in);
- PrintWriter pw = new PrintWriter(System.out, true);
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- String[] line = input.readLine().split(" ");
- V = Integer.parseInt(line[0]);
- E = Integer.parseInt(line[1]);
- R = Integer.parseInt(line[2]);
- C = Integer.parseInt(line[3]);
- X = Integer.parseInt(line[4]);
- epLatency = new int[E];
- ep = new HashSet[E];
- for(int i = 0; i < E; i++)ep[i] = new HashSet<>();
- caches = new Cache[C];
- for(int id = 0; id < C; id++)caches[id] = new Cache(id);
- videos = new Video[V];
- map = new HashMap<>();
- line = input.readLine().split(" ");
- for(int id = 0; id < V; id++) {
- videos[id] = new Video(Integer.parseInt(line[id]), id);
- map.put(id, videos[id]);
- }
- // Endpoints
- int idc, l, cnt;
- for(int id = 0; id < E; id++) {
- line = input.readLine().split(" ");
- epLatency[id] = Integer.parseInt(line[0]);
- cnt = Integer.parseInt(line[1]);
- for(int i = 0; i < cnt; i++) {
- line = input.readLine().split(" ");
- idc = Integer.parseInt(line[0]);
- l = Integer.parseInt(line[1]);
- caches[idc].eI.add(id);
- caches[idc].eI.add(l);
- ep[id].add(caches[idc]);
- }
- }
- //Requests
- int a, b, c;
- Video v;
- for(int i = 0; i < R; i++) {
- line = input.readLine().split(" ");
- a = Integer.parseInt(line[0]); //id
- b = Integer.parseInt(line[1]); // endppont
- c = Integer.parseInt(line[2]); // reqs
- v = map.get(a);
- v.ep.add(b);
- v.req.add(c);
- v.cs.addAll(ep[b]);
- v.cacheQ.addAll(ep[b]);
- }
- for(int i = 0; i < V; i++) {
- videos[i].calculatetotalReq();
- }
- // sol 1
- PriorityQueue<Video> pq = new PriorityQueue<>();
- for(int i = 0; i < V; i++) {
- pq.add(videos[i]);
- }
- // add condition for cache size and for adding v back to queue
- boolean filled = true;
- while(!pq.isEmpty() ) {
- //System.out.println(pq.peek());
- Video ved = pq.poll();
- // for(Cache cc: ved.cs) {
- // if(cc.add(ved)){
- // ved.
- // }
- // }
- Queue<Cache> cacheQ = pq.poll().cacheQ;
- while(!cacheQ.poll().add(ved)){
- }
- ved.deduct = 100000;
- pq.add(ved);
- }
- int used = 0;
- for(int i = 0; i < C; i++) {
- if(!caches[i].vs.isEmpty())used++;
- }
- pw.println(used);
- for(int i = 0; i < C; i++) {
- if(caches[i].vs.isEmpty())continue;
- pw.print(Integer.toString(caches[i].id));
- for(Video vv: caches[i].vs) {
- pw.print(" " + vv.id);
- }
- pw.println();
- }
- pw.close();
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement