cf500C 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 aslifted 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.

题意是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 0x7ffffff
using 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;
}
cf500C 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 ...
随机推荐
- Android 布局 ViewGroup
布局 res/layout 命名规则(全部小写) activity_ fragment_ item_ 基础组件 com.android.widget包下 父类View view:屏幕上一块矩阵区域 能 ...
- android ViewPager具体解释
Viewpager 在android界面布局中属于经常使用类型 ,它能够做导航,页面菜单,进入软件是的欢迎界面 等等.比方今最流行的几款手机软件 ,QQ,微信,微博 等 ,其主界面 都用到了View ...
- java 中读取本地文件中字符
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- TinyXml快速入门(二)
在<TinyXml快速入门(一)>中我介绍了使用TinyXml库如何创建和打印xml文件,下面我介绍使用tinyxml库对xml文件进行一系列的操作,包括获取xml文件声明,查询指定节点. ...
- EventBus 事件总线 案例
简介 地址:https://github.com/greenrobot/EventBus EventBus是一个[发布 / 订阅]的事件总线.简单点说,就是两人[约定]好怎么通信,一人发布消息,另外一 ...
- 用CSS边框图像让你的网站更漂亮
不久之前,添加一些装饰性元素,例如给网页中的图片添加花哨的边,以及耐心调整CSS文件才能使你的网页看起来不错.然而现在CSS已经做出了改变,用复杂的边框装饰你的网站只需几行代码.这篇文章将告诉你如何做 ...
- [译]一个灵活的 Trello 敏捷工作流
[译]一个灵活的 Trello 敏捷工作流 翻译自 An Agile Trello Workflow That Keeps Tasks Flexible Getting things done 可不只 ...
- Swift - 36 - 结尾闭包(Trailing closure)和捕获数值(Capturing Values)的简单介绍
//: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...
- CSS 解决<td>里面内容太多把表格弄变形的原因,设置 自动换行。
1 < style="word-break:break-all"> 例如div宽200px,它的内容就会到200px自动换行,如果该行末端有个英文单词很长(congra ...
- c 中可变参数的实现
我们在C语言编程中有时会遇到一些参数个数可变的函数,例如printf()函数,其函数原型为: 例一: int printf( const char* format, ... ...