The minimum number of rotating arrays of an offer

Source: Internet
Author: User

Title Description:

Moves the first element of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1.

Input:

The input may contain multiple test samples, for each test case,

The first behavior of the input is an integer n (1<= n<=1000000): The number of elements that represent the rotated array.

The second line of input includes n integers, where the range of each integer A is (1<=a<=10000000).

Output:

corresponding to each test case,

Outputs the smallest element in the rotated array.

Sample input:
53 4 5) 1 2
Sample output:
1


The idea of dichotomy:
1. If the elements in the array are not actually rotated, such as 1 2 3 4 5, then output ARR[INDEXMID] directly,
This is why Indexmid is initialized to index1.
2. Set 2 pointers (array subscript) index1 and Index2, respectively initialized to 0 and n-1; discussion:
If the median value of index1 and INDEX2 is greater than or equal to index1, the median value is still within the first incrementing array, so
Assigning the subscript of the median value to index1;
If the median value of index1 and Index2 is less than or equal to INDEX2, the middle value is in the second incrementing array, and the middle value
The subscript is assigned to INDEX2;
Until Index1 and Index2 are adjacent, Index2 points to the minimum value.
3. Consider special cases: assuming sequence 1 0 1 1 1, this time according to the above to the median value equals index1, then will be assigned to the value of the index1,
This is already wrong. Similarly, consider sequence 1 1 1 0 1, so when the median value is equal to arr[index1] and equal to ARR[INDEX2],
Had to iterate over the array to find the minimum value.



Code:
using namespace std;
intOrderfind (int*arr,intN) {    intresult=arr[0];  for(intI=1; i<n;++i) {        if(arr[i]<result) {Result=Arr[i]; }    }    returnresult;} intarr[1000010]; intMain () {intN;  while(cin>>n)! =EOF) {         for(intI=0; i<n;++i) {cin>>Arr[i]; }        intindex1=0; intindex2=n-1; intIndexmid=index1; BOOLflag=true;  while(arr[index1]>=Arr[index2]) {            if(index2-index1==1) {Indexmid=Index2;  Break; }            intIndexmid= (INDEX1+INDEX2)/2; if(arr[index1]==arr[index2]&&arr[index1]==Arr[indexmid]) {cout<<orderfind (arr,n) <<Endl; Flag=false;  Break; }            if(arr[indexmid]>=Arr[index1]) {index1=Indexmid; }            if(arr[indexmid]<=Arr[index2]) {Index2=Indexmid; }        }        if(flag==true) {cout<<arr[indexMid]<<Endl; }   }   return 0;} /************************************************************** problem:1386 User:lcyvino language:c++ Re sult:accepted time:670 Ms memory:5424 kb****************************************************************/

The minimum number of rotating arrays of an offer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.