back_to_match's blog

By back_to_match, history, 3 years ago, In English

Educational Codeforces Round 114 (Rated for Div. 2) Educational Codeforces Round 114 (Rated for Div. 2) question c

https://codeforces.com/contest/1574/problem/C

import java.util.*; import java.io.*; public class A124 { public static void process()throws IOException { int n=I(); TreeSet set=new TreeSet(); long[] a=Al(n);long sum=0; for(int i=0;i<n;i++) { set.add(a[i]); sum+=a[i]; }

    int m=I();
    for(int i=0;i<m;i++)
    {
       long xi=L();long yi=L();
       if(set.first()>xi)
       {
         if((long)(sum-set.first())>=yi)
            {
              pn((long)0);
            }
            else
            {
              pn((long)(yi-(sum-set.first())));
            }
       }
       else
       if(set.last()<xi)
       {
         long sum1=(long)Math.abs(xi-set.last());
         long s=sum-set.last();
         if(s>=yi)
            {
              pn((long)sum1);
            }
            else
            {
              pn((long)((yi-s)+sum1));
            }

       }
       else
       {
         long sum1=0;long sum2=0;

         sum1=(long)Math.abs(xi-set.floor(xi));
         sum2=(long)0;
         if((long)(sum-set.floor(xi))<yi)
         {
             sum1+=(long)(yi-(sum-set.floor(xi)));
         }
         if((long)(sum-set.ceiling(xi))<yi)
         {
          sum2+=(long)(yi-(sum-set.ceiling(xi)));
         }
         pn((long)Math.min(sum1, sum2));
       }
    }
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static void pn(Object o){out.println(o);out.flush();}
static void p(Object o){out.print(o);out.flush();}
static void pni(Object o){out.println(o);System.out.flush();}
static int I() throws IOException{return sc.nextInt();}
static long L() throws IOException{return sc.nextLong();}
static double D() throws IOException{return sc.nextDouble();}
static String S() throws IOException{return sc.next();}
static char C() throws IOException{return sc.next().charAt(0);}
static int[] Ai(int n) throws IOException{int[] arr = new int[n];for (int i = 0; i < n; i++)arr[i] = I();return arr;}
static String[] As(int n) throws IOException{String s[] = new String[n];for (int i = 0; i < n; i++)s[i] = S();return s;}
static long[] Al(int n) throws IOException {long[] arr = new long[n];for (int i = 0; i < n; i++)arr[i] = L();return arr;}
static void dyn(int dp[][],int n,int m,int z)throws IOException {for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ dp[i][j]=z;}} }

// --------------------------------------------------------------------------------------------------------------------------------// static class AnotherReader {BufferedReader br;StringTokenizer st;AnotherReader() throws FileNotFoundException {br = new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a) throws FileNotFoundException {br = new BufferedReader(new FileReader("input.txt"));} String next() throws IOException{while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}}return st.nextToken();} int nextInt() throws IOException {return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble() throws IOException {return Double.parseDouble(next());} String nextLine() throws IOException {String str = "";try {str = br.readLine();} catch (IOException e) {e.printStackTrace();}return str;}} public static void main(String[] args)throws IOException{try{boolean oj=true;if(oj==true) {AnotherReader sk=new AnotherReader();PrintWriter out=new PrintWriter(System.out);} else {AnotherReader sk=new AnotherReader(100);out=new PrintWriter("output.txt");}

{process();}out.flush();out.close();}catch(Exception e){return;}}}

//*-----------------------------------------------------------------------------------------------------------------------------------*//

  • Vote: I like it
  • -20
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +7 Vote: I do not like it

you know that you can link submissions here?

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

If you remove flush() from pn() it gets AC.

flush() is expensive (it writes the buffered data to disk which is slow). Now, you're using PrintWriter which disables automatic line flushing so it should be fast. But after each write you call flush(), negating the benefit of the disable of automatic line flushing, and end up with terrible performance because of IO.