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 ...
随机推荐
- PHP强制清除缓存
在页面最顶端加上 <?phpheader("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Last-Modifi ...
- poj2492 带权并查集
题意:研究一种生物,有n个生物个体,发现有一些之间进行了交配,给出了这些关系,问是否有同性恋的bug出现. 用0\1表示某元素和其祖先元素的性别关系,0 为相同,1 为不同,用 mod2 实现累计处理 ...
- Compute Mean Value of Train and Test Dataset of Caltech-256 dataset in matlab code
Compute Mean Value of Train and Test Dataset of Caltech-256 dataset in matlab code clc;imPath = '/ho ...
- 取得某个数组前key大 PHP实现
<?php function max_key($arr, $key) { $tmp_key = $key; $max_arr = array(); while($tmp_key--) $max_ ...
- 关于CSS Hack
CSS Hack由于不同厂商的浏览器,如Internet Explorer,Safari,Mozilla Firefox,Chrome 等,或者是同一厂商的浏览器的不同版本,如IE6和IE7,对CSS ...
- 华东师大OJ:IP Address【IP地址转换】
/*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...
- Zookeeper工作原理一
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...
- XMLHttpRequest 对象
XMLHttpRequest 对象 XML XSLTXML 解析器XMLHttpRequest 对象用于在后台与服务器交换数据. 什么是 XMLHttpRequest 对象? XMLHttpReque ...
- TKinter之输入框
输入框是 Entry,应用程序要取得用户的信息,输入框是必不可少的. 输入框比较重要的一个函数: get:返回值即输入框的内容 比如e是一个输入框,e['show']='*'就变成了密码框 小例子:用 ...
- .NET中的Timer类型用法详解
这篇文章主要介绍了.NET中的Timer类型用法,较为详细的分析了Timer类型在各种环境下的用法,需要的朋友可以参考下 在.NET FrameWork中有多个Timer,那么怎么根据实际情况进行 ...