답안 #332481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332481 2020-12-02T17:27:33 Z KWang31 수열 (APIO14_sequence) Java 11
0 / 100
593 ms 44704 KB
//Convex Hull Trick, verified on CF319C (319 is ID)
import java.io.*; import java.util.*;
public class sequence{
  static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        
    } 
    
    static ArrayList<Long> M=new ArrayList<>();
    static ArrayList<Long> B=new ArrayList<>();
    static ArrayList<Integer> ind=new ArrayList<>();
    static int pointer=0;
    static int[][] ch;
    static int N;
    public static void main(String[] args){
        FastReader br=new FastReader();
        N=br.nextInt(); int K=br.nextInt();
        long[] s=new long[N]; 
        long[] dp=new long[N];
        long[] dp2=new long[N];
        ch=new int[N][K];
        s[0]=br.nextInt(); for(int i=1;i<N;i++){s[i]=s[i-1]+br.nextInt(); }
        //System.out.println(Arrays.toString(s));
        for (int i = 0; i < K; i++) {
            M=new ArrayList<>();
            B=new ArrayList<>();
            add(s[0],-s[0]*s[0],i);
            for (int j = 1; j < N; j++) {
                dp2[j]=query(s[j],N*i+j);
                add(s[j],dp[j]-s[j]*s[j],j);
                //System.out.println(M);
                //System.out.println(B);
            }
            
            for (int j = 0; j < N; j++) {
                dp[j]=dp2[j];
            }
            //System.out.println(Arrays.toString(dp));
        }
        //System.out.println(Arrays.toString(dp));
        System.out.println(dp[N-1]);
        StringBuilder sb=new StringBuilder();
        ArrayList<Integer> arl=new ArrayList<>();
        int cur=N-1;
        for (int i = K-1; i >=0; i--) {
            arl.add(ch[cur][i]); cur=ch[cur][i];
        }
        for (int i = K-1; i >=0; i--) {
            sb.append((arl.get(i)+1)+" ");
        }
        System.out.println(sb.toString());
    }
    public static boolean bad(int l1, int l2, int l3){//Using doubles to prevent overflow
        return (M.get(l1)-M.get(l3))*((double)B.get(l2)-B.get(l1))>=(M.get(l1)-M.get(l2))*((double)B.get(l3)-B.get(l1));
        //Intersection of l1 and l3 must be ABOVE l2
        //For l2 to be added
        //l1.m>l2.m>l3.m, as sum is strictly increasing
    }
    public static void add(long m, long b, int i){
        M.add(m); B.add(b); ind.add(i);
        while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)){
            M.remove(M.size()-2); B.remove(B.size()-2); ind.remove(ind.size()-2);
        }
    }
    public static long query(long x, int j){
        if(pointer>=M.size()){pointer=M.size()-1;}
        while(pointer<M.size()-1 && 
                M.get(pointer+1)*x+B.get(pointer+1) >= M.get(pointer)*x+B.get(pointer)){
            pointer++;
        }
        ch[j%N][j/N]=ind.get(pointer);
        return M.get(pointer)*x+B.get(pointer);
    }
    
    
}
//Debugging:
//Are you sure your algorithm is correct?
//Bounds: long
//Special cases: n=0,1?
//Make sure you remove your debugging code before you submit!
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 9584 KB contestant found the optimal answer: 108 == 108
2 Correct 112 ms 9424 KB contestant found the optimal answer: 999 == 999
3 Correct 133 ms 9580 KB contestant found the optimal answer: 0 == 0
4 Correct 112 ms 9708 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 109 ms 9580 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 116 ms 9728 KB contestant found the optimal answer: 1 == 1
7 Correct 112 ms 9604 KB contestant found the optimal answer: 1 == 1
8 Correct 107 ms 9580 KB contestant found the optimal answer: 1 == 1
9 Correct 109 ms 9580 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 112 ms 9708 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 110 ms 9580 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 111 ms 9580 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 112 ms 9584 KB contestant found the optimal answer: 140072 == 140072
14 Correct 117 ms 9716 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 107 ms 9472 KB contestant found the optimal answer: 805 == 805
16 Correct 109 ms 9580 KB contestant found the optimal answer: 900189994 == 900189994
17 Incorrect 108 ms 9580 KB contestant didn't find the optimal answer: 999919991 < 999919994
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 9580 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 121 ms 9552 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 135 ms 9836 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 115 ms 9604 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Incorrect 117 ms 9576 KB declared answer doesn't correspond to the split scheme: declared = 1005304678, real = 1005304662
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 143 ms 9652 KB declared answer doesn't correspond to the split scheme: declared = 610590000, real = 13922197
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 158 ms 9872 KB declared answer doesn't correspond to the split scheme: declared = 21503404, real = 20780198
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 498 ms 19972 KB declared answer doesn't correspond to the split scheme: declared = 1818678304, real = 1091759210
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 593 ms 44704 KB declared answer doesn't correspond to the split scheme: declared = 19795776960, real = 19759164057
2 Halted 0 ms 0 KB -