Wing Payment Programming Contest--modifying series

Source: Internet
Author: User

Topic details (Modify the series):

We have a series. We can replace any of these items with other positive integers, but we cannot delete the items or exchange the order of the items. Does it need to be replaced at least several times to turn the sequence into a strictly monotonically increasing one?

Input format

Multiple sets of data, first row of each group of data a positive integer n (n<=1000000)

The second row of each set of data is a non-negative integer separated by n spaces, each of which does not exceed 10^9, which represents the number in the sequence.

Output format

For each set of data, the output line contains its minimum number of replacements.

Answer instructions:

Input sample

3

4 10 20

5

1 7 2) 20 22

Output sample

0

1

First, the idea: to draw on the idea of the longest increment subsequence (LIS), add the restriction condition: element value >= element subscript; The difference between elements >= the difference between the subscript of an element.
Example: a[0...8] = {2, 1, 5, 3, 6, 4, 8, 9, 7}
Set two arrays: B records the longest increment substring (in accordance with the condition of the subject test instructions), and POS records the subscript of each element in the substring in the original array. Set Variable: Len records the length of the substring.
(1) Looking from the back to the array A, the first satisfies: the element value >= the subscript element. Should be 9, so there are b[0]=9, pos[0]=7,len=1;
(2) Look again before 9 element 8, first determine whether it >= subscript, it is established. The second judgment (b[len-1]-a[6] = 9-8) >= (1=pos[len-1]-6), is established so that the substring is added to element 8. Then b[0,1] = {9,8}, pos[0,1] = {7,6}, len=2;
(3) Look again 8 before the element 4, because it < subscript, so directly skip;
(4) See element 6 before 4, it >= subscript, (b[len-1]-a[4] = 8-6) >= (2 = pos[len-1]-4), so the substring joins element 6, then b[0,1,2] = {9,8,6},pos[0,1,2]={7,6,4} , Len = 3;
(5) Look again before 6 element 3,>= subscript, 6-3>=1, then b[0,1,2,3]={9,8,6,3},pos[0,1,2,3]={7,6,4,3},len=4;
(6) Looking again before 3 5,>= subscript, does not conform to 3-5>=1, because 5>3, depends on whether to replace the existing elements of B. Found in B, just above 5, that's 6. The next step is to determine if 5来 should be replaced with the position of element 3 after 6. Because 6-5=1>= (2=pos[2]-2) is not established, so 5来 can not be replaced, so or skip;
(7) Again see 5 before the 1, are in line with the requirements, so b[0,1,2,3,4]={9,8,6,3,1},pos[0,1,2,3,4]={7,6,4,3,1},len=5;
(8) Looking again before 1 2,>= subscript, does not conform to 1-2>=1, because 2>1, depends on whether to replace the existing elements in B. Also found 3, because 3-2>=3 is not established, so skip.
At this point, the maximum number of substrings that need to be changed is obtained, so the total length of the-len is the minimum quantity to be replaced.
 required Java, basic input and output will not, have to help Baidu, the following is the code of chicken: 
Import Java.util.Scanner;          public class Main {public static void main (string[] args) {Scanner in = new Scanner (system.in);          int n;          int[] num;            while (In.hasnext ()) {n = in.nextint ();            num = new Int[n];            for (int i = 0; i < n; i++) Num[i] = In.nextint ();          Lis (num, n); }} private static void Lis (int[] num, int n) {//TODO auto-generated method stubint[] Bnum = new int[n];int[] B pos = new Int[n];int init;//from the back to the front, the first element value is greater than or equal to the subscript (starting from 0) of the element headed int len;//the longest strict monotonic substring length for (init = n-1; init >= 0; init--) {if (num [init] >= init) {bnum[0] = num[init];bpos[0] = init;break;}} Len = 1;if (init < 0) {System.out.println (n-1);} for (int i = init-1; I >= 0; i--) {if (Num[i] >= i) {//value is greater than or equal to subscript for comparison if (Bnum[len-1]-num[i] >= bpos[len-1]-I {//value difference greater than or equal to the underlying difference to add bnum[len] = num[i];bpos[len++] = i;} else if (Num[i] > bnum[len-1]) {int pos = find (Bnum, BPOs, Len, num[i], i);//Find Alternate locationif (pos! =-1) {Bnum[pos] = num[i];bpos[pos] = i;}}} System.out.println (N-len);} private static int Find (int[] bnum, int[] bpos, int len, int number, int pos) {//TODO auto-generated method Stubint i = 0 ; for (i = len-1; i > 0; i--) {//Find the element position that is just larger than number if (Bnum[i-1] > number) {break;}} if (i <= 0) return 0;else{if (Bnum[i-1]-number >= bpos[i-1]-POS)//judgment replacement feasible no return i;elsereturn-1;}}

The result of the local test is correct, but the submission is wrong, really do not know where the problem ...
4
5 4 3 2
3


6
5 6 3 7 4 8
4


6
5 6 4 7 3 8
4


6
5 6 3 4 7 8
2


6
5 6 4 3 7 8
3


6
1 2 3 4 5 6
0

Think of the reason for the mistake, in the revision ...

Wing Payment Programming Contest--modifying series

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.