Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- static class Heap {
- ArrayList<Pair> items;
- int [] a;
- public Heap(int n) {
- items = new ArrayList<Pair> ();
- a = new int [n+1];
- }
- //using shiftUp for insert.
- private void shiftUp () {
- int k = items.size() - 1;
- while (k > 0){
- int curr = (k-1)/2;
- Pair Item = items.get(k);
- Pair Parent = items.get(curr);
- if (Item.compareTo(Parent) < 0){
- items.set(k, Parent);
- a[Item.second] = curr;
- items.set(curr, Item);
- a[Parent.second] = k;
- k = curr;
- }
- else break;
- }
- }
- private int shiftUp (int index) {
- while (index > 0) {
- int curr = (index - 1) / 2;
- Pair Item = items.get(index);
- Pair Parent = items.get(curr);
- if (Item.compareTo(Parent) < 0) {
- items.set(index, Parent);
- a[Parent.second] = index;
- a[Item.second] = curr;
- items.set(curr, Item);
- index = curr;
- } else {
- return index;
- }
- }
- return index;
- }
- //using shiftDown fo delete
- private void shiftDown() {
- int curr = 0;
- int leftChild = 2*curr+1;
- while (leftChild < items.size()) {
- int max = leftChild;
- int rightChild = leftChild + 1;
- if (rightChild < items.size()) {
- if (items.get(rightChild).compareTo(items.get(leftChild)) < 0) {
- ++max;
- }
- }
- if (items.get(curr).compareTo(items.get(max)) > 0) {
- Pair tmp = items.get(curr);
- items.set(curr, items.get(max));
- a[items.get(max).second] = curr;
- items.set(max, tmp);
- a[tmp.second] = max;
- curr = max;
- leftChild = 2*curr+1;
- }
- else break;
- }
- }
- public void insert(Pair item) {
- items.add(item);
- a[item.second] = items.size()-1;
- shiftUp();
- }
- private int shiftDown(int curr) {
- int leftChild = 2*curr+1;
- while (leftChild < items.size()) {
- int max = leftChild;
- int rightChild = leftChild + 1;
- if (rightChild < items.size()) {
- if (items.get(rightChild).compareTo(items.get(leftChild)) > 0) {
- ++max;
- }
- }
- if (items.get(curr).compareTo(items.get(max)) < 0) {
- Pair tmp = items.get(curr);
- items.set(curr, items.get(max));
- items.set(max, tmp);
- a[items.get(max).second] = curr;
- a[tmp.second] = max;
- curr = max;
- leftChild = 2*curr+1;
- }
- else break;
- }
- return curr;
- }
- public Pair set(int y, int k){
- int number = a[y];
- Pair dt = items.get(number);
- if (dt.first )
- }
- public Pair deleteIndex(int index){
- Pair x = items.get(index);
- if (items.get(index).compareTo(items.get(items.size()-1)) < 0) {
- items.set(index, items.remove(items.size() - 1));
- int nowd = shiftDown(index);
- shiftUp(nowd);
- return x;
- }
- else
- {
- items.set(index, items.remove(items.size() - 1));
- int nowd = shiftUp(index);
- shiftDown(nowd);
- return x;
- }
- }
- public Pair remove() throws NoSuchElementException {
- if (items.size() == 0) {
- throw new NoSuchElementException();
- }
- if (items.size() == 1) return items.remove(0);
- Pair x = items.get(0);
- items.set(0, items.remove(items.size()-1));
- a[items.remove(items.size()-1).second] = 0;
- shiftDown();
- return x;
- }
- public int size() {
- return items.size();
- }
- public boolean isEmpty() {
- return items.isEmpty();
- }
- public String toString() {
- return items.toString();
- }
- public Pair min() {
- return items.get(0);
- }
- public void print() {
- for (int i = 0; i < items.size(); i++) {
- System.out.println(items.get(i).toString() + " ");
- }
- System.out.println();
- }
- }
- static Scanner s = new Scanner(System.in);
- static int len(int k){
- int c = 0;
- while (k > 0){
- c++;
- k /= 10;
- }
- return c;
- }
- static int gcd(int a, int b){
- if (a > b) {
- int t = a + b;
- a = t - a;
- b = t - b;
- }
- if (a == 0) return b;
- if (a == 1) return 1;
- return gcd(a, b % a);
- }
- static int k = 0;
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int tc = s.nextInt();
- for (int bgb = 0; bgb < tc; bgb++) {
- int n = s.nextInt();
- int [] a = new int [n+1];
- for (int i = 0; i < n; i++) {
- a[i+1] = s.nextInt();
- }
- int [] pref = prefsumma(a);
- int [] v = new int [n+1];
- for (int i = n-1; i > -1; i--) {
- v[i] = v[i+1] + a[i+1];
- }
- int last = n;
- int sum = 0;
- int [] to = new int [n+1];
- int sumdamage = 0;
- int min = Integer.MAX_VALUE;
- PriorityQueue<Pair> heap = new PriorityQueue<>();
- for (int i = 1; i <= n; i++) {
- sumdamage += a[i] * (i - 1);
- heap.add(new Pair(a[i] - v[i], i));
- }
- int [] is = new int [n+1];
- int [] interest = new int [n+1];
- for (int i = 1; i <= n; i++) {
- is[i] = 1;
- interest[i] = 1;
- }
- min = sumdamage;
- for (int t = n-1; t > 0; t--) {
- Pair p = heap.remove();
- int x = p.second;
- int d = p.first;
- sumdamage += d;
- }
- }
- }
- static class Pair implements Comparable<Pair>{
- int first;
- int second;
- public Pair(int first, int second) {
- this.first = first;
- this.second = second;
- }
- @Override
- public int compareTo(Pair pair) {
- if (this.first == pair.first) return second - pair.second; else return this.first - pair.first;
- }
- }
- static int [] prefsumma(int [] a){
- int n = a.length;
- int [] pref = new int [n];
- for (int i = 1; i < n; i++) {
- pref[i] = pref[i-1] + a[i];
- }
- return pref;
- }
- }
Add Comment
Please, Sign In to add comment