博客
关于我
cf500C New Year Book Reading
阅读量:798 次
发布时间:2023-04-17

本文共 3574 字,大约阅读时间需要 11 分钟。

C. New Year Book Reading
time limit per test  2 seconds
memory limit per test  256 megabytes
input  standard input
output  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.

  1. He lifts all the books above book x.
  2. He pushes book x out of the stack.
  3. He puts down the lifted books without changing their order.
  4. 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 aslifted on that step. Can you help him?

Input

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.

Output

Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.

Sample test(s)
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note

Here's a picture depicting the example. Each vertical column presents the stacked books.

 

题意是n本书排成一堆,按给定的顺序读m次,每次读第bi本书。每本书有重量,读第bi本书的时候,代价就是所有在bi上面的书的总重量。看完后就直接放回书堆的最上层。要求确定一开始书的位置,使得代价尽量小

一开始我就是直接按每本书第一次出现的顺序搞的。比如样例1 3 2 3 1就直接确定为1 3 2  结果数据还真A了……结果最后因为小号交了一遍所以skipped……我真是无话可说……就是因为这个我从1900+掉到1800+了

后来想清楚了

假设我们考虑两本书i,j放的顺序对代价的影响,写一写就很容易发现影响只跟它第一次出现的顺序有关

比如一个看书的序列2 3 ……

有两种放法:2 3 ……或者3 2 ……

显然第一种的代价是w[2],第二种的代价是w[2]+w[3],显然第一种放法优

很容易发现按照2 3的顺序读书读完,它们的位置就唯一确定了。一定是3在2上面。后面也许还会出现要读2和3的情况,但是代价已经确定了

所以就是贪心完模拟啦

#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<deque>#include<set>#include<map>#include<ctime>#define LL long long#define inf 0x7ffffffusing namespace std;inline LL read(){    LL x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int n,m,ans;int a[200010],b[200010];int lst[200010],nex[200010],succ[200010];int main(){    n=read();m=read();    for (int i=1;i<=n;i++)a[i]=read();    for (int i=1;i<=m;i++)b[i]=read();    for (int i=m;i>=1;i--)    {        nex[i]=lst[b[i]];        lst[b[i]]=i;    }    for (int i=1;i<=m;i++)    {        if (nex[i])succ[nex[i]]=i;    }    for (int i=1;i<=m;i++)    {        int x=succ[i]+1;        bool mrk[1010]={0};        for (int j=x;j<=i-1;j++)        {            if (!mrk[b[j]])ans+=a[b[j]],mrk[b[j]]=1;        }    }    printf("%d\n",ans);    return 0;}

 

转载于:https://www.cnblogs.com/zhber/p/4196502.html

你可能感兴趣的文章
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>