Exemple #1
1
  public static void main(String[] args) throws IOException {
    in = new Reader();
    out = new PrintWriter(System.out, true);

    int N = in.nextInt();

    PriorityQueue<Long> pq = new PriorityQueue<Long>();
    for (int i = 0; i < N; i++) pq.add(in.nextLong());

    long cost = 0;
    while (pq.size() > 1) {
      long a = pq.poll() + pq.poll();
      cost += a;
      pq.add(a);
    }

    out.println(cost);
  }
  public static void main(String args[]) throws IOException {

    String text = "C:\\Users\\Roshan Rajan\\Desktop\\squaredance.txt";
    String gender = "";
    PriorityQueue<String> men = new PriorityQueue<String>();
    PriorityQueue<String> women = new PriorityQueue<String>();
    BufferedReader input = new BufferedReader(new FileReader(text));
    String line = null;
    while ((line = input.readLine()) != null) {
      gender = line.substring(0, 1);
      if (gender.equals("M")) men.add(line.substring(2));
      else women.add(line.substring(2));
    }
    input.close();

    while (!men.isEmpty() && !women.isEmpty()) {
      System.out.println("The couples are ");
      System.out.println(men.poll() + " is Dancing with " + women.poll());
    }
    if (men.isEmpty()) {
      System.out.println(women.size() + " girls are Waiting to Dance");
      System.out.println(women.peek() + " is the first one waiting");
    }
    if (women.isEmpty()) {
      System.out.println(men.size() + " guys are Waiting to Dance");
      System.out.println(men.peek() + " is the first one waiting");
    }
  }
