fares's blog

By fares, history, 8 years ago, In English

Hi: I was solving this problem http://codeforces.com/problemset/problem/451/B

my idea in brief is to sort the input and repeatedly check if an entry has been moved(call it a[i]) in the sort ,where it is know and which entry it was exchanged with(call it a[j] )and check every entry between a[i] and a[j] to double sure that if it is a segment that was reversed , lastly check the number of the reversed segments to print the answer.

Here is the code:


import java.util.*; public class Main { public static void main (String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int unsort[]=new int[n]; int sort[]=new int[n]; for(int i=0;i<unsort.length;i++){ unsort[i]=sc.nextInt(); sort[i]=unsort[i]; } Arrays.sort(sort); int co=0;int counter=0,id=0,id2=1; for(int i=0;i<unsort.length;i++){ if(unsort[i]!=sort[i]){ co++; int in=Arrays.binarySearch(sort,unsort[i]); if(sort[i]==unsort[in]&&sort[i+1]==unsort[in-1]){ int i2=i;int in2=in;boolean b=false; while(i2++<=in2--){ if(sort[i2]!=unsort[in2]){b=true;break;} } if(b==false){ ++counter;id=i;id2=in; i=in;} } } } ++id;++id2; if(co==0){id=1;id2=1;} System.out.println((counter==1||co==0)?"yes\n"+id+" "+id2:"no"); } } Edit:changed the blog as you said :)

Full text and comments »

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

By fares, history, 8 years ago, In English

hi guys: i solved this problem : http://codeforces.com/problemset/problem/58/A

then i saw a code that an orange user used to solve it , here is the code: import java.util.Scanner; public class Main{

     public static void main(String[] args) throws Exception {
         Scanner in = new Scanner(System.in);

         String[] str = in.nextLine().split(".*h.*e.*l.*l.*o.*");
         System.out.println(str.length == 0 ? "YES" : "NO");

     }

}

i understand that the split method splits the String when it finds the String parameter given to it , then it puts the substrings
into the array . but what is all the "." and "*" do? what does this code do?

Full text and comments »

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

By fares, history, 9 years ago, In English

hi guys: I was trying to solve this problem -> http://codeforces.com/contest/580/problem/A here is my code :

import java.util.Scanner;

public class Main {

	public static void main(String []args){
		Scanner sc = new Scanner(System.in);
// putting the number of numbers in length.
    String length1=sc.nextLine();
    int length=Integer.parseInt(length1);
    // making an array of size length ,reading the input and putting it inside the array .
    String array[]=new String[length];
	String format=sc.nextLine();
	array=format.split(" ");
	// making help to put the lengths of the nondecreasing subsequences.
	String help="0";
	// making counter to keep track of the length of the nondecreasing subsequences.
	int counter=1;
	// looping
	for (int i = 0; i < array.length-1; i++) {
		// using parseLong to convert the elements of the array to the  long type (not using parseInt because the elements may be huge )
		
		/* testing if the current element <=the next one and incrementing counter if true , 
		 * else put the counter in help if it is bigger than it's current item and then reseting counter to 1*/
		if (Long.parseLong(array[i])<=Long.parseLong(array[i+1])) {
			++counter;
			// tracing the changes happening to counter and i.
			System.out.println(counter + " "+ i);
		}else{
			
			if (counter>Long.parseLong(help)) {
				System.out.println(counter + " "+ i);

				help=counter+"";
				counter=1;
				//  tracing counter here either.
				System.out.println(counter);
			}
		
		}

	}
	// the last counter value will not be tested and put in help ,so test it and put it in help if it is bigger than it's current element.
	if(counter>Long.parseLong(help))
		help=counter+"";
	// printing the answer.
		System.out.println(help);
	
	

		}
}

now when i test it using this input this occurs: input: 10 100 1000 100 1000 100 1000 100 1000 100 1000 output: 2 0 2 1 1 2 2 // (there should be a 2 3 and a 1 here. this isn't part of the output) 3 4 3 5 1 2 6 3 8 3 // the answer

you will notice that i in the fourth loop became 4 not 3 , how is this possible . that is the question .

Full text and comments »

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