10570-meeting with Al (greedy)

Source: Internet
Author: User

At first I had the illusion of finding the answer in O (n), but later proved that my method was wrong, and I will not repeat it here.

One way to do this online is to raise the point, and then make each bit from the starting point correspond to 1, 2, 3...N, or vice versa. If not, swap the corresponding values in the past.

This is obviously a greedy strategy. Constant pursuit of local optimization, the final answer is also optimal.

The method of proving greed is generally disproved: assuming this is not optimal, then the current number will be exchanged with the other number of times before switching to this position. Assuming and other number exchange K times to the original position, the best result is to change the number of k+1 to where it should be, however, according to our method, for just that method involved in the element, can also arrange the number of k+1 (think about why).

In other cases, our strategy can at least arrange the n number after the N-Exchange, and if not, it will obviously not be better. So, in summary, the greedy strategy is optimal.

See the code for details:

#include <bits/stdc++.h>using namespace Std;const int maxn = 505;int n,a[maxn],b[maxn],cnt[maxn];void init () {for (int i=0;i<n;i++)    {A[i] = B[i]; Cnt[a[i]] = i; }}int Main () {while (~SCANF ("%d", &n) &&n) {for (int i=0;i<n;i++) {scanf ("%d", &a[i])            ;            B[i] = A[i];        Cnt[a[i]] = i;        } int ans = 1000000000;            for (int i=0;i<n;i++) {int c = 1,bbs=0; for (int j=0;j<n;j++) {//forward int v = (i+j)%n;//small tip, used to handle the ring sequence if (a[v]! = c) {int u=a[v]; a [V]=c; A[cnt[c]]=u; CNT[U]=CNT[C]; Cnt[c]=v; bbs++;                 } C + +;            A little easier with swap} ans = min (Ans,bbs); Init (); Initialize c = 1;            BBS = 0;                for (int j=n-1;j>=0;j--) {//reverse int v = (i+j)%n;                if (a[v]! = c) {int u=a[v]; a[v]=c; a[cnt[c]]=u; cnt[u]=cnt[c]; cnt[c]=v; bbs++;}            C + +;     }       ans = min (Ans,bbs);        Init ();    } printf ("%d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

10570-meeting with Al (greedy)

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.