Coderforces 518C Anya and Smartphone (analog)

Source: Internet
Author: User

Test instructions: Given a phone, and then a total of n apps, tell you that each screen up to put K, now you run M app, each time from the first screen began to slide, each run one, it and the previous exchange position, the first will not change, now ask you how many times to slide.

Analysis: This problem, no algorithm, is analog Bai, but to pay attention to the time, can not tle, so we have to put all the positions in advance to save, so that the time to find an O (1), otherwise it will time out, you can use two arrays, you can use map, a deposit number, a storage location,

Then you can swap it out after you run it.

The code is as follows:

#include <iostream> #include <cstdio> #include <map> #include <cmath>using namespace std; typedef long LONG ll;const int MAXN = 1e5 + 5;map<int, int> mp;map<int, int> mps;int main () {    int n, m, K, x;    while (scanf ("%d%d", &n, &m, &k) = = 3) {        for (int i = 0; i < n; ++i) {            scanf ("%d", &x);            MP[X] = i+1;            MPS[I+1] = x;        }        LL ans = 0;        for (int i = 0; i < m; ++i) {            scanf ("%d", &x);            int t = mp[x];            Ans + = (LL) ceil ((double) t/k);            if (t = = 1) continue;            Swap (Mp[mps[t]], mp[mps[t-1]);            Swap (Mps[t], mps[t-1]);        }        printf ("%lld\n", ans);    }    return 0;}

Coderforces 518C Anya and Smartphone (analog)

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.