CF 500 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
题意:有n本书,编号为1~n,每一本书的重量为w[i],这堆书竖直堆在一起
现在有m天,第i天会从中拿出编号为day[i]的书看,代价是day[i]上面的书的总重量,书一天看完,看完后放在最上面。
问:最开始的时候你按照什么顺序放书,会让你这m天要搬的书的总重量最小。
贪心可知,拿每一本书第一次出现的序列作为初始序列,没有出现的序列放在最下面,不用管了。
用一个vector存放这个序列,看完后放在最上面删除,插入到最上面。
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream> using namespace std; #define ll long long
#define pb push_back bool vis[];
int w[];
int day[];
vector<int>v; int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&w[i]);
}
memset(vis,false,sizeof vis);
for(int i=;i<=m;i++){
scanf("%d",&day[i]);
if(!vis[day[i]]){
v.pb(day[i]);
vis[day[i]]=true;
}
}
int ans=;
for(int i=;i<=m;i++){
int j=;
for(;v[j]!=day[i];j++){
ans+=(ll)(w[v[j]]);
}
v.erase(v.begin()+j);
v.insert(v.begin(),day[i]);
} printf("%d\n",ans); return ;
}
CF 500 C. New Year Book Reading 贪心 简单题的更多相关文章
- cf 605A Sorting Railway Cars 贪心 简单题
其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...
- POJ 2393 贪心 简单题
有一家生产酸奶的公司,连续n周,每周需要出货numi的单位,已经知道每一周生产单位酸奶的价格ci,并且,酸奶可以提前生产,但是存储费用是一周一单位s费用,问最少的花费. 对于要出货的酸奶,要不这一周生 ...
- HDU - 5999 The Third Cup is Free 贪心 简单题
The Third Cup is Free Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- POJ 2260 Error Correction 模拟 贪心 简单题
Error Correction Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6825 Accepted: 4289 ...
- hdu2187悼念512汶川大地震遇难同胞——老人是真饿了(贪心 简单题)
传送门 简单题 #include<bits/stdc++.h> using namespace std; struct node { double dan,weight; }a[]; bo ...
- cf 620C Pearls in a Row(贪心)
d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3 ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- CF #296 (Div. 1) B. Clique Problem 贪心(构造)
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- 二叉树遍历 空间复杂度为O(1)
http://blog.csdn.net/mxw976235955/article/details/39829973 http://www.tuicool.com/articles/zA7NJbj / ...
- 【NOI2011】【P1308】道路修建
这题也太水了吧,为什么不是我这届的NOI(╯‵□′)╯︵┻━┻ 原题: 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都 ...
- 黑马程序员——JAVA基础之简述设计模式
------- android培训.java培训.期待与您交流! ---------- 设计模式(Design Patterns) 设计模式(Design pattern)是一套被反复使用.多数人知晓 ...
- js中arguments,caller,callee,apply的用法小结
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <style typ ...
- jquery下ie的margin-left ----bug 以及parseInt方法bug
ie下使用jquery的方法css('margin-left')可能会出现'auto'----从而使结果不可计算,即便使用parseInt()方法也不行 因为parseInt()方法的bug是如果参数 ...
- Win10系统下安装Tensorflow
TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,具备良好的灵活性和拓展性. 过去TensorFlow只支持Linux和Mac OS,而没有提供windows的支持. ...
- SDWebImage使用详解
这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征.使用示范的代码:UITableView使用UIImageView+ ...
- 解决outlook不能显示鼠标问题
今天发现打开outlook2010后, 没有鼠标显示. 解决方案: Control Panel -> Mouse Settings ->Pointer Options Uncheck th ...
- 使用bind(this)的情况
1.setInterval().setTimeout()的回调函数,一定要加.bind(this)方法. 原因是:在setInterval()中定义的回调函数,是在同步代码执行完后,随着事件触发来异步 ...
- android 为应用程序创建桌面快捷方式技巧分享
手机装的软件过多,找起来很不方便,所以在主页面有一个快捷方式的话会很不错的,本文将介绍如何实现,需要了解跟多的朋友可以参考下 我们开发一款软件后,如果手机装的软件过多,去翻的话会很难翻的,所以 ...