CodeForces500c - New Year Book Reading
2017-12-09 17:42
381 查看
Description
New Year is coming, and Jaehyun decided to read many books during2015, 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 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 beachieved by rearranging the order of stacked books.
Sample Input
3 51 2 3
1 3 2 3 1
Sample Output
12Note
Here’s a picture depicting the example. Each vertical column presentsthe stacked books.
Explanation
对于这个样例,我们可以从后面往前面看最后一个是1,1前面是3,那么将3放在1上面,3前面是2,那么将2放在3上面,
现在的序列是2,3,1,我们继续往前看,2前面是3,那么将之前的3拿走并放在2上面,
3前面是1,则将之前的1拿走并放在3上面,则现在的序列为1,3,2即为调整后的书的序列,
然后根据拿书的顺序模拟拿书操作进行加和即可
Code
#include <stdio.h> #include <string.h> #define maxn 1005 int book[maxn]; //当前书的序列 int day[maxn]; //day[i]为第i天要读的书 int weigh[maxn]; //书的weight int vis[maxn]; //初始book数组时用到的标记数组 int main() { int n,m; int now = 0; long long sum_weight = 0; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); for(int i = 0; i < n; i++) scanf("%d", &weigh[i]); for(int i = 0; i < m; i++){ scanf("%d", &day[i]); day[i] --;//使书的编号从0开始 } for(int i = 0; i < m; i++){ if(!vis[day[i]]){ book[now++] = day[i]; vis[day[i]]++; //对书的序列初始化以使sum_weight最小 } } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(book[j] != day[i]){ sum_weight += weigh[book[j]]; } else{ for(int k = j - 1; k >= 0; k--){ book[k+1] = book[k]; //其上面的书下移 } book[0] = day[i]; break; } } } printf("%lld\n", sum_weight); return 0; }
相关文章推荐
- 【Good Bye 2014C】【脑洞】New Year Book Reading 书放一摞依次看 最佳顺序使费用最小
- CodeForces 500 C. New Year Book Reading(贪心)
- codeforces 500C New Year Book Reading (贪心,很好的思维题)
- CF 500C New Year Book Reading
- CODE FORCE 500C (New Year Book Reading) (贪心or 模拟)
- Codeforces 500C New Year Book Reading
- C. New Year Book Reading
- C. New Year Book Reading
- CF~Good Bye 2014 C. New Year Book Reading
- codeforces 500C New Year Book Reading(取书)
- 【codeforces】New Year Book Reading
- Codeforces 500C:New Year Book Reading(思维+贪心?)
- cf (dfs,floyd,并查集). New Year Book Reading
- 【Goodbye2014】Codeforces 500C New Year Book Reading【贪心+模拟】
- codeforces New Year Book Reading
- Codeforces 500C New Year Book Reading
- CodeForces 500C New Year Book Reading(贪心+思维)
- Codeforces 500C New Year Book Reading
- codeforces 500C New Year Book Reading
- 【CodeForces】500C - New Year Book Reading(贪心)