Exemple #3
0
  static void test00() {
    Aron.beg();
    PriorityQueue<Interval> queue = new PriorityQueue<Interval>();
    Stack<Interval> stack = new Stack<Interval>();
    int[] arr1 = {4, 1, 2, 6, 9};
    int[] arr2 = {5, 1, 4, 9, 10};

    for (int i = 0; i < arr1.length; i++) {
      queue.add(new Interval(arr1[i], arr2[i]));
    }
    if (queue.size() > 0) {
      stack.push(queue.remove());
    }
    while (!queue.isEmpty()) {
      Interval top = stack.peek();
      Interval inter = queue.remove();
      if (top.end < inter.begin) stack.push(inter);
      else {
        stack.peek().end = Math.max(stack.peek().end, inter.end);
      }
    }
    while (!stack.empty()) {
      System.out.println("[" + stack.peek().begin + " " + stack.peek().end + "]");
      stack.pop();
    }

    Aron.end();
  }
  public static void solveOne() throws Exception {
    int n = nextInt();
    int m = nextInt();
    int k = nextInt();
    int w = nextInt();
    char[][][] levels = new char[k][n][];
    ;
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < n; j++) {
        levels[i][j] = nextString().toCharArray();
      }
    }
    int[][] cost = new int[k][k];
    PriorityQueue<int[]> q =
        new PriorityQueue<>(
            k * k,
            new Comparator<int[]>() {
              @Override
              public int compare(int[] o1, int[] o2) {
                return o1[2] - o2[2];
              }
            });
    for (int i = 0; i < k; i++) {
      for (int j = i + 1; j < k; j++) {
        int[] cur = {i, j, diff(levels[i], levels[j]) * w};
        cost[i][j] = cur[2];
        q.add(cur);
        //              px("edge", cur);
      }
    }
    //      px(q.size());
    //      for (int[] e: cost) px(e);
    DisjointUnionSet djs = new DisjointUnionSet(k);
    boolean[][] al = new boolean[k][k];
    int finalCost = n * m * k;
    for (; q.size() > 0; ) {
      int[] edge = q.poll();
      int p1 = djs.getPartitionId(edge[0]);
      int p2 = djs.getPartitionId(edge[1]);
      //          px(edge, p1 == p2, edge[2]);
      if (edge[2] > n * m) break;
      if (p1 != p2) {

        djs.unionElement(edge[0], edge[1]);
        finalCost -= n * m - edge[2];
        al[edge[1]][edge[0]] = true;
        al[edge[0]][edge[1]] = true;
      }
    }
    //      for (boolean[] e: al) px(e);
    pn(finalCost);
    boolean[] ex = new boolean[k];
    for (int i = 0; i < k; i++) {
      if (!ex[i]) {
        dfs(i, -1, ex, k, al);
      }
    }
  }
  public void getIndexInfo(String indexdir, int freqThreshold) {
    IndexReader reader = null;

    try {
      Directory dir = FSDirectory.open(new File(indexdir));
      System.out.println(dir);
      reader = IndexReader.open(dir);

      System.out.println("document num:" + reader.numDocs());
      System.out.println("======================");

      TermEnum terms = reader.terms();
      sortedTermQueue.clear();
      maxDocNum = reader.maxDoc();
      linkMap.clear();
      termList.clear();
      while (terms.next()) {
        // System.out.print(terms.term() + "\tDocFreq:" +
        TermDocs termDocs = reader.termDocs(terms.term());
        MyTerm temp = new MyTerm(terms.term(), termDocs, maxDocNum);
        if (temp.totalFreq < freqThreshold) {
          continue;
        } /*
           * if(temp.originTrem.text().length()==1){ continue; }
           */
        linkMap.put(temp.originTrem.text(), temp);
        sortedTermQueue.add(temp);
        termList.add(temp);
      }
      System.out.println("total Size:" + sortedTermQueue.size());
      System.out.println("mapsize:" + linkMap.keySet().size());
      // System.exit(0);
      int num = 0;
      this.maxFreq = sortedTermQueue.peek().totalFreq;
      while (!sortedTermQueue.isEmpty()) {
        num++;
        System.out.println(num + ":" + sortedTermQueue.poll());
      }
      System.out.println("read index info done");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        reader.close();

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  private static int dijkstra(int s, int e, int[] cost, int[][] flow) {
    int[] best = new int[cost.length];
    Arrays.fill(best, -1);
    best[s] = cost[s];
    PriorityQueue<Node> pq = new PriorityQueue<Node>();
    pq.add(new Node(cost[s], s));
    while (pq.size() > 0) {
      Node n = pq.poll();

      if (n.at == e) return n.cost;
      if (n.cost < best[n.at]) continue;
      for (int i = 0; i < cost.length; i++) {
        if (flow[n.at][i] > 0) {
          int c = min(cost[i], n.cost);
          if (c > best[i]) {
            best[i] = c;
            pq.add(new Node(c, i));
          }
        }
      }
    }
    return 0;
  }
Exemple #7
0
  private static List<Double> getTopN(INDArray vec, int N) {
    ArrayComparator comparator = new ArrayComparator();
    PriorityQueue<Double[]> queue = new PriorityQueue(vec.rows(), comparator);

    for (int j = 0; j < vec.length(); j++) {
      final Double[] pair = new Double[] {vec.getDouble(j), (double) j};
      if (queue.size() < N) {
        queue.add(pair);
      } else {
        Double[] head = queue.peek();
        if (comparator.compare(pair, head) > 0) {
          queue.poll();
          queue.add(pair);
        }
      }
    }
    List<Double> lowToHighSimLst = new ArrayList();

    while (!queue.isEmpty()) {
      double ind = queue.poll()[1];
      lowToHighSimLst.add(ind);
    }
    return Lists.reverse(lowToHighSimLst);
  }
  public JSONArray Cluster(String wekaFilePath, int clusterNum) throws Exception {
    File inputFile = new File(wekaFilePath);
    ArffLoader arf = new ArffLoader();
    arf.setFile(inputFile);
    Instances originIns = arf.getDataSet();
    Instances insTest = new Instances(originIns);
    insTest.deleteStringAttributes();
    int totalNum = insTest.numInstances();

    // SimpleKMeans sm = new SimpleKMeans();
    EM em = new EM();
    em.setNumClusters(clusterNum);
    MakeDensityBasedClusterer sm = new MakeDensityBasedClusterer();
    sm.setClusterer(em);
    sm.buildClusterer(insTest);

    System.out.println("totalNum:" + insTest.numInstances());
    System.out.println("============================");
    System.out.println(sm.toString());
    Map<Integer, ArrayList<String>> result = new HashMap<Integer, ArrayList<String>>();
    for (int i = 0; i < clusterNum; i++) {
      result.put(i, new ArrayList<String>());
    }

    for (int i = 0; i < totalNum; i++) {
      Instance ins = originIns.instance(i);
      String word = ins.stringValue(0);
      Instance tempIns = new Instance(ins);
      tempIns.deleteAttributeAt(0);
      int cluster = sm.clusterInstance(tempIns);
      result.get(cluster).add(word);
    }

    // print the result
    ArrayList<String> words = new ArrayList<String>();
    JSONArray keyWords = new JSONArray();
    for (int k : result.keySet()) {
      words = result.get(k);
      PriorityQueue<MyTerm> clusterQueue = new PriorityQueue<MyTerm>(1, MyTermCompare);
      for (int i = 0; i < words.size(); i++) {
        String s = words.get(i);
        assert linkMap.containsKey(s);
        int freq = linkMap.get(s).totalFreq;
        clusterQueue.add(linkMap.get(s));
        words.set(i, "(" + s + ":" + freq + ")");
      }

      JSONArray clusterArray = new JSONArray();
      int num = clusterQueue.size() / 10 + 1; // 5%
      int totalFreq = 0;
      int totalLength = 0;
      for (int i = 0; i < num && !clusterQueue.isEmpty(); ) {
        JSONObject mem = new JSONObject();
        MyTerm myTerm = clusterQueue.poll();
        String word = myTerm.originTrem.text();
        if (word.length() == 1) {
          continue;
        }
        mem.put("text", word);
        mem.put("freq", myTerm.totalFreq);
        clusterArray.put(mem);
        i++;
        totalFreq += myTerm.totalFreq;
        totalLength += word.length();
      }

      double averFreq = totalFreq * 1.0 / num;
      double averLength = totalLength * 1.0 / num;
      int count = 0;
      while (!clusterQueue.isEmpty() && count < num) {
        MyTerm myTerm = clusterQueue.poll();
        String word = myTerm.originTrem.text();
        int freq = myTerm.totalFreq;
        int times = (int) (word.length() / averFreq) + 1;
        if (freq > averFreq / times) {
          JSONObject mem = new JSONObject();
          mem.put("text", word);
          mem.put("freq", freq);
          mem.put("extra", true);
          clusterArray.put(mem);
        }
      }

      keyWords.put(clusterArray);
      System.out.println(
          "cluster" + k + ":" + words.size() + ":\t" + (int) (words.size() * 1.0 / totalNum * 100));
      if (result.get(k).size() < 100) {
        System.out.println(result.get(k));
      }
    }
    // System.out.println("errorNum:"+errorNum);
    return keyWords;
  }
Exemple #9
0
  // Sorts Both the file , individually
  // irrespective of their size.
  public static void initialize(
      String table1, String table1_join_col, String table2, String table2_join_col) {

    // System.out.println(DBSystem.PATH_FOR_DATA);
    // System.out.println(table1);

    tab1 = table1;
    tab2 = table2;

    for (Table t : DBSystem.tableList) {

      if (t.getName().equalsIgnoreCase(table1)) {
        table1JoinColIndex = t.getColumnNum().get(table1_join_col);
        table1JoinColType = t.getColumnData().get(table1_join_col);
        table1Lines = t.getLines();
        t1 = t;
      } else if (t.getName().equalsIgnoreCase(table2)) {
        table2JoinColIndex = t.getColumnNum().get(table2_join_col);
        table2JoinColType = t.getColumnData().get(table2_join_col);
        table2Lines = t.getLines();
        t2 = t;
      }
    }
    // System.out.println(table1 + " - " + table1_join_col + " - " + table1JoinColIndex + " - " +
    // table1JoinColType);
    // System.out.println(table2 + " - " + table2_join_col + " - " + table2JoinColIndex + " - " +
    // table2JoinColType);

    if (!table1JoinColType.equalsIgnoreCase(table2JoinColType)) {
      System.out.println("Both the tables must have similar join types");
      System.exit(1);
    }

    int numberOfcolumnsForJoin = 2; // For Joining Two tables

    // Sort the columns used for join
    for (int k = 0; k < numberOfcolumnsForJoin; k++) {
      String tab = null;
      String tabColType = null;
      int tabColIndex = 0;
      int tLines = 0;

      if (k == 0) {
        // Sorting table 1.
        tab = table1;
        tabColType = table1JoinColType;
        tabColIndex = table1JoinColIndex;
        tLines = table1Lines;
      } else if (k == 1) {
        // Sorting table 2
        tab = table2;
        tabColType = table2JoinColType;
        tabColIndex = table2JoinColIndex;
        tLines = table2Lines;
      }

      Comparator<String> cmp = new SortingComparator(tabColIndex, tabColType);
      PriorityQueue<String> priorityQueue = new PriorityQueue<String>(10, cmp);

      int remainingBufferSize = MAX_MEM_TO_USE;
      int fileCount = 1;
      String line;
      int inOutFlag = 0;

      for (int i = 0; i < tLines; i++) {
        line = DBSystem.getRecord(tab, i);

        // System.out.println(line);
        // String cols [] = line.split(",");

        remainingBufferSize -= line.getBytes().length;
        if (remainingBufferSize >= 0) {
          priorityQueue.add(line);
          inOutFlag = 1;
        } else {
          // write to file
          FileWriter fileWriter = null;
          // System.out.println("Written");
          try {
            // String fileName = setBlockFile(fileCount,table1);
            // System.out.println(fileName);
            fileWriter = new FileWriter(new File(setBlockFile(fileCount, tab)));
            int siz = priorityQueue.size();
            // System.out.println(siz);
            for (int j = 0; j < siz; j++) {
              fileWriter.write(priorityQueue.poll().toString());
              // System.out.println(priorityQueue.poll().toString());
              fileWriter.write("\n");
            }
            fileCount++;
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
            try {
              fileWriter.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          // set remainingSize back to MAX_MEM_TO_USE
          remainingBufferSize = MAX_MEM_TO_USE;
          // current read line
          priorityQueue.add(line);
          remainingBufferSize -= line.getBytes().length;
          inOutFlag = 0;
        }
      }

      if (inOutFlag == 1) {
        FileWriter fw = null;

        try {
          fw = new FileWriter(new File(setBlockFile(fileCount, tab)));
          int psiz = priorityQueue.size();
          for (int j = 0; j < psiz; j++) {
            fw.write(priorityQueue.poll().toString());
            // System.out.println(priorityQueue.poll().toString());
            fw.write("\n");
          }

        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            fw.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }

      if (k == 0) table1_tempfileCount = fileCount;
      if (k == 1) table2_tempfileCount = fileCount;
    }

    // Begin the merging process for both the files.

    // System.out.println(table1_tempfileCount);
    // System.out.println(table2_tempfileCount);

    // Merging Process
    SortedMergeJoin sortedMergeJoin =
        new SortedMergeJoin(
            table1,
            table1_tempfileCount,
            table1JoinColIndex,
            table1JoinColType,
            table2,
            table2_tempfileCount,
            table2JoinColIndex,
            table2JoinColType);
    sortedMergeJoin.execute();
  }
 public static int size() {
   return pq.size();
 }
Exemple #11
0
  public static void main(String[] args) throws Throwable {
    DATASET = System.getProperty("dataset", "movielens-pos.json");
    TRAINPERCENT = Double.parseDouble(System.getProperty("trainPercent", "0.8"));
    TESTPERCENT = Double.parseDouble(System.getProperty("testPercent", "0.5"));
    RUNS = Integer.parseInt(System.getProperty("runs", "1"));
    PREDICTIONS = Integer.parseInt(System.getProperty("predictions", "1"));
    // rand = new Random( Integer.parseInt( System.getProperty( "seed", "1" ) ) );
    rand = new Random();

    System.out.println("File: " + DATASET);
    System.out.println("Train Percent: " + TRAINPERCENT);
    System.out.println("Test Percent: " + TESTPERCENT);
    System.out.println("Runs: " + RUNS);
    System.out.println("Predictions: " + PREDICTIONS);
    docs = DatasetOld.loadDataset(DATASET);

    for (Algorithm alg : algs) {
      System.out.print(alg.name + "\t");
      double total = 0.0;

      long trainTime = 0;
      long predictTime = 0;

      for (int run = 0; run < RUNS; run++) {
        ArrayList<HashMap<Integer, Double>> traindocs = new ArrayList<HashMap<Integer, Double>>();
        ArrayList<HashMap<Integer, Double>> testdocs = new ArrayList<HashMap<Integer, Double>>();

        for (HashMap<Integer, Double> doc : docs) {
          if (rand.nextDouble() < TRAINPERCENT) traindocs.add(doc);
          else testdocs.add(doc);
        }

        long startTime = System.nanoTime();
        alg.train(traindocs);
        trainTime += System.nanoTime() - startTime;

        int successes = 0;

        for (HashMap<Integer, Double> testdoc : testdocs) {
          HashMap<Integer, Double> givenwords = new HashMap<Integer, Double>();
          HashSet<Integer> testwords = new HashSet<Integer>();

          for (int word : testdoc.keySet()) {
            if (rand.nextDouble() < TESTPERCENT) {
              testwords.add(word);
            } else {
              givenwords.put(word, testdoc.get(word));
            }
          }

          startTime = System.nanoTime();
          double[] scores = alg.predict(givenwords);
          predictTime += System.nanoTime() - startTime;
          // System.out.println(Arrays.toString( scores ));
          PriorityQueue<Pair> pq = new PriorityQueue<Pair>();

          for (int i = 0; i < scores.length; i++) {
            if (givenwords.containsKey(i)) {
              continue;
            }

            if (pq.size() < PREDICTIONS) {
              pq.add(new Pair(i, scores[i]));
            }
            if (scores[i] > pq.peek().score) {
              pq.poll();
              pq.add(new Pair(i, scores[i]));
            }
          }

          while (!pq.isEmpty()) {
            Pair pair = pq.poll();
            // System.out.println( WordIndex.get( pair.word ) + "\t" + pair.score + "\t" +
            // testwords.contains( pair.word ) );
            if (testwords.contains(pair.word)) {
              successes++;
            }
          }
        }

        total += (double) successes / PREDICTIONS / testdocs.size();
      }

      System.out.println(
          total / RUNS
              + "\t"
              + (trainTime / 1000000000.0 / RUNS)
              + "\t"
              + (predictTime / 1000000000.0 / RUNS));
    }
  }
Exemple #12
0
  /*
   * Finds the shortest route from a specified start node to a specified end node
   */
  private static void findShortestRoute(String inputString) {
    HashMap<Character, Node> nodeMapTemp = new HashMap<Character, Node>(nodeMap);
    HashMap<Character, Node> nodeMapDijkstra = new HashMap<Character, Node>();
    PriorityQueue<Node> pq = new PriorityQueue<Node>();
    Node current;
    Node currentTo;
    int shortestRoute = INFINITY;
    Set nodeSet;

    first = inputString.charAt(0);
    second = inputString.charAt(2);

    for (char key : nodeMap.keySet()) {
      nodeMap.get(key).setDistance(INFINITY);
    }

    if (first == second) {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (pq.size() != 0) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapDijkstra.put(pq.peek().getName(), pq.peek());
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          } else {
          }
        }
      }
      for (char key : nodeMapDijkstra.keySet()) {
        edgeList = nodeMapDijkstra.get(key).getEdges();
        for (i = 0; i < edgeList.size(); i++) {
          if (edgeList.get(i).getEdgeDestination() == second) {
            if (nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength()
                < shortestRoute) {
              shortestRoute =
                  nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength();
            }
          }
        }
      }
    } else {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (nodeMapTemp.get(second) != null) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          }
        }
      }
      shortestRoute = nodeMapTemp.get(second).getDistance();
    }
    try {
      bw.write(Integer.toString(shortestRoute));
      bw.newLine();
    } catch (IOException e) {
      System.out.println(e);
    }
  }