点击打开题目
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.
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 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.
Examples
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.
昨天想了半天没有想出来解法。今天突然想到从最后一天开始往前找,比如样例:
3 5
1 2 3
1 3 2 3 1
我们倒着看,1 3 2 3 1(这个数据有点特殊,是个回文的),我们先把1放在最上面,现在只有一本1,然后把3放在1上面(这一点考虑清楚,由于3在1前面,所以不管3在哪里取1的时候3肯定还在上面,所以把3放在最上面肯定最省事),然后把2放在3上面——现在的序列为2 3 1——然后我们继续,下来又是3,我们把3拿出来,再放在2上面(现在序列变为3 2 1),最后是1,我们再把1取出来放在3上面,则最终序列为:1 3 2。
这一个贪心思路是正确的,但是实现起来很费时间啊。然后照这个思路会发现:第一个出现的数字就放在最上面就行了,比如 1 1 1 1 3 3 2 3 ,最终的序列为1 3 2。那么一个for循环就可以把顺序整理好,用一个vector维护就行了。
这是我解题的思考的过程。
代码如下:
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
int n,m;
int w[500+11];
int day[1011];
bool vis[511]; //记录该书是否放好
CLR(vis,false);
vector<int> L;
scanf ("%d %d",&n,&m);
for (int i = 1 ; i <= n ; i++)
scanf ("%d",&w[i]);
for (int i = 1 ; i <= m ; i++)
{
scanf ("%d",&day[i]);
if (!vis[day[i]])
{
vis[day[i]] = true;
L.push_back(day[i]);
}
}
int ans = 0;
for (int i = 1 ; i <= m ; i++)
{
for (int j = 0 ; j < L.size() ; j++)
{
if (day[i] == L[j])
{
L.erase(L.begin()+j);
L.insert(L.begin(),day[i]);
break;
}
else
ans += w[L[j]];
}
}
printf ("%d\n",ans);
return 0;
}