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]);
	}
}



深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值