quinamatics's blog

By quinamatics, history, 7 years ago, In English

Hello, I am getting WA on test case 4. I followed the editorial quite closely, so I think the issue might lie in not properly taking the modulus. Here is my WA code: Please tell me the error in the code.

import java.util.Arrays; import java.util.HashMap; import java.util.Scanner;

public class DuffInBeach { public static void main(String[] args) {

Scanner s = new Scanner(System.in);
String str = s.nextLine();
String[] strarr = str.split(" ");
int N = Integer.parseInt(strarr[0]);
double L = Double.parseDouble(strarr[1]);
int K = Integer.parseInt(strarr[2]);

long[][] dp = new long[N][K+1];
long[] a = new long[N];
str = s.nextLine();
strarr = str.split(" ");
HashMap hmap = new HashMap();
long[] b = new long[N];
//Index of sortings, a[p[i]]<= a[p[i+1]];
for(int i = 0; i < N; i++){
   a[i] = Long.parseLong(strarr[i]);
   b[i] = a[i];
   hmap.put(a[i], i);
}
Arrays.sort(b);
int[] p = new int[N];
for(int i = 0; i < N; i++)
   p[i] = (int) hmap.get(b[i]);
//# of permutations from Block 1 to Block J, for each pos I. 
for(int i = 0; i < dp.length; i++)
   dp[i][1] = 1;

for(int j = 2; j <= K; j++){
   for(int i = 0; i < N; i++){
     int ptr = 0;
     while(ptr < N && a[p[ptr]]<= a[i])
      dp[i][j] = (int) ((dp[i][j]+dp[p[ptr++]][j-1])%(1e9+7));
   }
}
Integer denom = new Integer(N);
long c = (long) Math.ceil(L/denom.doubleValue());
long min = Math.min(c, K);
int x = (int) ((L-1)%(N));

long ans = 0;
for(int i = 0; i <= x; i++){
   for(int j = 1; j <= min; j++){
     long sum = (long) ((dp[i][j]*(c-j+1))%(1e9+7));
     ans = (long) ((ans+sum)%(1e9+7));
   }
}
for(int i = x+1; i < N; i++){
   for(int j = 1; j <= Math.min(c-1,K); j++){
     long sum = (long)((dp[i][j]*(c-j)%(1e9+7)));
     ans = (long)((ans+sum)%(1e9+7));
   }
}
System.out.println(ans);

} }

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By quinamatics, history, 7 years ago, In English

Hello, I am working on the problem Duff In Beach for Codeforces Div 1 Round 326 getting WA on test case 4. I followed the editorial quite closely, so I think the issue might lie in not properly taking the modulus. I really need help on this problem have been stuck for Weeks!

Here is my WA code:

import java.util.Arrays; import java.util.HashMap; import java.util.Scanner;

public class DuffInBeach { public static void main(String[] args) {

Scanner s = new Scanner(System.in);
String str = s.nextLine();
String[] strarr = str.split(" ");
int N = Integer.parseInt(strarr[0]);
double L = Double.parseDouble(strarr[1]);
int K = Integer.parseInt(strarr[2]);

long[][] dp = new long[N][K+1];
long[] a = new long[N];
str = s.nextLine();
strarr = str.split(" ");
HashMap hmap = new HashMap();
long[] b = new long[N];
//Index of sortings, a[p[i]]<= a[p[i+1]];
for(int i = 0; i < N; i++){
   a[i] = Long.parseLong(strarr[i]);
   b[i] = a[i];
   hmap.put(a[i], i);
}
Arrays.sort(b);
int[] p = new int[N];
for(int i = 0; i < N; i++)
   p[i] = (int) hmap.get(b[i]);
//# of permutations from Block 1 to Block J, for each pos I. 
for(int i = 0; i < dp.length; i++)
   dp[i][1] = 1;

for(int j = 2; j <= K; j++){
   for(int i = 0; i < N; i++){
     int ptr = 0;
     while(ptr < N && a[p[ptr]]<= a[i])
      dp[i][j] = (int) ((dp[i][j]+dp[p[ptr++]][j-1])%(1e9+7));
   }
}
Integer denom = new Integer(N);
long c = (long) Math.ceil(L/denom.doubleValue());
long min = Math.min(c, K);
int x = (int) ((L-1)%(N));

long ans = 0;
for(int i = 0; i <= x; i++){
   for(int j = 1; j <= min; j++){
     long sum = (long) ((dp[i][j]*(c-j+1))%(1e9+7));
     ans = (long) ((ans+sum)%(1e9+7));
   }
}
for(int i = x+1; i < N; i++){
   for(int j = 1; j <= Math.min(c-1,K); j++){
     long sum = (long)((dp[i][j]*(c-j)%(1e9+7)));
     ans = (long)((ans+sum)%(1e9+7));
   }
}
System.out.println(ans);

} }

Full text and comments »

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

By quinamatics, history, 7 years ago, In English

Hello, I am getting WA on test case 4. I followed the editorial quite closely, so I think the issue might lie in not properly taking the modulus. Here is my WA code: import java.util.Arrays; import java.util.HashMap; import java.util.Scanner;

