Codeforces 500C New Year Book Reading
2 seconds
256 megabytes
standard input
standard output
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
- He lifts all the books above book x.
- He pushes book x out of the stack.
- He puts down the lifted books without changing their order.
- After reading book x, he puts book x on the top of the stack.

He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
3 5
1 2 3
1 3 2 3 1
12
Here's a picture depicting the example. Each vertical column presents the stacked books. 
贪心。
假设要按照 i->j 顺序看 , 假设 i 在 j 上边,花费就是 wi , 假设j 在 i 上边 ,花费就是wj + wi 了 。
那么直接按照m个日子的顺序来模拟一次 。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ; int n , m , w[N] , d[N];
bool vis[N];
int main()
{
while( cin >> n >> m ) {
for( int i = ; i <= n ; ++i ) cin >> w[i] ;
for( int i = ; i <= m ; ++i ) cin >> d[i] ;
LL ans = ;
for( int i = ; i <= m ; ++i ) {
memset( vis , false , sizeof vis );
for( int j = i - ; j > ; --j ){
if( d[i] == d[j] ) break ;
if( !vis[ d[j] ] ) { ans += w[ d[j] ] ; vis[ d[j] ] = true ; }
}
}
cout << ans << endl ;
}
}
Codeforces 500C New Year Book Reading的更多相关文章
- Educational Codeforces Round 31 A. Book Reading【暴力】
A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces 820A. Mister B and Book Reading 解题报告
题目链接:http://codeforces.com/problemset/problem/820/A 坑爹题目,坑爹题目,坑爹题目....汗 = =! 后台还110个 test 有个地方需要注意下 ...
- Codeforces Round #546 (Div. 2) A. Nastya Is Reading a Book
链接:https://codeforces.com/contest/1136/problem/A 题意: 给n个区间,每个区间范围不超过100,n不超过100. 给一个位置k,1-(k-1)是遍历过的 ...
- 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading
[题目链接]:http://codeforces.com/contest/820/problem/A [题意] 每天看书能看v页; 且这个v每天能增加a; 但是v有上限v1; 然后每天还必须往回看t页 ...
- 【Educational Codeforces Round 31 A】Book Reading
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...
- Codeforces 1213C Book Reading
cf题面 中文题意 多组数据,每组给一个n给一个m,要求正整数\(1\)~\(n\)中,所有能被m整除的数的个位之和. 解题思路 首先,能被m整除的数的数量是\(\lfloor\frac{n}{m}\ ...
- Codeforces Round #582 (Div. 3) C. Book Reading
传送门 题意: 给你n,k.表示在[1,n]这个区间内,在这个区间内找出来所有x满足x%k==0,然后让所有x的个位加到一起(即x%10),输出. 例如:输入10 2 那么满足要求的数是2 4 6 8 ...
- Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)
题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library set
B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
随机推荐
- spring(一):spring的基础以及组件
spring简介 spring是一种开源轻量级框架,是为了解决企业应用程序复杂性而创建的 spring是企业应用开发的“一站式”框架,致力于为javaEE应用的各层(表现层.业务层.持久层)开发提供解 ...
- 20180119-文件操作open用法
官方文档 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, open ...
- Spring Cloud Contract简介
转载:https://www.jianshu.com/p/e3277824a10a 和dummy service一样 Spring Cloud Contract是个啥? Spring Cloud Co ...
- 202-基于TI DSP TMS320C6678、Xilinx K7 FPGA XC72K325T的高速数据处理核心板
基于TI DSP TMS320C6678.Xilinx K7 FPGA XC72K325T的高速数据处理核心板 一.板卡概述 该DSP+FPGA高速信号采集处理板由我公司自主研发,包含一片TI DSP ...
- pycharm 断点跟踪
F8 下一步 F7 step info F9 直接运行
- Vue组件-组件组合
组件设计初衷就是要配合使用的,最常见的就是形成父子组件的关系:组件 A 在它的模板中使用了组件 B. <html> <head> <title>Vue组件 A 在它 ...
- django model的get和filter方法的区别
django的get方法: 1django的get方法是从数据库的取得一个匹配的结果,返回一个对象,如果记录不存在的话,它会报错. 2如果你用django的get去取得关联表的数据的话,而关键表的数据 ...
- Appium API文档中文版
传送门 https://testerhome.com/topics/3144
- vue.js 导出JSON
cnpm install file-saver --save <template> <div class="hello"> <button @clic ...
- python3安装pdfminer并使用
1.python3不同与2版本不能使用pdfminer pip install pdfminer3k 2.使用pdfminer解析相应文档并保存到相应的文件夹中 # encoding : udf-8 ...