HDU 5122 K.bro Sorting (2014 Beijing Regional Game K-problem simulation)

Source: Internet
Author: User

The problem defines a new sort algorithm, which is to put a number in a sequence of sequences, if its right number is smaller than it

You can move to the right of the past until the number on the right is larger than it.

Easy to get, if the simulation is O (n^2) efficiency, certainly not

Thinking about it, this problem can be translated into

For each element in this sequence, the right side of the string is less than its number and, if present, ++ans

I didn't think of it at first. = = should not be

1 //#pragma COMMENT (linker, "/stack:16777216")//For C + + Compiler2#include <stdio.h>3#include <iostream>4#include <cstring>5#include <cmath>6#include <stack>7#include <queue>8#include <vector>9#include <algorithm>Ten #definell Long Long One #defineMax (b) ((a) > (b))? (a): (b)) A #defineMin (b) ((a) < (a))? (a): (b)) - #defineAbs (x) (((x) > 0)? (x): (-(x))) -  the using namespacestd; -  - Const intINF =0x3f3f3f3f; - inta[1000001]; +  - intMain () { +     intI, J, K, T, N, m, numcase =0; Ascanf"%d",&t); at      while(t--){ -scanf"%d",&n); -          for(i =0; I < n; ++i) scanf ("%d",&a[i]); -         intAns =0; -          for(i = n-2; I >=0; --i) { -             if(A[i] > a[i +1]){ inSwap (A[i], a[i +1]); -++ans; to             } +         } -printf"Case #%d:%d\n",++numcase, ans); the     } *     return 0; $}

HDU 5122 K.bro Sorting (2014 Beijing Regional Game K-problem simulation)

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.