結果

問題 No.919 You Are A Project Manager
ユーザー aajisakaaajisaka
提出日時 2019-10-25 03:09:50
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,351 bytes
コンパイル時間 4,712 ms
コンパイル使用メモリ 77,168 KB
実行使用メモリ 71,984 KB
最終ジャッジ日時 2023-09-25 15:07:02
合計ジャッジ時間 57,953 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,489 ms
71,984 KB
testcase_01 AC 124 ms
55,908 KB
testcase_02 AC 123 ms
56,092 KB
testcase_03 AC 125 ms
56,148 KB
testcase_04 AC 460 ms
63,312 KB
testcase_05 AC 491 ms
64,644 KB
testcase_06 AC 281 ms
60,200 KB
testcase_07 AC 1,093 ms
69,604 KB
testcase_08 AC 516 ms
62,524 KB
testcase_09 AC 443 ms
62,316 KB
testcase_10 AC 571 ms
62,624 KB
testcase_11 AC 558 ms
62,816 KB
testcase_12 AC 177 ms
57,408 KB
testcase_13 AC 592 ms
62,528 KB
testcase_14 AC 216 ms
60,876 KB
testcase_15 AC 386 ms
61,368 KB
testcase_16 AC 873 ms
70,072 KB
testcase_17 AC 1,003 ms
65,228 KB
testcase_18 AC 912 ms
65,772 KB
testcase_19 AC 1,175 ms
65,908 KB
testcase_20 AC 1,635 ms
69,284 KB
testcase_21 WA -
testcase_22 AC 1,146 ms
69,052 KB
testcase_23 AC 1,128 ms
68,996 KB
testcase_24 AC 1,195 ms
69,460 KB
testcase_25 AC 1,146 ms
69,604 KB
testcase_26 AC 1,605 ms
70,004 KB
testcase_27 AC 1,483 ms
69,876 KB
testcase_28 AC 1,689 ms
69,988 KB
testcase_29 AC 914 ms
65,744 KB
testcase_30 AC 985 ms
65,176 KB
testcase_31 AC 910 ms
64,384 KB
testcase_32 AC 898 ms
68,744 KB
testcase_33 AC 1,195 ms
69,100 KB
testcase_34 AC 1,188 ms
69,336 KB
testcase_35 AC 1,719 ms
69,488 KB
testcase_36 AC 1,690 ms
69,136 KB
testcase_37 AC 1,699 ms
69,612 KB
testcase_38 AC 1,716 ms
69,652 KB
testcase_39 AC 179 ms
57,240 KB
testcase_40 AC 398 ms
61,800 KB
testcase_41 AC 246 ms
61,972 KB
testcase_42 AC 294 ms
63,328 KB
testcase_43 AC 317 ms
61,476 KB
testcase_44 AC 440 ms
65,080 KB
testcase_45 AC 285 ms
62,292 KB
testcase_46 AC 418 ms
61,072 KB
testcase_47 AC 1,184 ms
69,264 KB
testcase_48 AC 1,163 ms
69,368 KB
testcase_49 AC 1,077 ms
69,512 KB
testcase_50 AC 1,148 ms
67,636 KB
testcase_51 AC 1,112 ms
68,928 KB
testcase_52 AC 968 ms
69,304 KB
testcase_53 AC 1,055 ms
69,068 KB
testcase_54 AC 395 ms
61,240 KB
testcase_55 AC 126 ms
55,828 KB
testcase_56 AC 126 ms
55,928 KB
testcase_57 AC 131 ms
55,792 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<>();
            // 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 = 300;

        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();
                Median med2 = new Median();
                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());
            vm2.add(new Median());
        }
        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