Codeforces 573D Bear and Cavalry

2 篇文章 0 订阅
2 篇文章 0 订阅

<span style="font-size: 1em; line-height: 1.4em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">Would you want to fight against bears riding horses? Me neither.</span>

Limak is a grizzly bear. He is general of the dreadful army of Bearland. The most important part of an army is cavalry of course.

Cavalry of Bearland consists of n warriors and n horses. i-th warrior has strength wi and i-th horse has strength hi. Warrior together with his horse is called a unit. Strength of a unit is equal to multiplied strengths of warrior and horse. Total strength of cavalry is equal to sum of strengths of all n units. Good assignment of warriors and horses makes cavalry truly powerful.

Initially, i-th warrior has i-th horse. You are given q queries. In each query two warriors swap their horses with each other.

General Limak must be ready for every possible situation. What if warriors weren't allowed to ride their own horses? After each query find the maximum possible strength of cavalry if we consider assignments of all warriors to all horses that no warrior is assigned to his own horse (it can be proven that for n ≥ 2 there is always at least one correct assignment).

Note that we can't leave a warrior without a horse.

Input

The first line contains two space-separated integers, n and q (2 ≤ n ≤ 30 0001 ≤ q ≤ 10 000).

The second line contains n space-separated integers, w1, w2, ..., wn (1 ≤ wi ≤ 106) — strengths of warriors.

The third line contains n space-separated integers, h1, h2, ..., hn (1 ≤ hi ≤ 106) — strengths of horses.

Next q lines describe queries. i-th of them contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi), indices of warriors who swap their horses with each other.

Output

Print q lines with answers to queries. In i-th line print the maximum possible strength of cavalry after first i queries.

Sample test(s)
input
4 2
1 10 100 1000
3 7 2 5
2 4
2 4
output
5732
7532
input
3 3
7 11 5
3 2 1
1 2
1 3
2 3
output
44
48
52
input
7 4
1 2 4 8 16 32 64
87 40 77 29 50 11 18
1 5
2 7
6 2
5 6
output
9315
9308
9315
9315
Note

Clarification for the first sample:

Warriors: 1 10 100 1000

Horses:   3  7  2    5 

After first query situation looks like the following:

Warriors: 1 10 100 1000

Horses:   3  5  2    7 

We can get 1·2 + 10·3 + 100·7 + 1000·5 = 5732 (note that no hussar takes his own horse in this assignment).

After second query we get back to initial situation and optimal assignment is 1·2 + 10·3 + 100·5 + 1000·7 = 7532.

Clarification for the second sample. After first query:

Warriors:  7 11 5

Horses:    2  3 1

Optimal assignment is 7·1 + 11·2 + 5·3 = 44.

Then after second query 7·3 + 11·2 + 5·1 = 48.

Finally 7·2 + 11·3 + 5·1 = 52.



题意:给你n个人n匹马,都有各自的价值,人骑马的价值之和等于人的价值乘以马的价值,唯一的要求是第i个人不能骑第i匹马。求最大价值。中间还有交换。

做法:假如没有第i个人不能骑第i匹马的限制的话。那么直接sort一发按顺序匹配就好了。但是有了这个限制之后。按顺序匹配的过程中就需要有交换。因为这个交换不会换到距离超过2的位置。所以直接暴力处理就好。把所有的状态用数字表示出来。建一个二维矩阵用线段树来搞。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <time.h>
#include <vector>
#include <stack>
using namespace std;
#define inf 99999999999999999LL
__int64 c[100000][6][6];
pair<__int64 ,int>war[33333],hor[33333];
int n,q,h[33333],rr[33333];
void run(__int64 a[6][6],int pos)
{
	__int64 e[6];
	int o=h[war[pos].second];
	e[1]=(pos>2&&o!=hor[pos-2].second)?war[pos].first*hor[pos-2].first:-inf;
	e[2]=(pos>1&&o!=hor[pos-1].second)?war[pos].first*hor[pos-1].first:-inf;
	e[3]=(pos>0&&o!=hor[pos].second)?war[pos].first*hor[pos].first:-inf;
	e[4]=(pos<n&&o!=hor[pos+1].second)?war[pos].first*hor[pos+1].first:-inf;
	e[5]=(pos<n-1&&o!=hor[pos+2].second)?war[pos].first*hor[pos+2].first:-inf;
	for(int i=0;i<6;i++)
		for(int j=0;j<6;j++)
			a[i][j]=-inf;
	a[0][0]=e[3]; a[0][1]=e[4]; a[0][2]=e[5];
	a[1][0]=e[2]; a[1][3]=e[4]; a[1][4]=e[5];
	a[2][1]=e[2]; a[2][3]=e[3]; a[2][5]=e[5];
	a[3][0]=e[1]; a[4][1]=e[1]; a[5][3]=e[1];
	
}
void push_up(__int64 a[6][6],__int64 b[6][6],__int64 d[6][6])
{
	for(int i=0;i<6;i++)
		for(int j=0;j<6;j++)
			a[i][j]=-inf;
	for(int i=0;i<6;i++)
		for(int j=0;j<6;j++)
			for(int k=0;k<6;k++)
			{
				a[i][j]=max(a[i][j],b[i][k]+d[k][j]);
			}
}
void build(int t,int l,int r)
{
	int e[6];
	if(l==r)
	{
		run(c[t],l);
		return ;
	}
	int mid=l+r>>1;
	build(t<<1,l,mid);
	build(t<<1|1,mid+1,r);
	push_up(c[t],c[t<<1],c[t<<1|1]);
}
void update(int t,int l,int r,int pos)
{
	if(l==r)
	{
		run(c[t],l);
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid)update(t<<1,l,mid,pos);
	else update(t<<1|1,mid+1,r,pos);
	push_up(c[t],c[t<<1],c[t<<1|1]);
}
int main()
{
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&war[i].first);
		war[i].second=i;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&hor[i].first);
		hor[i].second=i;
		h[i]=i;
	}
	sort(war+1,war+1+n);
	sort(hor+1,hor+1+n);
	for(int i=1;i<=n;i++)
	{
		rr[war[i].second]=i;
	}
	build(1,1,n);
	for(int i=1;i<=q;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		swap(h[u],h[v]);
		if(u!=v)
		{
			update(1,1,n,rr[u]);
			update(1,1,n,rr[v]);
		}
		printf("%I64d\n",c[1][0][0]);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值