Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.*;
  4.  
  5. public class Solution {
  6.  public static void bubble_srt( int a[],int b[], int n ){
  7.     int i, j,t=0;
  8.     for(i = 0; i < n; i++){
  9.       for(j = 1; j < (n-i); j++){
  10.         if(a[j-1] > a[j]  && (Math.abs(b[j-1]-b[j])>a[j-1] || Math.abs(b[j-1]-b[j])>a[j])){
  11.           t = a[j-1];
  12.           a[j-1]=a[j];
  13.           a[j]=t;
  14.           t = b[j-1];
  15.           b[j-1]=b[j];
  16.           b[j]=t;
  17.         }
  18.       }
  19.     }
  20.   }
  21.     public static void main(String[] args) {
  22.        try {
  23.         FileWriter oos1 = new FileWriter("output.txt");
  24.         File inTxt=new File("input.txt");
  25.         Scanner kbd = new Scanner(inTxt);
  26.         int number=Integer.parseInt(kbd.nextLine());
  27.         int[] distance=new int[number];
  28.         int[] time=new int[number];
  29.         int i=0;
  30.         while(kbd.hasNextLine())  {
  31.             StringTokenizer tokenizer=
  32.                     new StringTokenizer(kbd.nextLine());
  33.             distance[i]=Integer.parseInt(tokenizer.nextToken());
  34.             time[i]=Integer.parseInt(tokenizer.nextToken());
  35.             i++;
  36.         }
  37.      
  38.         for(int i1=0;i1<number;i1++)
  39.         {
  40.             for(int j1=0;j1<number;j1++)
  41.             {
  42.                 if(Math.abs(distance[j1]-distance[i1])>time[i1] && Math.abs(distance[i1]-distance[j1])>time[j1])
  43.                 {
  44.                 oos1.write("No solution");
  45.                 oos1.close();
  46.                 return;
  47.                 }
  48.             }
  49.         }
  50.         int sum1=0;
  51.         int sum2=0;
  52.         for(int i1=0;i1<number;i1++)
  53.             sum1+=time[i1];
  54.         for(int i1=0;i1<number;i1++)
  55.             sum2+=distance[i1];
  56.         if(sum2>=sum1)
  57.             {
  58.                 oos1.write("No solution");
  59.                 oos1.close();
  60.                 return;
  61.                 }
  62.         int[][] ar=new int[number][number];
  63.         bubble_srt(time,distance, number);
  64.          for(i = 0; i <distance.length; i++)
  65.       System.out.print(distance[i]+"  ");
  66.         for(int i1=0;i1<number;i1++)
  67.         {
  68.            ar[i1][i1]=0;
  69.         }
  70.  
  71.         for(int i1=0;i1<number-1;i1++)
  72.         {
  73.            ar[i1][i1+1]=Math.abs(distance[i1+1]-distance[i1]);
  74.            System.out.println("this"+ar[i1][i1+1]);
  75.         }
  76.         for(int n=2;n<number;n++)
  77.         for(int i1=0;i1<number-n;i1++)
  78.             ar[i1][i1+n]=ar[i1][i1+1]+ar[i1+1][i1+n];
  79.  
  80.         oos1.write(new Integer(ar[0][number-1]).toString());
  81.         oos1.close();
  82.         }
  83.        catch (FileNotFoundException ex) {}
  84.       catch(IOException ex) {}
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement