結果

問題 No.919 You Are A Project Manager
ユーザー aajisakaaajisaka
提出日時 2019-10-25 03:11:23
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,366 bytes
コンパイル時間 3,929 ms
コンパイル使用メモリ 76,812 KB
実行使用メモリ 70,164 KB
最終ジャッジ日時 2023-09-25 15:11:00
合計ジャッジ時間 66,393 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,439 ms
68,520 KB
testcase_01 AC 126 ms
56,112 KB
testcase_02 AC 124 ms
56,200 KB
testcase_03 AC 124 ms
56,048 KB
testcase_04 AC 493 ms
64,264 KB
testcase_05 AC 498 ms
64,108 KB
testcase_06 AC 292 ms
60,276 KB
testcase_07 AC 1,319 ms
68,900 KB
testcase_08 AC 570 ms
62,744 KB
testcase_09 AC 514 ms
62,312 KB
testcase_10 AC 641 ms
63,124 KB
testcase_11 AC 621 ms
62,864 KB
testcase_12 AC 175 ms
57,248 KB
testcase_13 AC 689 ms
62,816 KB
testcase_14 AC 216 ms
61,160 KB
testcase_15 AC 439 ms
61,880 KB
testcase_16 AC 830 ms
63,272 KB
testcase_17 AC 965 ms
67,200 KB
testcase_18 AC 979 ms
64,760 KB
testcase_19 AC 1,324 ms
67,672 KB
testcase_20 AC 2,181 ms
69,400 KB
testcase_21 WA -
testcase_22 AC 1,378 ms
69,056 KB
testcase_23 AC 1,349 ms
69,780 KB
testcase_24 AC 1,416 ms
68,992 KB
testcase_25 AC 1,363 ms
69,656 KB
testcase_26 AC 2,192 ms
69,472 KB
testcase_27 AC 2,099 ms
70,164 KB
testcase_28 AC 2,403 ms
69,772 KB
testcase_29 AC 1,026 ms
65,308 KB
testcase_30 AC 1,124 ms
65,652 KB
testcase_31 AC 987 ms
65,028 KB
testcase_32 AC 977 ms
68,944 KB
testcase_33 AC 1,429 ms
68,868 KB
testcase_34 AC 1,421 ms
69,500 KB
testcase_35 AC 2,216 ms
69,720 KB
testcase_36 AC 2,202 ms
69,612 KB
testcase_37 AC 2,228 ms
69,644 KB
testcase_38 AC 2,202 ms
69,296 KB
testcase_39 AC 177 ms
58,124 KB
testcase_40 AC 455 ms
64,668 KB
testcase_41 AC 242 ms
61,524 KB
testcase_42 AC 280 ms
64,728 KB
testcase_43 AC 351 ms
61,560 KB
testcase_44 AC 525 ms
65,944 KB
testcase_45 AC 299 ms
63,316 KB
testcase_46 AC 456 ms
62,440 KB
testcase_47 AC 1,381 ms
69,300 KB
testcase_48 AC 1,351 ms
69,264 KB
testcase_49 AC 1,406 ms
69,684 KB
testcase_50 AC 1,425 ms
69,340 KB
testcase_51 AC 1,347 ms
69,496 KB
testcase_52 AC 1,064 ms
68,920 KB
testcase_53 AC 1,310 ms
69,420 KB
testcase_54 AC 439 ms
61,876 KB
testcase_55 AC 125 ms
56,200 KB
testcase_56 AC 127 ms
55,852 KB
testcase_57 AC 129 ms
55,800 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 = 464; // 10000^(2/3)

        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