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 ...
随机推荐
- vue开发 - 根据vue-router的meta动态设置html里title标签内容
1.路由文件 :router/index.js 添加 meta属性配置: import Vue from 'vue' import Router from 'vue-router' import in ...
- shell函数与位置参数举例
- lvm相关
LVM 概念:PV(单个硬件)--VG(组合)--LV(分区) pv打头的:代表pv相关的命令vg带头的:代表vg相关的命令lv带头的: 代表lv相关的命令 create:创建相关remove:移除相 ...
- windows linux子系统(Windows Subsystem for Linux)的存放目录
win10子系统把windows的底层接口做了个转换到Linux从而能运行linux,但是他在安装的时候并没有提供安装位置的选项.(还有hyper v) 现在,所有从商店安装的发行版都存在于以下目录中 ...
- 在Ubuntu上安装LAMP(Apache、Mysql、Php)
原文地址:https://howtoubuntu.org/how-to-install-lamp-on-ubuntu Ubuntu有很多工具可以帮助我们一键配置LAMP环境,比如tasksel,但这些 ...
- 最强大的Android线程池框架
背景 大家都知道在我们的开发中永远都离不开多线程,对于我们为什么要使用多线程,多线程的使用和多线程的一些基础知识这里我们就不讲了,有兴趣的朋友可以去看一下博主之前的几篇文章: 线程你真的了解它吗 这才 ...
- HTML5 canvas绘制文本
demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...
- BZOJ4269 再见xor
考前挣扎 线性基裸题 mx直接求 次大直接从低到高枚举第一个非0位 然后次大就是异或上就行了[显然贪心呐qwq 不到800b可还行 //Love and Freedom. #include<cs ...
- springboot上传excel到oss
参考:https://blog.csdn.net/qq_34864038/article/details/80239320 https://blog.csdn.net/qq_27319683/arti ...
- 修改host,访问指定服务器
途径: 1.hosts文件修改 以Windows文件为例:进入system32/drivers/etc/hosts 或者用一些软件比如switchhost等来进行修改 2.通过抓包工具修改 因为本人是 ...