Can't debugg this !

Revision en5, by fares, 2016-01-31 23:32:19

Hi: I was solving this problem http://codeforces.me/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 :)
Tags help, binary search

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English fares 2016-01-31 23:32:19 38 Tiny change: '\n \n}\n\n~~~~~\n\' -
en4 English fares 2016-01-31 23:29:59 736 Tiny change: 'he code:\n~~~~~\n\' -
en3 English fares 2016-01-31 11:02:18 44 Tiny change: 'f it). \n\n ' -> 'f it). \nedit2:the code gives WA on huge test cases .\n '
en2 English fares 2016-01-31 10:10:17 106 Tiny change: 'de:\n~~~~~\n import ' -
en1 English fares 2016-01-31 09:44:58 1701 Initial revision (published)