题目链接:

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. spring aop提供了两种实现方式jdk和cglib

    Spring AOP使用了两种代理机制:一种是基于JDK的动态代理:另一种是基于CGLib的动态代理.之所以需要两种代理机制,很大程度上是因为JDK本身只提供接口的代理,而不支持类的代理. Sprin ...

  2. 【spring boot】13.在spring boot下使用多线程

    使用场景: 方法处理到某一步,需要将信息交给另一个线程去处理!! =================================================================== ...

  3. Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息。

    Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息.”的问题解决 2015-01-22 09:49:02 标签:Windows Server2003 PerfDi ...

  4. IOS 后台保持连接

    当iphone应用程序进行网络编程时,切到后台后,socket连接会断掉,ios的设计就是这样. 但是好在apple公司也没有那么绝,还是有一些东西可以在后台运行的(backgroundmodes), ...

  5. linux下脚本监控网络流量

    linux下脚本监控网络流量 学习了:https://blog.csdn.net/chenghuikai/article/details/48437479 学习了:http://www.jb51.ne ...

  6. DM8168 unrecoverable error: OMX_ErrorBadParameter (0x80001005) [resolved]

    DM8168 custom board 成功启动系统之后想先測一下8168编解码功能,把开发包里的examples跑一遍.启动完毕后.连上HDMI显示,在starting Matrix GUI app ...

  7. Cocos2d-X中提高性能的方法

     1)内存使用效率: 使用大纹理 场景切换时,要尽量使用replaceScene 2)用好缓存: CCTextureCache(纹理缓存) CCSpriteFrameCache(精灵帧缓存) CC ...

  8. 网页编程-Django(一)

    业内: GET:获取数据 POST:提交数据 上传单个数据: request.POST.get(‘’name名‘’) 上传多选数据: request.POST.getlist('name名') 上传文 ...

  9. cvpr2017-code-etc

    cvpr2017: code:   http://staffhome.ecm.uwa.edu.au/~00053650/code.html

  10. 数据库MySQL经典面试题之SQL语句

    数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...