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文件时出错.
随机推荐
- 【MySQL 基础】MySQ LeetCode
MySQL LeetCode 175. 组合两个表 题目描述 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+----- ...
- IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerp
[root@k8s-master ~]# scp /etc/sysctl.d/k8s.conf root@192.168.30.23:/etc/sysctl.d/k8s.conf@@@@@@@@@@@ ...
- 夯实基础系列一:Java 基础总结
前言 大学期间接触 Java 的时间也不短了,不论学习还是实习,都让我发觉基础的重要性.互联网发展太快了,各种框架各种技术更新迭代的速度非常快,可能你刚好掌握了一门技术的应用,它却已经走在淘汰的边缘了 ...
- 转 15 jmeter分布式性能测试
15 jmeter分布式性能测试 背景由于jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起Java内存溢出的错误.要解决这个问题, ...
- cisco交换机路由器静态路由配置
一.切换模式 router>en //用户模式enable router#conf t //特权模式 ...
- 解决 win10 无法安装VS2019,visual studio installer下载进度始终为0
解决 win10 无法安装VS2019,visual studio installer下载进度始终为0 目录 解决 win10 无法安装VS2019,visual studio installer下载 ...
- Intellij idea 报错:Error : java 不支持发行版本5
保证下面几个地方编译版本一致: 分两步: 第一步,进入Project Structure中设置Project JDK 以及Project Level ,高于JDK5版本 第二步,进入设置中将项目的JD ...
- What is the difference between Serialization and Marshaling?
How to serialize and deserialize JSON using C# - .NET | Microsoft Docs https://docs.microsoft.com/en ...
- Lua大量字符串拼接方式效率对比及原因分析
Lua大量字符串拼接方式效率对比及原因分析_AaronChan的博客-CSDN博客_lua字符串拼接消耗 https://blog.csdn.net/qq_26958473/article/detai ...
- SICP 解题集 — SICP 解题集 https://sicp.readthedocs.io/en/latest/
SICP 解题集 - SICP 解题集 https://sicp.readthedocs.io/en/latest/