codeforces 500C New Year Book Reading (贪心,很好的思维题)

C. New Year Book Reading

time limit per test

2 seconds

memory limit per test

256 megabytes

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.

  1. He lifts all the books above book
    x.
  2. He pushes book x out of the stack.
  3. He puts down the lifted books without changing their order.
  4. 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 be achieved by rearranging the order of stacked books.

Sample test(s)

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.

题目链接:http://codeforces.com/problemset/problem/500/C

题目大意:一个人要读n本书每本书有个重量wi,要读m天,每天读1本,他每天把要看的书抽出来(把上面的搬开,拿出要读的书再把上面的书放回去),看完以后放到一摞书的最上面,问根据他的阅读顺序怎样初始化书的排列顺序能使他搬书的重量最小,求出这个最小重量

题目分析:yy出奇迹,就拿每本书第一次出现的序列作为最终的序列然后按顺序模拟一下,后来仔细思考了一下理解了这样做的正确性,其实也挺好理解的,先看一个序列1 1 1 1 1 1 1 2,对于这个序列,我们很明显的策略是按1 2来排,因为看过以后放在最上面,所以每次读1都不用搬书,对于一个阅读序列的子序列a b,我们有两种排法,按a b排代价是w[b],按b a排代价是w[a] + w[b],而且要明确的一点是确定了顺序,代价也就随之确定了,我们再将这个性质推广到一般得出贪心结论:拿每本书第一次出现的顺序作为初始序列,比如1
1 3 1 1 3 2 1 1

按此策略得到的初始序列为1 3 2,不管初始序列是什么样的,读完 1 1 3以后的书的顺序肯定都是确定的都为3 1 2,但是只有初始序列为1 3 2时得到3 1 2的代价最小,其实还是要自己理解理解~

#include <cstdio>
#include <cstring>
using namespace std;
int w[505], b[1005];
bool vis[505];
int main()
{
    int n, m, ans = 0;
    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", &b[i]);
    for(int i = 2; i <= m; i++)
    {
        memset(vis, false, sizeof(vis));
        for(int j = i - 1; j > 0; j--)
        {
            if(b[j] == b[i]) //只要找同一本书读到两次之间的书
                break;
            //对于其上面的书,我只关心它们的总重量,比如1 2 3 2 3 1
            //我只关心第二次读到1时它上面有哪些书和它们的重量即w[2] + w[3]
            //这里用个vis标记一下重复的即可
            if(!vis[b[j]])
            {
                ans += w[b[j]];
                vis[b[j]] = true;
            }
        }
    }
    printf("%d\n", ans);
}
时间: 2024-08-07 08:25:10

codeforces 500C New Year Book Reading (贪心,很好的思维题)的相关文章

Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增

Codeforces Round #480 (Div. 2) C. Posterized 题意: 给出 n 个数,都是区间 [0,255] 内的数,要你把 [0,255] 划分成多个长度 <=k 的不重叠的子区间.每个数必须包含在一个子区间内,且这个数的价值是这个子区间的左端点.要你输出这 n 数的价值,且这 n 个价值字典序要最小. tags: 首先很明显字典序最小,那对于第 i 个数 p[i] 定它的区间时,左端点肯定要尽可能小.所以我们直接枚举区间 [ p[i]-k+1, p[i] ] 定

CodeForces 1000B Light It Up(贪心、思维)

https://codeforces.com/problemset/problem/1000/B 题意: 一个模拟思维题.就是有一盏灯,0时刻开着.n次操作,你可以在其中加入一次操作(或者不加),操作为:a[i]时刻按一下开关,状态变为相反状态(开->关or关->开).问灯亮着的时长最长为多少? 样例一0~4开(时长为4-0),4~6关(时长为6-4),6~7开(时长为7-6),7~10关(时长为10-7),可以这这些时间点中加操作使开灯时间变长 在3处加操作,开灯时长变为0-3(开)3-4(

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces 442C Artem and Array(stack+贪心)

题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数,删除一个数的得分为两边数的最小值,如果左右有一边不存在则算作0分.问最大得分是多少. 解题思路:首先将连续的a,b,c,a > b && c > b的情况将c掉,获得min(a,b)分,这样处理后数组变成一个递増再递减的序列,除了最大和第二大的取不到,其他数字均可以得分. 样例:4 10 2 2 8 #include <cstdio> #include

Codeforces 18D Seller Bob java大数+贪心

题目链接:点击打开链接 java: import java.math.BigInteger; import java.util.Scanner; public class Main { static int N = 5005; static BigInteger[] er = new BigInteger[N]; static BigInteger E = new BigInteger("2"); static int[] a = new int[N]; static int[] ma

Codeforces 437C The Child and Toy(贪心)

题目连接:Codeforces 437C The Child and Toy 题目大意:孩子有一个玩具,有n个部件组成,m条绳子组成,每条绳子连接两个部件.小孩比较顽皮,要将玩具拆成不可分割的部件,每次剪断一条绳子的代价是该绳子连接的两个部件的权值中较小的值.问说最小的总代价是多少. 解题思路:以为每条边都是要被剪断的,所以将节点按照代价值从大到小排序,每次拿掉权值大的点,与该点连接并且还未剪断的边均用另外点的权值. #include <cstdio> #include <cstring

Codeforces 437D The Child and Zoo(贪心+并查集)

题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去参观动物园,动物园分很多个区,每个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么f值即为从一个区走到另一个区中所经过的路中权值最小的值做为权值.问,平均两个区之间移动的权值为多少. 解题思路:并查集+贪心.将所有的边按照权值排序,从最大的开始连接,每次连接时计算的次数为连接两块的节点数的积(乘法原理). #in

Codeforces 459E Pashmak and Graph(dp+贪心)

题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边按照权值排序,每次将相同权值的边同时加入,维护每个点作为终止点的最大长度即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 3

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i