Submission #345710

# Submission time Handle Problem Language Result Execution time Memory
345710 2021-01-08T00:58:40 Z KWang31 Gift (IZhO18_nicegift) Java 11
49 / 100
2000 ms 151916 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 cur=0; 
        for (int i = 0; i < N; i++) {
            a[i]=new Pair(1+i,br.nextLong()); cur+=a[i].val; 
        }
        
        //A number is bottle neck if a[i]
        Arrays.sort(a);
        if(cur%K>0 || cur/K < a[N-1].val){
            System.out.println("-1");return;
        }
        cur/=K;
 
        //Tracks K+1 elements
        long d;
        long[] ans=new long[3000000]; int pt=0;
        int lp=0; int rp=N-1; 
        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;
            
            //System.out.println(j+" "+cur+" "+d);
            ans[pt++]=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
            	a[k]=new Pair(a[k].ind,a[k].val-d); ans[pt++]=a[k].ind;
            	
            }
            for(int k=rp+1; k<N; k++) {
            	a[k]=new Pair(a[k].ind,a[k].val-d); ans[pt++]=a[k].ind;
            }
            cur-=d;
            
            
        }
        //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;
                    
                    ans[pt++]=a[i].val;
                }
                ans[pt++]=a[i].ind;
            }
        }
        System.out.println(pt/(K+1));
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < pt/(K+1); i++) {
            for (int j = 0; j <=K; j++) {
                sb.append(ans[i*(K+1)+j]+" ");
            }
            sb.append("\n");
            if(i*(K+1)%100000 < (i-1)*(K+1)%100000){
                System.out.println(sb.toString()); sb=new StringBuilder();
            }
        }
        System.out.println(sb.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!
# Verdict Execution time Memory Grader output
1 Correct 128 ms 33792 KB n=4
2 Correct 131 ms 33804 KB n=3
3 Correct 73 ms 8452 KB n=3
4 Correct 137 ms 33760 KB n=4
5 Correct 88 ms 8556 KB n=4
6 Correct 85 ms 8556 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 128 ms 33792 KB n=4
2 Correct 131 ms 33804 KB n=3
3 Correct 73 ms 8452 KB n=3
4 Correct 137 ms 33760 KB n=4
5 Correct 88 ms 8556 KB n=4
6 Correct 85 ms 8556 KB n=2
7 Correct 147 ms 33796 KB n=5
8 Correct 81 ms 8788 KB n=8
9 Correct 139 ms 33792 KB n=14
10 Correct 135 ms 33900 KB n=11
11 Correct 594 ms 42868 KB n=50000
12 Correct 569 ms 42336 KB n=50000
13 Correct 158 ms 33720 KB n=10
14 Correct 159 ms 34028 KB n=685
15 Correct 156 ms 34028 KB n=623
16 Correct 204 ms 35316 KB n=973
# Verdict Execution time Memory Grader output
1 Correct 128 ms 33792 KB n=4
2 Correct 131 ms 33804 KB n=3
3 Correct 73 ms 8452 KB n=3
4 Correct 137 ms 33760 KB n=4
5 Correct 88 ms 8556 KB n=4
6 Correct 85 ms 8556 KB n=2
7 Correct 147 ms 33796 KB n=5
8 Correct 81 ms 8788 KB n=8
9 Correct 139 ms 33792 KB n=14
10 Correct 135 ms 33900 KB n=11
11 Correct 594 ms 42868 KB n=50000
12 Correct 569 ms 42336 KB n=50000
13 Correct 158 ms 33720 KB n=10
14 Correct 159 ms 34028 KB n=685
15 Correct 156 ms 34028 KB n=623
16 Correct 204 ms 35316 KB n=973
17 Correct 229 ms 35504 KB n=989
18 Correct 236 ms 35684 KB n=563
19 Correct 302 ms 36188 KB n=592
20 Correct 315 ms 35988 KB n=938
21 Correct 262 ms 35820 KB n=747
22 Correct 330 ms 36220 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 1250 ms 133616 KB n=1000000
2 Correct 970 ms 109128 KB n=666666
3 Correct 801 ms 79632 KB n=400000
4 Correct 756 ms 66176 KB n=285714
5 Correct 600 ms 40020 KB n=20000
6 Correct 702 ms 57896 KB n=181818
7 Correct 556 ms 39004 KB n=10000
8 Correct 420 ms 38340 KB n=6666
9 Correct 381 ms 37828 KB n=4000
10 Correct 521 ms 39232 KB n=2857
11 Correct 279 ms 37256 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 128 ms 33792 KB n=4
2 Correct 131 ms 33804 KB n=3
3 Correct 73 ms 8452 KB n=3
4 Correct 137 ms 33760 KB n=4
5 Correct 88 ms 8556 KB n=4
6 Correct 85 ms 8556 KB n=2
7 Correct 147 ms 33796 KB n=5
8 Correct 81 ms 8788 KB n=8
9 Correct 139 ms 33792 KB n=14
10 Correct 135 ms 33900 KB n=11
11 Correct 594 ms 42868 KB n=50000
12 Correct 569 ms 42336 KB n=50000
13 Correct 158 ms 33720 KB n=10
14 Correct 159 ms 34028 KB n=685
15 Correct 156 ms 34028 KB n=623
16 Correct 204 ms 35316 KB n=973
17 Correct 229 ms 35504 KB n=989
18 Correct 236 ms 35684 KB n=563
19 Correct 302 ms 36188 KB n=592
20 Correct 315 ms 35988 KB n=938
21 Correct 262 ms 35820 KB n=747
22 Correct 330 ms 36220 KB n=991
23 Correct 1250 ms 133616 KB n=1000000
24 Correct 970 ms 109128 KB n=666666
25 Correct 801 ms 79632 KB n=400000
26 Correct 756 ms 66176 KB n=285714
27 Correct 600 ms 40020 KB n=20000
28 Correct 702 ms 57896 KB n=181818
29 Correct 556 ms 39004 KB n=10000
30 Correct 420 ms 38340 KB n=6666
31 Correct 381 ms 37828 KB n=4000
32 Correct 521 ms 39232 KB n=2857
33 Correct 279 ms 37256 KB n=2000
34 Correct 1037 ms 42024 KB n=23514
35 Correct 1239 ms 41472 KB n=23514
36 Correct 195 ms 33868 KB n=940
37 Correct 141 ms 33900 KB n=2
38 Correct 1672 ms 65680 KB n=100000
39 Correct 1525 ms 65720 KB n=100000
40 Correct 140 ms 33764 KB n=10
41 Correct 232 ms 35560 KB n=100
42 Correct 415 ms 38008 KB n=1000
43 Correct 1330 ms 151916 KB n=1000000
44 Execution timed out 2080 ms 78036 KB Time limit exceeded
45 Halted 0 ms 0 KB -