Los P5149 meeting Valley seat

Los P5149 meeting Valley seat

Description

  • Now the principal campus network published a seating chart, n teachers left to right in a row. The teachers are very satisfied with this seat.

    However, to the meeting, the president does not care to seating disrupted, teachers are dissatisfied. The teachers do not care how much has changed his position, but if there is a teacher and a b, their original seats on the left is a b, now turned into a b in the right side, then this will be a contribution to the teacher units dissatisfaction value.

    Principals want to know the total value of these teachers are dissatisfied with how much.

Input

  • The first line of a positive integer n, n is a teacher.

    The second row of n strings, each string representing the name of the teacher (case sensitive). This line represents the original seat table.

    After the third row of n string representative disrupt seating.

Output

  • The total value of the dissatisfied teachers.

Sample Input

5
A B C D E
B A D E C

Sample Output

3

Data Size

  • 30% of the data, n≤1000;

    100% of the data, n≤100000, every teacher name not longer than 5, only uppercase and lowercase letters and different from each other. Note that the names are case sensitive.

answer:

  • Reverse right.
  • Brush stroke engine room water problem
  • Fenwick tree with the requirements.
#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#define int long long
#define N 100005
#define lowbit(x) (x & (-x))
using namespace std;

int n, ans;
int a[N], t[N];
map<string, int> mp;

void upd(int pos, int val)
{
    while(pos <= n)
    {
        t[pos] += val;
        pos += lowbit(pos);
    }
}

int ask(int pos)
{
    int res = 0;
    while(pos >= 1)
    {
        res += t[pos];
        pos -= lowbit(pos);
    }
    return res;
}

signed main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        string t; cin >> t;
        mp[t] = i;
    }
    for(int i = 1; i <= n; i++)
    {
        string t; cin >> t;
        a[i] = mp[t];
    }
    for(int i = 1; i <= n; i++)
    {
        upd(a[i], 1);
        ans += i - ask(a[i]);
    }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11620740.html