C. New Year Book Reading
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.

这道题的主要是考虑怎么排序,其实只要按照取书本的顺序就行了,比如1 1 1 1 1 1 2,取两本书2 1,那么显然初始序列是1 2 时总价值最小,分析样例,如果3本书取书的顺序是3 1 2 ,那么这些步骤后书的排序顺序一定是2 3 1,但是初始序列是3 1 2的总价值最小,那么加上后面的也一定最小。
#include<stdio.h>
#include<string.h>
int w[600],a[600],c[1005];
int main()
{
int n,m,i,j,t,flag,sum,u,h;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(w,0,sizeof(w));
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(i=1;i<=n;i++){
scanf("%d",&w[i]);
}
t=0;
for(i=1;i<=m;i++){
scanf("%d",&c[i]);
if(t==0){
t++;a[t]=c[i];continue;
}
flag=1;
for(j=1;j<=t;j++){
if(c[i]==a[j]){
flag=0;break;
}
}
if(flag==0)continue;
else{
t++;a[t]=c[i];continue;
}
}
sum=0;
for(i=1;i<=m;i++){
/*for(j=1;j<=n;j++){
printf("%d ",a[j]);
}printf("\n");*/
if(c[i]==a[1])continue;
for(j=1;j<=t;j++){
if(c[i]==a[j]){
u=j;break;
}
sum=sum+w[a[j]];
}
h=a[u];
for(j=u;j>=2;j--){
a[j]=a[j-1];
}
a[1]=h;
}
printf("%d\n",sum);
}
return 0;
}
C. New Year Book Reading的更多相关文章
- Reading C type declarations(引用http://unixwiz.net/techtips/reading-cdecl.html)
Even relatively new C programmers have no trouble reading simple C declarations such as int foo[5]; ...
- Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9001/api/size/get. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:/ ...
- Git Learning - By reading ProGit
Today I begin to learn to use Git. I learn from Pro Git. And I recommend it which is an excellent bo ...
- MySQL远程连接丢失问题解决方法Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0
最近远程连接mysql总是提示 Lost connection 很明显这是连接初始化阶段就丢失了连接的错误 其实问题很简单,都是MySQL的配置文件默认没有为远程连接配置好,只需要更改下MySQL的配 ...
- 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)
Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
- Flesch Reading Ease -POJ3371模拟
Flesch Reading Ease Time Limit: 1000MS Memory Limit: 65536K Description Flesch Reading Ease, a reada ...
- Mac下遇到 'reading initial communication packet’ 问题
今天在开发过程中,一个单位跑的好好的项目,在家中的Mac下运行时,遇到了下面这个错误: "Lost connection to MySQL server at 'reading init ...
- A log about Reading the memroy of Other Process in C++/WIN API--ReadProcessMemory()
Memory, is a complex module in Programing, especially on Windows. This time, I use cpp with win wind ...
- Error: Cannot open main configuration file '//start' for reading! 解决办法
当执行service nagios start启动nagios时,报错:Error: Cannot open main configuration file '//start' for reading ...
- Spring mvc 中使用ftl引用共通文件出错 FreeMarker template error: Error reading included file "/WEB-INF/ftl/common/errormessage.ftl"
初次接触spring mvc,想做一个小的练习项目,结果在ftl文件中引用其它的共通ftl文件时出错.
随机推荐
- Ansible User 模块添加单用户并ssh-key复制
Ansible User 模块添加单用户并ssh-key复制 1 Ansible 版本: ansible 2.9.6 config file = /etc/ansible/ansible.cfg co ...
- LeetCode202. 快乐数
题目 编写一个算法来判断一个数 n 是不是快乐数. 快乐数定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1, 也可能是 无限循环 但始终变不到 ...
- buuctf刷题之旅—web—随便注
打开环境 根据提示应该是sql注入 查看数据库名,和数据表 1';show databases;# 1';show tables;# 查看表内字段(1';desc `1919810931114514` ...
- oracle绑定变量测试及性能对比
1.创建测试数据 2.查看cursor_sharing的值 SQL> show parameter cursor_sharing; NAME TYPE VALUE --------------- ...
- centos7虚拟机开启端口后 外部不能访问的问题
转载 https://blog.csdn.net/u012045045/article/details/104219823 虚拟机新开了5005端口,系统内部是显示开了的(wget 192.168.4 ...
- django ajax应用
ajax: 什么是ajax,有什么作用: 以前我们在页面向后台提交数据的时候都是使用from表单,这样的提交会在提交的时候将整个页面全部刷新,如果你在填写表单的时候提交之后发现某个数据不对,但是你已提 ...
- 并发编程常用工具类(一) countDownLatch和cyclicBarrier的使用对比
1.CountDownLatch countDownLatch的作用是让一组线程等待其他线程完成工作以后在执行,相当于加强版的join(不懂可以百度一下join的用法),一般在初始 ...
- CACTI优化-流量接口统计total输入和输出流量数据
看图,没有优化前(没有显示流入和流出的总流量是多少): 优化后(有显示流入和流出总流量统计): 如何实现呢?本节就是处理的过程小结.第一步:登陆cacti管理平台进入控制台->模板->图形 ...
- jmeter的线程数,并发用户数,TPS,RPS 关系解说
背景 在做性能测试的时候,传统方式都是用并发虚拟用户数来衡量系统的性能(站在客户端视角),一般适用于一些网页站点例如首页.H5的压测:而RPS(Requests per second)模式主要是为了方 ...
- 关于redis搭建环境
首先,window键+r 输入cmd进入dos命名窗口,我的redis是装在了d盘,so我得输入cd:或者d:进入d盘,cd\redis文件夹路径,这样的话,直接输入 redis-server -- ...