Stack of Presents(二分)

Santa has to send presents to the kids. He has a large stack of nn presents, numbered from 11 to nn; the topmost present has number a1a1, the next present is a2a2, and so on; the bottom present has number anan. All numbers are distinct.

Santa has a list of mm distinct presents he has to send: b1b1, b2b2, …, bmbm. He will send them in the order they appear in the list.

To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are kk presents above the present Santa wants to send, it takes him 2k+12k+1 seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).

What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.

Your program has to answer tt different test cases.

Input
The first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

Then the test cases follow, each represented by three lines.

The first line contains two integers nn and mm (1≤m≤n≤1051≤m≤n≤105) — the number of presents in the stack and the number of presents Santa wants to send, respectively.

The second line contains nn integers a1a1, a2a2, …, anan (1≤ai≤n1≤ai≤n, all aiai are unique) — the order of presents in the stack.

The third line contains mm integers b1b1, b2b2, …, bmbm (1≤bi≤n1≤bi≤n, all bibi are unique) — the ordered list of presents Santa has to send.

The sum of nn over all test cases does not exceed 105105.

Output
For each test case print one integer — the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.

Example
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
思路:我们每拿出一个新的礼物,就要判断一下在这个范围内能不能拿出更多的礼物,因为在这种情况下拿出礼物的是时间是1。二分找到这个礼物的位置,然后再计算就好了。注意要用long long。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
struct node{
	int pos;
	int num;
	bool operator <(const node &a)const{
		return num<a.num;
	}
}p[maxx];
int a[maxx],b[maxx];
int n,m;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) scanf("%d",&p[i].num),p[i].pos=i;
		for(int i=1;i<=m;i++) scanf("%d",&a[i]);
		sort(p+1,p+1+n);
		ll ans=0;
		int j;
		for(int i=1;i<=n;i++) b[i]=p[i].num;
		for(int i=1;i<=m;)
		{
			int pos=lower_bound(b+1,b+1+n,a[i])-b;
			pos=p[pos].pos;
			ans+=(2ll*((ll)pos-i)+1ll);
			for(j=i+1;j<=m;j++)
			{
				int pos1=lower_bound(b+1,b+1+n,a[j])-b;
				pos1=p[pos1].pos;
				if(pos1<pos) ans+=1ll;
				else break;
			}
			i=j;
		}
		cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值