public class DuffInBeach {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    String str = s.nextLine();
    String[] strarr = str.split(" ");
    int N = Integer.parseInt(strarr[0]);
    double L = Double.parseDouble(strarr[1]);
    int K = Integer.parseInt(strarr[2]);

    long[][] dp = new long[N][K+1];
    long[] a = new long[N];
    str = s.nextLine();
    strarr = str.split(" ");
    HashMap hmap = new HashMap();
    long[] b = new long[N];
    //Index of sortings, a[p[i]]<= a[p[i+1]];
    for(int i = 0; i < N; i++){
       a[i] = Long.parseLong(strarr[i]);
       b[i] = a[i];
       hmap.put(a[i], i);
    }
    Arrays.sort(b);
    int[] p = new int[N];
    for(int i = 0; i < N; i++)
       p[i] = (int) hmap.get(b[i]);
    //# of permutations from Block 1 to Block J, for each pos I. 
    for(int i = 0; i < dp.length; i++)
       dp[i][1] = 1;

    for(int j = 2; j <= K; j++){
       for(int i = 0; i < N; i++){
         int ptr = 0;
         while(ptr < N && a[p[ptr]]<= a[i])
          dp[i][j] = (int) ((dp[i][j]+dp[p[ptr++]][j-1])%(1e9+7));
       }
    }
    Integer denom = new Integer(N);
    long c = (long) Math.ceil(L/denom.doubleValue());
    long min = Math.min(c, K);
    int x = (int) ((L-1)%(N));

    long ans = 0;
    for(int i = 0; i <= x; i++){
       for(int j = 1; j <= min; j++){
         long sum = (long) ((dp[i][j]*(c-j+1))%(1e9+7));
         ans = (long) ((ans+sum)%(1e9+7));
       }
    }
    for(int i = x+1; i < N; i++){
       for(int j = 1; j <= Math.min(c-1,K); j++){
         long sum = (long)((dp[i][j]*(c-j)%(1e9+7)));
         ans = (long)((ans+sum)%(1e9+7));
       }
    }
    System.out.println(ans);
}

}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By quinamatics, history, 7 years ago, In English

why the fucking fuck am I getting runtime error on test cases 26 and 29? code is below

import java.util.LinkedList; import java.util.Scanner;

public class Strip { public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine(); String[] strarr = str.split(" ");

    int n = Integer.parseInt(strarr[0]);
    int s = Integer.parseInt(strarr[1]);
    int l = Integer.parseInt(strarr[2]);


    LinkedList<Pair> dmax = new LinkedList<Pair>();
    LinkedList<Pair> dmin = new LinkedList<Pair>();
    int[] arr = new int[n];
    int[] f = new int[n]; //least # of cuts needed
    int[] g = new int[n];//leftmost index of right strip
    int tail = 0;

    g[0] = 0;  
    for(int i = 0; i < l; ++i){
       f[i] = 69696969;
    }
    str = sc.nextLine();strarr = str.split(" ");
    for(int i = 0; i < strarr.length; i++){
       arr[i] = Integer.parseInt(strarr[i]);
    }




    dmin.add(new Pair(arr[0],0));
    dmax.add(new Pair(arr[0],0));
    //Monotonic Queue
    for(int i = 1; i < n; i++){
       while(!dmax.isEmpty()&&(arr[i]>=dmax.getLast().val))
         dmax.pollLast();
       dmax.add(new Pair(arr[i],i));

       while(!dmin.isEmpty()&&(arr[i]<=dmin.getLast().val))
         dmin.pollLast();
       dmin.add(new Pair(arr[i],i));

       while(!dmax.isEmpty() && !dmin.isEmpty()&&(dmax.getFirst().val -dmin.getFirst().val > s)){
         ++tail;
         if(dmax.getFirst().pos < tail)
          dmax.poll();
         if(dmin.getFirst().pos<tail)
          dmin.poll();
       }
       g[i] = tail;
    }



    dmin = new LinkedList<Pair>();
    dmin.add(new Pair(0,-1));
    if(g[l-1]==0)
       f[l-1]=1;
    else{
       System.out.println(-1);
       return;
    }

    for(int i = l; i < n; i++){
       f[i]= 69696969;
       while(!dmin.isEmpty() && f[i-l] <= dmin.getLast().val)
         dmin.pollLast();

       dmin.add(new Pair(f[i-l],i-l));

       while(!dmin.isEmpty() && dmin.getFirst().pos < g[i]-1)
         dmin.poll();
       if(!dmin.isEmpty())
         f[i] = dmin.getFirst().val+1;
    }
    if(f[n-1] < 69696969)  
       System.out.println(f[n-1]);
    else
       System.out.println(-1);
}

static class Pair
{
    int val;
    int pos;
    public Pair(int val, int pos)
    {
        this.val = val;
        this.pos = pos;
    }

}

}

Full text and comments »

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

By quinamatics, history, 7 years ago, In English

