Codeforces 584E Anton and Ira [idea title]

Source: Internet
Author: User

Test Instructions Brief Description:

Given the initial arrangement of a $1$ to $n (n<=2000) $ and the final arrangement we can pick the location for $i$ and $j$ each time

and swap their locations cost $ |i-j| $ minimum cost required to transition from initial state to end state

-----------------------------------------------------------------------------------------------------------

The game left a $40min$, but his greedy strategy still has loopholes.

After the end of the homepage of the official solution of the sense of thought is very worthy of reference

First we change the end state to the $1~n $ initial state and correspond to it (set to $a[i]$)

For example, a sample can become

$2 4 1$

$2 3 4$

We assume that the cost per exchange is $2|i-j| $ then the cost from the initial state to the last state optimal is

$\sum_{i}^{n}|a[i]-i|$

-----------------------------------------------------------------------------------------------------------

Already know the nether of the problem. Let's consider whether there is always a plan to achieve the nether cost for any situation

Each time we consider the smallest value in the current state and not the number in the corresponding position, then find the first number on the left that is larger than its position.

And then swap until it changes to its target location.

We're going to find that we've been moving towards the target position for as long as we've changed. No round trip

So the cost is the smallest, that is $\sum_{i}^{n}|a[i]-i|$.

In addition due to the number of exchanges given the range of the larger $ (N^{2}/2) $ is just a little larger than the limit condition ($n $ to $1$ rank)

So this problem can be solved here.

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn= .;intA[n],pos[n];intAns1[n*n>>1],ans2[n*n>>1];intN,k,ans;intMain () {scanf ("%d",&N);  for(intI=1; i<=n;++i) scanf ("%d",&A[i]); intx;  for(intI=1; i<=n;++i) {scanf ("%d",&x); POS[X]=i; }     for(intI=1; i<=n;++i) a[i]=Pos[a[i]];  for(intI=1; i<=n;++i) {intj=i;  while(a[j]!=i)++J;  while(j!=i) {intl=j-1;  while(a[l]<j)--l; Ans+=j-M; ++K; ANS1[K]=l; ANS2[K]=J;            Swap (a[l],a[j]); J=l; }} printf ("%d\n%d\n", ans,k);  for(intI=1; i<=k;++i) printf ("%d%d\n", Ans1[i],ans2[i]); return 0;}

Codeforces 584E Anton and Ira [idea title]

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.