結果
問題 |
No.919 You Are A Project Manager
|
ユーザー |
![]() |
提出日時 | 2019-10-25 02:54:25 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,315 bytes |
コンパイル時間 | 3,253 ms |
コンパイル使用メモリ | 83,232 KB |
実行使用メモリ | 98,476 KB |
最終ジャッジ日時 | 2024-07-18 11:33:05 |
合計ジャッジ時間 | 58,160 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 52 WA * 3 |
ソースコード
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<>(); 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(); 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); } }