HDU 5532--almost Sorted Array —————— "Tips"

Source: Internet
Author: User

Almost Sorted Array

Time limit:4000/2000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 236 Accepted Submission (s): 113


Problem Descriptionwe is all familiar with sorting algorithms:quick sort, merge sort, heap sort, insertion sort, selecti On sort, bubble sort, etc. But sometimes it was an overkill to use these algorithms for an almost sorted array.

We say an array was sorted if its elements be in non-decreasing order or non-increasing order. We say an array are almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now is given an array a1,a2,... ,an, is it almost sorted?

Inputthe first line contains an integerTindicating the total number of test cases. Each test case starts with an integerNIn one line, then one line withNIntegersa1,a2,... ,an .

1≤T≤
2≤n≤5
< Span id= "mathjax-span-57" class= "Mrow" >1 ≤ Ai≤105
There is at the most test cases With < Span id= "mathjax-span-68" class= "Mrow" >n >1000 .

Outputfor Each test case, please output "' YES '" if it is almost sorted. Otherwise, Output "' NO '" (both without quotes).

Sample Input332 1 733 2 153 1 4 1 5

Sample Outputyesyesno

SOURCE2015ACM/ICPC Asia Changchun Station-Replay (thanks to Northeast Normal University) title: give you a sequence of n number, ask if you can delete a number from it, so that the remaining sequence is not incremented or not decremented. Thinking about the problem: consider whether the first number must be retained first. If you delete the first number, then directly from the second number, determine whether the sequence is not increment or decrement. If you do not delete the first number, then you find that only one number satisfies the rendering in both cases. Take judgment not diminishing as an example. For sequence 7 8 1000 9 10来 said that the middle of the number is too large, if the retention of 1000, then the back of the sequence will not be used, so we need to delete 1000, so that the sequence will not be decremented. For sequence 7 8 1 9 10来 said, it is clear that 1 is to be deleted. After deleting 1, the sequence will not be rendered decremented. So we judge whether A[i]<a[i-1]. If there is, then the element needs to be deleted, for the first case, our I will point to 9, and we need to determine whether a[i]>=a[i-2], that is, 9 and 8 here. For the second case, we also judge whether A[i]<a[i-1], but this time I point to 1, we have to judge whether A[i+1]>=a[i-1]. If not satisfied, it is necessary to delete more than one element here, so direct false, otherwise, the record has deleted an element. If the next time there is a[i]<a[i-1], then direct false.
#include <stdio.h> #include <algorithm> #include <string.h>using namespace std;const int maxn = 100010;    const int INF = 0x3f3f3f3f;int a[maxn];bool jud1 (int n) {int tmp = a[2];        for (int i = 2; I <=n; i++) {if (tmp > A[i]) {return false;        }else{tmp = A[i]; }} return true;    BOOL Jud2 (int n) {int tmp = a[2];        for (int i = 2; I <= n; i++) {if (TMP < a[i]) {return false;        }else{tmp = A[i]; }} return true;    BOOL Jud3 (int n) {a[0] = 0; a[n+1] = INF;    int Tim = 0;    int fir = 0;            for (int i = 2; I <= n; i++) {if (A[i]<a[i-1]) {if (fir = = 1) return false;            fir++; if (a[i+1]>=a[i-1]| |            A[i]>=a[i-2]) {continue;            }else{return false; }}} return true;    BOOL Jud4 (int n) {a[0] = INF; a[n+1] = 0;    int Tim = 0;    int fir = 0; for (int i= 2; I <= N;            i++) {if (A[i]>a[i-1]) {if (fir = = 1) return false;            fir++; if (a[i+1]<=a[i-1]| |            A[i]<=a[i-2]) {continue;            }else{return false; }}} return true;    int main () {int t,n,m;    scanf ("%d", &t);       while (t--) {scanf ("%d", &n);       for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]);            } if (JUD1 (n)) {puts ("YES");        Continue            } if (JUD2 (n)) {puts ("YES");        Continue            } if (Jud3 (n)) {puts ("YES");        Continue            } if (Jud4 (n)) {puts ("YES");        Continue    } puts ("NO"); } return 0;} /*55548 6 7 848 6 7 751 2 1000 7 851 2 100 2 341 1 1 167 8 1 1 1 10*/

  

HDU 5532--almost Sorted Array —————— "Tips"

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.