1279C - Stack of Presents - CodeForces Solution


data structures implementation *1400

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    input()
    pilha_presentes = {}
    for indice, presente in enumerate(list(map(int, input().split(" ")))):
        pilha_presentes[presente] = indice

    tempo = 0
    cont_enviados = 0
    maior_enviado = -1
    for presente in list(map(int, input().split(" "))):
        indice = pilha_presentes[presente]
        if indice > maior_enviado:
            maior_enviado = indice
            tempo += (indice - cont_enviados) * 2 + 1
        else:
            tempo += 1
        cont_enviados += 1
    print(tempo)

C++ Code:

#include <bits/stdc++.h>
#include <algorithm>
#define vi vector<long long>
#define ll long long int
#define lcm(a, b) (a * b) / __gcd(a, b)
#define sr(a) sort(a.begin(), a.end())
#define fr(i, a, b) for (long long i = a; i < b; i++)
#define srr(a) sort(a.rbegin(), a.rend())
#define mx(a) *max_element(a.begin(), a.end());
#define mn(a) *min_element(a.begin(), a.end());
#define fast                    \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define tolow(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define toupp(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define yesOrNo(cond) cout << ((cond) ? "YES\n" : "NO\n");
using namespace std;
const ll P = 1e9 + 7;
const ll N = 1e5 + 2;
bool isPrime(int number)
{

  if (number < 2)
    return false;
  if (number == 2)
    return true;
  if (number % 2 == 0)
    return false;
  for (int i = 3; (i * i) <= number; i += 2)
  {
    if (number % i == 0)
      return false;
  }
  return true;
}
vi ayush(ll n)
{
  bool prime[n + 1] = {true};
  for (ll i = 2; i <= n; i++)
  {
    if (prime[i] == true)
    {
      for (ll j = i * i; j <= n; j++)
      {
        prime[j] = false;
      }
    }
  }
  vi res;
  for (ll i = 2; i <= n; i++)
  {
    if (prime[i] == true)
    {
      res.push_back(i);
    }
  }
  return res;
}
bool isInt(float k)
{
  return (floor(k) == ceil(k)) ? true : false;
}
bool isPow2(ll n)
{
  return (n && !(n & (n - 1)));
}
bool isPsquare(ll f)
{
  ll r = sqrt(f);
  return (r * r == f);
}
bool isValid(string s)
{
  int ans = 0;
  for (char c : s)
  {
    if (c == '(')
    {
      ans++;
    }
    else
    {
      if (ans > 0)
      {
        ans--;
      }
      else
      {
        return false;
      }
    }
  }
  return true;
}
ll mul(ll x, ll y)
{
  return ((x * y) % P); // time complexity O(1)
}
ll fact[N];
void calculate_factorial()
{
  fact[0] = 1;
  for (ll i = 1; i < N; i++)
  {
    fact[i] = mul(fact[i - 1], i); // time complexity(O(N))
  }
}
ll powrm(ll x, ll y)
{
  ll res = 1;
  while (y)
  {
    if (y & 1)
    {
      res = mul(x, res); // time complexity O(log(Y))
    }
    y = y >> 1;
    x = mul(x, x);
  }
  return res;
}
ll inv(ll x)
{
  return powrm(x, P - 2); // time complexity O(log(Y))
}
ll divm(ll x, ll y)
{
  return mul(x, powrm(y, P - 2)); // time complexity O(log(Y))
}
ll ncr(ll n, ll r)
{
  if (n >= r)
  {
    calculate_factorial();
    return mul(mul(fact[n], inv(fact[r])), inv(fact[n - r])); // time complexity O(log(Y))
  }
  else
  {
    return -1;
  }
}
void solve()
{
  ll n, m;
  cin >> n >> m;
  ll p[N];
  vi q(m, 0);
  fr(i, 0, n)
  {
    ll k;
    cin >> k;
    p[k] = i + 1;
  }
  fr(i, 0, m)
  {
    cin >> q[i];
  }
  ll mx = p[q[0]];
  ll ans = 2 * (p[q[0]] - 1) + 1;
  ll left = 1;
  fr(i, 1, m)
  {
    if (p[q[i]] < mx)
    {
      ans++;
      left++;
    }
    else
    {
      ans += 2 * (p[q[i]] - 1 - left) + 1;
      left++;
      mx = p[q[i]];
    }
  }
  cout << ans << "\n";
}
int main()
{
  fast int t;
  cin >> t;
  while (t--)
  {
    solve();
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions