New Year Book Reading

source

Description

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?

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 *w*1,\ *w*2, ..., *w**n* (1 ≤ *w**i* ≤ 100) — the weight of each book.
The third line contains m space separated integers * b * 1,  * b * 2, ..., *b**m* (1 ≤ *b**j* ≤ *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.

Example
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.


题意:Jaehyun决定读n本书,每本书的id都不一样,每本书都有它的重量。每次读特定id的书时,Jaehyun 先搬起位于它前面的书,然后取出该书,剩下的书的位置不变,再放下搬起的书,读完后把该书放到书堆的顶端。求怎么安排初始的书堆顺序,使得Jaehyun 要搬起的书的重量最小,输出最小重量。

题解:假设前7本书的阅读顺序仅仅出现书A,B,C,而第8本书首次出现书D;则对于第8本书:书D而言,无论前7本选择怎样的阅读顺序,例如顺序为ABCABAA或CABBCBB等等,到阅读第8本书D的时候,都要搬起书A,B,C。由此猜想,书堆的摆放顺序应该是:书的阅读顺序中首次出现的书应该越接近书堆顶。

栈模拟版本

#include<cstdio>
#include<iterator>
#include<set>
#include<list>
using namespace std;
int main()
{
        int n,m,temp,sum=0;
        scanf("%d%d",&n,&m);
        int *wight=new int[n+1];
        int *step=new int [m+1];
        for(int i=1;i<=n;i++)
        {
            scanf("%d",wight+i);
        }
        list<int> mystack;
        set<int> s;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&temp);
            step[i]=temp;
            if(s.find(temp)==s.end())
            {
                mystack.push_front(temp);
                s.insert(temp);
            }
        }
        list<int> sta;
        while(!mystack.empty())
        {
            sta.push_front(mystack.front());
            mystack.pop_front();
        }
        for(int i=1;i<=m;i++)
        {
            for(list<int>::iterator it=sta.begin();;it++)
            {
                if(*it==step[i]){
                    sta.erase(it);
                    sta.push_front(step[i]);
                    break;
                }
                else{
                sum+=wight[*it];
                }

            }
        }
        printf("%d\n",sum);
}

不用构造栈的版本

#include<cstdio>
#include<string.h>
using namespace std;
int wight[510];
int book[1010];
bool vis[510];
int main()
{
        int n,m,sum=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",wight+i);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",book+i);
        }
        for(int i=2;i<=m;i++)
        {
            memset(vis,false,sizeof(vis));
            for(int j=i-1;j>0;j--)
            {
                if(book[j]==book[i]) break;
                if(!vis[book[j]])
                {
                    sum+=wight[book[j]];
                    vis[book[j]]=true;
                }
            }
        }
        printf("%d\n",sum);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,793评论 0 23
  • 那天的雨下了一整天,直到晚上仍在持续。回到学校后,我和十末都变得沉默。就连应清笑我们两个淋成了落汤鸡,也无人再去反...
    灯火如初阅读 214评论 0 0
  • 随笔,仅个人见解 现在像我这样到了适婚年龄的人大多都会纠结爱情,婚姻和现实的问题。 个人认为,爱情是理想的,而婚姻...
    Jasmine楠楠阅读 550评论 1 0
  • 在这样的生活里面,因为是我们年轻吗?所以,仿佛对自己的喜恶比较在意,比较注重,没有那么多的想法,想怎么样就怎么样,...
    凉生Vero阅读 301评论 0 0
  • 这两天做系统,需要用到让模型自动旋转从而达到展示功能,问了很多人,具体实现发现蓝图其实很简单。 其中一大神提供了一...
    许阿渝阅读 9,820评论 0 1