UVa 299-train Swapping

Source: Internet
Author: User

Title: give you a sequence that can only exchange adjacent two numbers at a time, asking the minimum number of exchanges that make the sequence increment.

Analysis: Mathematics, reverse order number.

If a[i] > A[j] and I < J are present in a sequence, then a[i] and a[j] form a pair of reverse pairs;

The total number of reverse pairs of a sequence is the inverse number of the sequence;

Title, the adjacent element exchange, the reverse number of each exchange sequence will inevitably change 1, and an increment of the sequence number 0;

Therefore, the least number of exchanges is the number of reverse order, and each time in reverse order to reduce the way to get an incremental sequence of exchange.

Description: Finishing a long time ago brush the topic.

#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio>using namespace std; int Data[100];int Main () {int n,m,count;while (CIN >> N) while (n--) {cin >> m;for (int i = 0; i < m; + + I cin >> Data[i];count = 0;for (int i = 0; i < m; + + i) for (int j = 0; J < i; + + j) Count + = (data[j]>dat A[i]) cout << "Optimal train swapping takes" << count << "swaps." << Endl; return 0;}

UVa 299-train Swapping

Related Article

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.