結果

問題 No.919 You Are A Project Manager
ユーザー aajisakaaajisaka
提出日時 2019-10-25 03:25:24
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,692 bytes
コンパイル時間 4,895 ms
コンパイル使用メモリ 76,932 KB
実行使用メモリ 70,520 KB
最終ジャッジ日時 2023-09-29 12:19:34
合計ジャッジ時間 64,874 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,496 ms
70,520 KB
testcase_01 AC 128 ms
58,016 KB
testcase_02 AC 127 ms
55,948 KB
testcase_03 AC 126 ms
55,836 KB
testcase_04 AC 500 ms
64,488 KB
testcase_05 AC 508 ms
64,224 KB
testcase_06 AC 277 ms
61,820 KB
testcase_07 AC 1,238 ms
69,544 KB
testcase_08 AC 552 ms
63,040 KB
testcase_09 AC 505 ms
61,932 KB
testcase_10 AC 612 ms
62,812 KB
testcase_11 AC 614 ms
64,564 KB
testcase_12 AC 178 ms
57,336 KB
testcase_13 AC 674 ms
62,960 KB
testcase_14 AC 211 ms
59,228 KB
testcase_15 AC 438 ms
62,128 KB
testcase_16 AC 978 ms
69,112 KB
testcase_17 AC 1,259 ms
69,988 KB
testcase_18 AC 1,062 ms
69,440 KB
testcase_19 AC 1,391 ms
69,540 KB
testcase_20 AC 1,824 ms
69,340 KB
testcase_21 WA -
testcase_22 AC 1,280 ms
69,144 KB
testcase_23 AC 1,272 ms
68,856 KB
testcase_24 AC 1,300 ms
69,208 KB
testcase_25 AC 1,222 ms
66,920 KB
testcase_26 AC 1,798 ms
69,468 KB
testcase_27 AC 1,795 ms
69,524 KB
testcase_28 AC 1,932 ms
69,452 KB
testcase_29 AC 1,166 ms
69,644 KB
testcase_30 AC 1,129 ms
69,544 KB
testcase_31 AC 1,194 ms
69,624 KB
testcase_32 AC 1,103 ms
69,436 KB
testcase_33 AC 1,328 ms
68,928 KB
testcase_34 AC 1,319 ms
69,140 KB
testcase_35 AC 1,962 ms
69,372 KB
testcase_36 AC 1,997 ms
69,324 KB
testcase_37 AC 1,969 ms
69,216 KB
testcase_38 AC 1,989 ms
69,616 KB
testcase_39 AC 178 ms
56,776 KB
testcase_40 AC 425 ms
63,208 KB
testcase_41 AC 235 ms
61,720 KB
testcase_42 AC 251 ms
59,344 KB
testcase_43 AC 356 ms
62,676 KB
testcase_44 AC 484 ms
62,424 KB
testcase_45 AC 277 ms
62,556 KB
testcase_46 AC 458 ms
60,592 KB
testcase_47 AC 1,294 ms
69,452 KB
testcase_48 AC 1,299 ms
69,084 KB
testcase_49 AC 1,305 ms
67,392 KB
testcase_50 AC 1,258 ms
69,192 KB
testcase_51 AC 1,179 ms
69,352 KB
testcase_52 AC 1,048 ms
68,852 KB
testcase_53 AC 1,247 ms
69,392 KB
testcase_54 AC 432 ms
63,544 KB
testcase_55 AC 128 ms
55,832 KB
testcase_56 AC 129 ms
56,160 KB
testcase_57 AC 130 ms
56,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Solution {

    static class Median {
        final PriorityQueue<Integer> lower, higher;
        int count;
        
        Median() {
            lower = new PriorityQueue<>(11, Comparator.reverseOrder());
            higher = new PriorityQueue<>(11);
            // Avoid NPE in push()
            lower.add(Integer.MIN_VALUE);
            higher.add(Integer.MAX_VALUE);
            count = 2;
        }

        Median(int capacity) {
            lower = new PriorityQueue<>(capacity/2+2, Comparator.reverseOrder());
            higher = new PriorityQueue<>(capacity/2+2);
            // Avoid NPE in push()
            lower.add(Integer.MIN_VALUE);
            higher.add(Integer.MAX_VALUE);
            count = 2;
        }

        void push(int a) {
            if (count % 2 == 0) {
                lower.add(a);
            } else {
                higher.add(a);
            }
            if (lower.peek() > higher.peek()) {
                Integer high = lower.poll();
                Integer low = higher.poll();
                higher.add(high);
                lower.add(low);
            }
            count++;
        }

        Integer get() {
            return lower.peek();
        }

        void erase(int a) {
            if (lower.remove(a)) {
                if (lower.size() < higher.size()) {
                    lower.add(higher.poll());
                }
            } else if (higher.remove(a)) {
                if (lower.size() + 1 > higher.size()) {
                    higher.add(lower.poll());
                }
            }
            count--;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        final int h = 400;

        List<Integer> a = new ArrayList<>();
        for(int i=0; i<n; i++) {
            a.add(sc.nextInt());
        }

        long ans = 0;
        for(int k=1; k<=Math.min(h, n); k++) {
            List<Long> v1 = new ArrayList<>();
            List<Long> v2 = new ArrayList<>();
            v1.add(0L); v2.add(0L);
            long no1 = 0;
            long no2 = 0;
            long ma1 = 0;
            long ma2 = 0;
            for(int i=0; i+k<=n; i+=k) {
                Median med1 = new Median(k);
                Median med2 = new Median(k);
                for(int j=i; j<i+k; j++) {
                    med1.push(a.get(j));
                    med2.push(a.get(n-1-j));
                }
                no1 += med1.get();
                ma1 = Math.max(ma1, no1);
                v1.add(ma1);

                no2 += med2.get();
                ma2 = Math.max(ma2, no2);
                v2.add(ma2);
            }
            for(int i=0; i<v1.size(); i++) {
                ans = Math.max(ans, (v1.get(i)+v2.get(v1.size()-1-i))*k);
            }
        }

        int q = n/(h+1);
        List<Median> vm1 = new ArrayList<>();
        List<Median> vm2 = new ArrayList<>();
        for(int i=0; i<q; i++) {
            vm1.add(new Median(h+1));
            vm2.add(new Median(h+1));
        }
        for(int i=0; i<q*(h+1); i++) {
            vm1.get(i/(h+1)).push(a.get(i));
            vm2.get(i/(h+1)).push(a.get(n-1-i));
        }

        for(int k=h+1; k<=n; k++) {
            int t = vm1.size();
            List<Long> v1 = new ArrayList<>();
            List<Long> v2 = new ArrayList<>();
            v1.add(0L); v2.add(0L);
            long no1 = 0;
            long no2 = 0;
            long ma1 = 0;
            long ma2 = 0;
            for(int i=0; i<t; i++) {
                no1 += vm1.get(i).get();
                ma1 = Math.max(ma1, no1);
                v1.add(ma1);
                no2 += vm2.get(i).get();
                ma2 = Math.max(ma2, no2);
                v2.add(ma2);
            }
            for(int i=0; i<=t; i++) {
                ans = Math.max(ans, (v1.get(i)+v2.get(t-i))*k);
            }

            for(int i=0; i<t; i++) {
                int bleft = i*k;
                int bright = (i+1)*k;
                int aleft = i*(k+1);
                int aright = (i+1)*(k+1);
                if (aright > n) {
                    vm1.remove(vm1.size()-1);
                    vm2.remove(vm2.size()-1);
                    continue;
                }
                for(int j=bleft; j<aleft; j++) {
                    vm1.get(i).erase(a.get(j));
                    vm2.get(i).erase(a.get(n-1-j));
                }
                for(int j=bright; j<aright; j++) {
                    vm1.get(i).push(a.get(j));
                    vm2.get(i).push(a.get(n-1-j));
                }
            }
        }

        System.out.println(ans);
    }
}
0