题目链接:

B. Shopping

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

 

Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online.

The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order.

Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer.

When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating.

Your task is to calculate the total time it takes for Ayush to process all the orders.

You can assume that the market has endless stock.

Input
 

The first line contains three integers nm and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market.

The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k.

Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.

Output
 

Print the only integer t — the total time needed for Ayush to process all the orders.

Example
 
input
2 2 5
3 4 1 2 5
1 5
3 1
output
14
Note

Customer 1 wants the items 1 and 5.

pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5].

pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2].

Time taken for the first customer is 3 + 5 = 8.

Customer 2 wants the items 3 and 1.

pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2].

pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2].

Time taken for the second customer is 3 + 3 = 6.

Total time is 8 + 6 = 14.

Formally pos(x) is the index of x in the current row.

题意:

找到所有顾客的所有物品的位置的和,每找到一件物品就把这件物品拿到最前面;

思路:

暴力求解;

AC代码:

/*2014300227    665B - 11    GNU C++11    Accepted    15 ms    2176 KB*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+;
const ll mod=1e9+;
int n,m,fk,a[],x;
int main()
{
scanf("%d%d%d",&n,&m,&fk);
for(int i=;i<=fk;i++)
{
scanf("%d",&a[i]);
}
int ans=;
while(n--)
{
for(int i=;i<=m;i++)
{
scanf("%d",&x);
for(int j=;j<=fk;j++)
{
if(a[j]==x)
{
ans+=j;
for(int k=j;k>;k--)
{
a[k]=a[k-];
}
a[]=x;
break;
}
}
}
}
printf("%d\n",ans);
return ;
}

codeforces 665B B. Shopping(水题)的更多相关文章

  1. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  2. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...

  3. codeforces 569B B. Inventory(水题)

    题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. Codeforces 489A SwapSort (水题)

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. codeforces 688A A. Opponents(水题)

    题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #332 (Div. 2) A. Patrick and Shopping 水题

    A. Patrick and Shopping Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  7. CodeForces 534B Covered Path (水题)

    题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...

  8. Codeforces Gym 100286I iSharp 水题

    Problem I. iSharpTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  9. CodeForces 705A(训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/A 从第三个输出中可看出规律, I hate that I love that I hate it ...

随机推荐

  1. KVC技巧二则

    说两个与KVC相关的技巧. 1.KVC与字典 有时候我们需要取出嵌套字典中的某个键的值.例如某个嵌套字典: NSDictionary *dict = @{@"subDict":@{ ...

  2. 邁向IT專家成功之路的三十則鐵律 鐵律二十四:IT人歲月增長之道-智慧

    老子曾經在道德經中提到:「以道為本而繁守不失的,可算是長久.身雖死亡而精神不朽的,可算是長壽」.人在世間最悲哀的莫過於老死,但最可貴的則莫過於智慧.只是人的智慧不一定會隨著年齡的增長與歲月的流逝而成長 ...

  3. NMM3DViewer 设计

    在FrameworkInterfaces工程的INMM3DServer.cs中定义了 岩石材料结构 BlockMaterial  -----> StrBLOCKProperty     publ ...

  4. iphone越狱-------平刷回越狱前(未越狱)状态

    众所周知,iPhone采用了沙盒机制,应用之间不能任意的访问,所以很多机友在拿到iPhone后,往往选择进行越狱,但是有时候,越狱的手机有的时候在安全.性能.流畅性等方面表现并不如意,所以只好寻求重新 ...

  5. Linux包括hash_map和hash_set的not declared问题

    当在Linux下cpp文件包括hash_map或hash_set时.会出现"'hash_map' was not declared in this scope"问题. #inclu ...

  6. jquery 的父子节点

    1.一级父节点 parent() n级父节点 parents(???). 2.一级子节点 children() n级子节点  用find(???)

  7. react 监听 移动端 手机键盘 enter 事件

    处理方法: (1)html书写 form标签中去掉action参数,定义onSubmit方法,如下所示: /** * 搜索框 组件 */ import React,{PureComponent} fr ...

  8. WPF简单计算器

  9. Ubuntu 16.04下配置Golang开发环境

    安装之前先要明白两个变量,后面介绍安装时,会用这两个变量 GOROOT   , 这是go的工作目录,比如 /home/[替换为你的用户名]/go/work GOPATH    , 这是go的安装目录, ...

  10. 抽象类的子类能够new

    纠结了半天,我以为继承了Activity后不能new这里被那个onCreate方法迷惑了以为会出现故障一直没直接创建对象类使用 后来试了试才知道 activity似乎是一个抽象类吧. 你要用他的方法, ...