답안 #345704

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
345704 2021-01-08T00:40:52 Z KWang31 Gift (IZhO18_nicegift) Java 11
49 / 100
2000 ms 158248 KB
import java.io.*; import java.util.*;
public class nicegift{
  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()); 
        } 
        long nextLong(){
            return Long.parseLong(next());
        }
        
    } 
    public static class Pair implements Comparable<Pair>{
        int ind; long val;
        public Pair(int a, long b){
            this.ind=a; this.val=b;
        }
        public int compareTo(Pair other){
            if(this.val<other.val)return -1;
            if(this.val>other.val)return 1;
            if(this.ind<other.ind)return -1;
            return 1;
        }
    }
    static int MOD=998244353;
    static int[] rk, p,siz;
    public static void main(String[] args){
        FastReader br=new FastReader();
        int N=br.nextInt(); int K=br.nextInt();
        Pair[] a=new Pair[N];
        long sum=0; long max=0;
        for (int i = 0; i < N; i++) {
            a[i]=new Pair(1+i,br.nextLong()); sum+=a[i].val; max=Math.max(max,a[i].val);
        }
        if(sum%K>0 || sum/K < max){
            System.out.println("-1");return;
        }
        //A number is bottle neck if a[i]
        Arrays.sort(a);
        long cur=sum/K;
 
        //Tracks K+1 elements
        long d;StringBuilder ans=new StringBuilder();
        int op=0; int lp=0; int rp=N-1; long v; 
        for (int j = 0; j < N; j++) {
        	
            while(lp<N && a[lp].val==0) {
        	lp++;
            }
            while(rp>=0 && a[rp].val==cur) {
                   rp--;
             }
            if(rp<lp)break;
            //System.out.println(j+" "+lp+" "+rp);
            d=Math.min(a[lp].val, cur-a[rp].val);
            if(d==0)continue;
            op++;
            //System.out.println(j+" "+cur+" "+d);
            ans.append(d+" ");
            //System.out.println(lp+K-(N-rp-1));
            for(int k=lp; k<lp+K-(N-rp-1); k++) {//There are at least K elements
            	v=a[k].val-d; a[k]=new Pair(a[k].ind,v); ans.append(a[k].ind+" ");
            	
            }
            for(int k=rp+1; k<N; k++) {
            	v=a[k].val-d; a[k]=new Pair(a[k].ind,v); ans.append(a[k].ind+" ");
            }
            cur-=d;
            ans.append("\n"); 
            
        }
        //Now K of them are "bottlenecks"
        
        boolean val=false;
        for (int i = 0; i < N; i++) {
            if(a[i].val>0){
            	
                if(!val){
                    val=true;
                    op++;
                    ans.append(a[i].val+" ");
                }
                ans.append(a[i].ind+" ");
            }
        }
        System.out.println(op);
        System.out.println(ans.toString());
    }
    
    
}
//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 142 ms 10432 KB n=4
2 Correct 151 ms 10508 KB n=3
3 Correct 80 ms 8516 KB n=3
4 Correct 151 ms 10440 KB n=4
5 Correct 95 ms 8364 KB n=4
6 Correct 88 ms 8308 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 10432 KB n=4
2 Correct 151 ms 10508 KB n=3
3 Correct 80 ms 8516 KB n=3
4 Correct 151 ms 10440 KB n=4
5 Correct 95 ms 8364 KB n=4
6 Correct 88 ms 8308 KB n=2
7 Correct 141 ms 10364 KB n=5
8 Correct 80 ms 8584 KB n=8
9 Correct 146 ms 10476 KB n=14
10 Correct 142 ms 10428 KB n=11
11 Correct 710 ms 20552 KB n=50000
12 Correct 629 ms 19288 KB n=50000
13 Correct 146 ms 10432 KB n=10
14 Correct 172 ms 10704 KB n=685
15 Correct 176 ms 10668 KB n=623
16 Correct 207 ms 11776 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 10432 KB n=4
2 Correct 151 ms 10508 KB n=3
3 Correct 80 ms 8516 KB n=3
4 Correct 151 ms 10440 KB n=4
5 Correct 95 ms 8364 KB n=4
6 Correct 88 ms 8308 KB n=2
7 Correct 141 ms 10364 KB n=5
8 Correct 80 ms 8584 KB n=8
9 Correct 146 ms 10476 KB n=14
10 Correct 142 ms 10428 KB n=11
11 Correct 710 ms 20552 KB n=50000
12 Correct 629 ms 19288 KB n=50000
13 Correct 146 ms 10432 KB n=10
14 Correct 172 ms 10704 KB n=685
15 Correct 176 ms 10668 KB n=623
16 Correct 207 ms 11776 KB n=973
17 Correct 256 ms 11692 KB n=989
18 Correct 249 ms 11968 KB n=563
19 Correct 307 ms 13052 KB n=592
20 Correct 355 ms 13192 KB n=938
21 Correct 316 ms 12136 KB n=747
22 Correct 378 ms 12984 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 1450 ms 132344 KB n=1000000
2 Correct 1267 ms 80832 KB n=666666
3 Correct 954 ms 51604 KB n=400000
4 Correct 915 ms 46548 KB n=285714
5 Correct 600 ms 16796 KB n=20000
6 Correct 808 ms 31808 KB n=181818
7 Correct 504 ms 15268 KB n=10000
8 Correct 463 ms 15160 KB n=6666
9 Correct 347 ms 15172 KB n=4000
10 Correct 427 ms 14864 KB n=2857
11 Correct 279 ms 13588 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 10432 KB n=4
2 Correct 151 ms 10508 KB n=3
3 Correct 80 ms 8516 KB n=3
4 Correct 151 ms 10440 KB n=4
5 Correct 95 ms 8364 KB n=4
6 Correct 88 ms 8308 KB n=2
7 Correct 141 ms 10364 KB n=5
8 Correct 80 ms 8584 KB n=8
9 Correct 146 ms 10476 KB n=14
10 Correct 142 ms 10428 KB n=11
11 Correct 710 ms 20552 KB n=50000
12 Correct 629 ms 19288 KB n=50000
13 Correct 146 ms 10432 KB n=10
14 Correct 172 ms 10704 KB n=685
15 Correct 176 ms 10668 KB n=623
16 Correct 207 ms 11776 KB n=973
17 Correct 256 ms 11692 KB n=989
18 Correct 249 ms 11968 KB n=563
19 Correct 307 ms 13052 KB n=592
20 Correct 355 ms 13192 KB n=938
21 Correct 316 ms 12136 KB n=747
22 Correct 378 ms 12984 KB n=991
23 Correct 1450 ms 132344 KB n=1000000
24 Correct 1267 ms 80832 KB n=666666
25 Correct 954 ms 51604 KB n=400000
26 Correct 915 ms 46548 KB n=285714
27 Correct 600 ms 16796 KB n=20000
28 Correct 808 ms 31808 KB n=181818
29 Correct 504 ms 15268 KB n=10000
30 Correct 463 ms 15160 KB n=6666
31 Correct 347 ms 15172 KB n=4000
32 Correct 427 ms 14864 KB n=2857
33 Correct 279 ms 13588 KB n=2000
34 Correct 1174 ms 18560 KB n=23514
35 Correct 1244 ms 18640 KB n=23514
36 Correct 174 ms 10592 KB n=940
37 Correct 163 ms 10472 KB n=2
38 Correct 1708 ms 32780 KB n=100000
39 Correct 1539 ms 32768 KB n=100000
40 Correct 139 ms 10508 KB n=10
41 Correct 237 ms 11788 KB n=100
42 Correct 445 ms 17348 KB n=1000
43 Correct 1468 ms 158248 KB n=1000000
44 Execution timed out 2086 ms 78308 KB Time limit exceeded
45 Halted 0 ms 0 KB -