Hi, what is wrong with this code. I know that test case 4: 70,3326631213 is wrong, but I can't find the optimal solution.

import java.util.Arrays; import java.util.Scanner;

public class D2417B {

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    s.nextLine();
    String str = s.nextLine();
    int[] arr = new int[str.length()];
    char[] ch = str.toCharArray();
    int sum =0;
    for(int i = 0;i < ch.length; i++){
       arr[i] = Character.getNumericValue(ch[i]);
       sum += arr[i];
    }

    Arrays.sort(arr);
    int fin = 0;
    if(sum >= k)
       System.out.println(0);
    else{
       sum = k-sum;
       for(int i = 0; i < arr.length; i++){
         if(sum + arr[i] <= 9){
          fin += Math.min(sum,10-sum);
          sum =0;
          break;
         }
         else{
          sum += arr[i];
          sum -= 9;
          fin += Math.min(9-arr[i],arr[i]+1);
         }
       }
       System.out.println(fin);
    }
}

}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By quinamatics, history, 7 years ago, In English

I have a wrong answer on test case 12, but I have no idea what is wrong. Here is my code:

import java.util.Arrays; import java.util.Scanner;

public class D2416C {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    s.nextLine();
    String lin = s.nextLine();
    int[] arr = new int[n];
    int[] st = new int[5001];
    int[] nd = new int[5001];
    String[] linarr = lin.split(" ");
    Arrays.fill(st, -1);
    Arrays.fill(nd, -2);
    for(int i = 0; i < n; i++){
       arr[i] = Integer.parseInt(linarr[i]);
       if(st[arr[i]]==-1)
         st[arr[i]] = i;
    }
    for(int j = n-1;j >= 0; j--)
       if(nd[arr[j]]==-2)
         nd[arr[j]]=j;
    for(int i = 1; i < arr.length; i++)
       arr[i]= arr[i]^arr[i-1];

    int[] max = new int[n];

    max[0] = 0;
    for(int i = 0; i < nd.length; i++) 
       if(nd[i] == 0)
         max[0] = i;

    for(int i = 1; i < n; i++){
       int k = 0;
       for(int j = 0; j < nd.length; j++){
         if(nd[j]==i){
          k = j;
          break;
         }       
       }
       if(k != 0){
         if(st[k] == 0)
          max[i] = Math.max(max[i-1],arr[st[k]]);
         else
          if(st[k] == nd[k])
              max[i] = max[i-1]+k;
         else{
           if(st[k]== 1)
            max[i] = Math.max(max[i-1],(arr[nd[k]-1]^arr[st[k]-1]));
           else{
            int qw = arr[nd[k]-1]^arr[st[k]-1];
            max[i] = Math.max(max[i-1],qw+max[st[k]-2]);
           }
         }
       }
       else
         max[i] = max[i-1];
    }

    System.out.println(max[n-1]);

}

}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By quinamatics, history, 7 years ago, In English

I am trying Problem C on Round 338 Div2, Running Track. However my code receives TLE on Test Cast 16. I have had these issues in the past and would like details on if my solution can be optimized or if I need to switch to a faster method.

Here is my code below.

import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner;

public class D2615C { public static void main(String[] args) {

  Scanner s = new Scanner(System.in);
  int inc = 0;
  String[] sol = new String[100000];
  String str = s.nextLine();
  String tgt = s.nextLine();
  String[] item = new String[tgt.length()+1];
  int[] index = new int[tgt.length()+1];
  Arrays.fill(index,Integer.MAX_VALUE);

  int[] dp = new int[tgt.length()+1];
  dp[0] = 0;



  for(int i = 1; i < dp.length; i++){



      String reverse = "";
      int min = 99999;
      int prevMin = 99999;

      for(int j = 0; j < i; j++){
        String rev = ""; 


        if(dp[j] == -1)
          continue;

        String seg = tgt.substring(j,i);
        for(int k = 0; k < seg.length(); k++)
          rev += seg.charAt(seg.length()-k-1);


         if(str.contains(tgt.substring(j,i))){
           min = Math.min(min, dp[j]+1);    
           if(prevMin > min){
            prevMin = min;
            index[i] = j;
            item[i] = (str.indexOf(tgt.substring(j,i))+1)+ " "+ (str.indexOf(tgt.substring(j,i))+(i-j));
           }
         }
         else if(str.contains(rev)){
           min = Math.min(min, dp[j]+1);
           if(prevMin > min){
            prevMin = min;
            index[i] = j;
            item[i] = (str.indexOf(rev)+(i-j))+" "+(str.indexOf(rev)+1);
           }
         }       

      }
      if(min == 99999)
         dp[i] = -1; 
      else
         dp[i] = min;
  } 
  System.out.println(dp[tgt.length()]);
  if(dp[tgt.length()]!= -1){
      int c = tgt.length();
      while( c!= 0){
         sol[inc++]=item[c];
         c = index[c];
      }
      for(int i = inc-1; i >= 0; i--){
         System.out.println(sol[i]);
     }

  }

}

}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it