Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.lang.*;
- public class Solution {
- public static void bubble_srt( int a[],int b[], int n ){
- int i, j,t=0;
- for(i = 0; i < n; i++){
- for(j = 1; j < (n-i); j++){
- 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])){
- t = a[j-1];
- a[j-1]=a[j];
- a[j]=t;
- t = b[j-1];
- b[j-1]=b[j];
- b[j]=t;
- }
- }
- }
- }
- public static void main(String[] args) {
- try {
- FileWriter oos1 = new FileWriter("output.txt");
- File inTxt=new File("input.txt");
- Scanner kbd = new Scanner(inTxt);
- int number=Integer.parseInt(kbd.nextLine());
- int[] distance=new int[number];
- int[] time=new int[number];
- int i=0;
- while(kbd.hasNextLine()) {
- StringTokenizer tokenizer=
- new StringTokenizer(kbd.nextLine());
- distance[i]=Integer.parseInt(tokenizer.nextToken());
- time[i]=Integer.parseInt(tokenizer.nextToken());
- i++;
- }
- for(int i1=0;i1<number;i1++)
- {
- for(int j1=0;j1<number;j1++)
- {
- if(Math.abs(distance[j1]-distance[i1])>time[i1] && Math.abs(distance[i1]-distance[j1])>time[j1])
- {
- oos1.write("No solution");
- oos1.close();
- return;
- }
- }
- }
- int sum1=0;
- int sum2=0;
- for(int i1=0;i1<number;i1++)
- sum1+=time[i1];
- for(int i1=0;i1<number;i1++)
- sum2+=distance[i1];
- if(sum2>=sum1)
- {
- oos1.write("No solution");
- oos1.close();
- return;
- }
- int[][] ar=new int[number][number];
- bubble_srt(time,distance, number);
- for(i = 0; i <distance.length; i++)
- System.out.print(distance[i]+" ");
- for(int i1=0;i1<number;i1++)
- {
- ar[i1][i1]=0;
- }
- for(int i1=0;i1<number-1;i1++)
- {
- ar[i1][i1+1]=Math.abs(distance[i1+1]-distance[i1]);
- System.out.println("this"+ar[i1][i1+1]);
- }
- for(int n=2;n<number;n++)
- for(int i1=0;i1<number-n;i1++)
- ar[i1][i1+n]=ar[i1][i1+1]+ar[i1+1][i1+n];
- oos1.write(new Integer(ar[0][number-1]).toString());
- oos1.close();
- }
- catch (FileNotFoundException ex) {}
- catch(IOException ex) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement