Submission #344031

# Submission time Handle Problem Language Result Execution time Memory
344031 2021-01-05T04:55:24 Z KWang31 Gift (IZhO18_nicegift) Java 11
Compilation error
0 ms 0 KB
import java.io.*; import java.util.*;
public class gift{
  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()); 
        } 
  
        
    } 
    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();
        long[] a=new long[N];
        long sum=0; long max=0;
        for (int i = 0; i < N; i++) {
            a[i]=br.nextInt(); sum+=a[i]; max=Math.max(max,a[i]);
        }
        if(sum%K>0 || sum/K < max){
            System.out.println("-1");return;
        }
        //A number is bottle neck if a[i]
        
        long cur=sum/K;
        
        
        PriorityQueue<Pair> pq=new PriorityQueue<>();
        //Tracks K+1 elements
        long d;StringBuilder ans=new StringBuilder();
        int op=0;
        for (int j = 0; j < K; j++) {
            pq=new PriorityQueue<>();
            for (int i = 0; i < N; i++) {
                if(pq.size()<=K){
                    pq.add(new Pair(i,a[i]));
                }else if(pq.peek().val < a[i]){
                    pq.poll(); pq.add(new Pair(i,a[i]));
                }
            }
            d=cur-pq.poll().val;
            if(d==0)continue;
            op++;
            ans.append(d+" ");
            for (Pair p: pq) {
                ans.append((p.ind+1)+" ");
                a[p.ind]-=d;
            }
            ans.append("\n");
            cur-=d;
        }
        //Now K of them are "bottlenecks"
        ans.append("\n");
        boolean val=false;
        for (int i = 0; i < N; i++) {
            if(a[i]>0){
                if(!val){
                    val=true;
                    op++;
                    ans.append(a[i]+" ");
                }
                ans.append((i+1)+" ");
            }
        }
        System.out.println(op);
        System.out.println(ans.toString());
    }
    
    public static int find(int x, int[] p){
        if(p[x]==x)return x;
        int ans=find(p[x],p); p[x]=ans; return ans;
    }
    
    public static void merge(int a, int b) {
        if(rk[a]<rk[b]) {
            p[a]=b; siz[b]+=siz[a];
        }else if(rk[a]==rk[b]) {
            p[a]=b; rk[b]++;siz[b]+=siz[a];
        }else {
            p[b]=a; siz[a]+=siz[b];
        }
    }
    public static long pow(int b, int e){
        if(e==0)return 1; if(e==1)return b;
        long x=pow(b,e/2); x*=x; x%=MOD; x*=pow(b,e%2); return x%MOD;
    }
  public static void dfs(ArrayList<Integer>[] arl, int cur){
       
        for (int i : arl[cur]) {
            
            
            
        }
        
    }
}
//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!

Compilation message

nicegift.java:2: error: class gift is public, should be declared in a file named gift.java
public class gift{
       ^
1 error