Codeforces 500c-new Year Book Reading (greedy) __ greedy algorithm

Source: Internet
Author: User

Click to open the topic

C. New Year Book Reading time limit/test 2 seconds memory limit per test 256 megabytes input standard input output STA Ndard output

New year are 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) is WI.

As Jaehyun ' s house isn't large enough to have a bookshelf, he keeps the n books by stacking them. When he wants to read a certain book x, he follows the steps described below. He lifts 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, his puts book X in the top of the stack.

He decided to read the for M days. In the j-th (1≤j≤m) Day, he'll read the book this is numbered with integer BJ (1≤bj≤n). To read the book, he has 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 "the total weight to books he should lift during M" would be too heavy. So, he decided the order of the "stacked books before" The New year comes, and minimize the total weight. You may assume so books can is stacked in any possible order. Note This book, which is going to read on certain step isn ' t considered as lifted on the that step. Can you help him? Input

The "I" contains two space-separated integers n (2≤n≤500) and M (1≤m≤1000)-the number of books, and the N Umber of 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 this he would read. Note This can read the same book more than once. Output

Print the minimum total weight the should lift, which can is 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.




Yesterday thought for a long time did not think out the solution. Today, it suddenly occurred to me to start looking forward from the last day, like the example:

3 5
1 2 3 1 3 2 3 1
We look backwards, 1 3 2 3 1 (this data is a bit special, is a palindrome), we first put 1 on the top, now there's only one 1, and then 3 on 1. (This is considered clear, because 3 in 1 before, so no matter 3 where to take 1 at the time 3 must still be above, so put 3 on the top is certainly the most convenient), And then put 2 on the 3--now the sequence is 2 3 1--then we go on, down is 3, we take 3 out, then put on 2 (now the sequence into 3 2 1), and finally 1, we then remove 1 to put on 3, then the final sequence is: 1 3 2.


This is a greedy idea is correct, but it is time-consuming to achieve. And then this idea will find: the first occurrence of the number on the top of the line, such as 1 1 1 1 3 3 2 3, the final sequence is 1 3 2. Then a for loop can be sorted out in order, with a vector to maintain the line.


This is the process of thinking about my problem solving.


The code is as follows:

#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
int main ()
{
	int n,m;
	int w[500+11];
	int day[1011];
	BOOL vis[511];		Record whether the book is well placed in the 
	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;
}
  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.