CodeForces 500C New Year Book Reading (贪心)

题意:给出n本书的重量,以及计划要看的书的顺序,一开始书按某种未知顺序垒好,要看哪本书,需要将这本书上面的其他书拿起来,代价为这些书的总重量,看完再将这本书放在最上面,求看完所有书的最小总代价。

题解:贪心
先考虑最先摆放顺序。
对于之前还未出现的书,前面出现的必然在当前书上面,那么最小代价就是在之前的下面。故而顺序就是每本书第一次出现的顺序,然后正常模拟即可。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
const int maxn = 1e3 + 5;
int n, m, w[maxn], b[maxn];
set<int> s;
int main() {
	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]);
	int ans = 0;
	for (int i = 2; i <= m; i++) {
		s.clear();
		for (int j = i - 1; j >= 1; j--) {
			if (b[i] == b[j]) break;
			s.insert(b[j]);
		}
		set<int>::iterator it = s.begin();
		while (it != s.end()) ans += w[*it++];
	}
	printf("%d\n", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值