2021牛客多校1 G Game of Swapping Numbers

131 篇文章 0 订阅

G Game of Swapping Numbers

链接:https://ac.nowcoder.com/acm/contest/11166/G
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Given two arrays A, B with length N, you should perform exactly K operations in array A.

For each operation, choose 2 elements Ai,Aj(1≤i<j≤N)A_i, A_j(1 \leq i < j \leq N)Ai​,Aj​(1≤i<j≤N) and swap the positions of AiA_iAi​ and AjA_jAj​.

Please maximize ∑i=1n∣Ai−Bi∣\sum_{i = 1}^{n}|A_i - B_i|∑i=1n​∣Ai​−Bi​∣.

输入描述:

The first line of input contains two integers N,K(2≤N≤5×105,0≤K≤108)N, K(2 \leq N \leq 5 \times 10^5, 0 \leq K \leq 10^8)N,K(2≤N≤5×105,0≤K≤108), describing the length of A, B and number of operations.

The second line contains N integers A1,...,AN(−108≤Ai≤108)A_1,...,A_N(-10^8 \leq A_i \leq 10^8)A1,...,AN(−108≤Ai≤108).

The third line contains N integers B1,...,BN(−108≤Bi≤108)B_1,...,B_N(-10^8 \leq B_i \leq 10^8)B1,...,BN(−108≤Bi≤108).

输出描述:

Output one integer, the maximum answer.

示例1

输入

[复制](javascript:void(0)😉

3 2
1 2 3
3 2 1

输出

[复制](javascript:void(0)😉

4

示例2

输入

[复制](javascript:void(0)😉

3 2
1 2 3
1 2 3

输出

[复制](javascript:void(0)😉

4

示例3

输入

[复制](javascript:void(0)😉

3 1
1 2 3
3 2 1

输出

[复制](javascript:void(0)😉

4

思路与代码

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
例如:

A 0 3 
B 1 2

我们应该变成

 A 3 0    
 B 1 2

这样的差才大
结果是3-1+2-0
即给3和2分配正号,1分配负号(较大的数获得正号)
相当于A的负数和B的正数或者A的正数和B的负数进行匹配,只要最后正负的数量都是n就行

假设我们能任意指定k来求最优解,相当于是把AB合在一起排序,取最大的n个填正号,最小的n个填符号即可。
图片中的最少步数得到最优解很清晰了

图中结论的话,例如

A 0 3 5
B 1 1 2

符号表示

A -0 3 5
B -1 -1 2

假设k=3
那么2和-1换,1次
那么-1和-1可以一直换下去,而n=2需要特判

假设考虑四个数a>b>c>d
a b -c -d才是正确的,但是如果原来的排位是a -b c -d,那么就需要交换
假设A,B数组对应的是(a1,b1)和(a2,b2),那么满足min(a1,b1)>max(a2,b2)或者min(a2,b2)>max(a1,b1)的就需要交换
我们可以这样理解

A 4 2
B 3 1

这样子是需要交换的
需要交换成

A 2 4
B 3 1

a b c d
4 3 2 1
原来是4-3+2-1,即4和3是一对,2和1是一对

4---3
        2---1
变成
4-----------1
    3---2

再结合一下min(a1,b1)>max(a2,b2)看看,这里min(a1,b1)是3,max(a2,b2)是2,满足,那么交换之后比原来的值多了2*(min(a1,b1)-max(a2,b2))(从图中可以明显的看出来)
所以,回归题目,只需要k次操作,使得2*(min(a1,b1)-max(a2,b2))尽可能的大就可以了,即满足min(a1,b1)>max(a2,b2)min(a1,b1)尽可能大,max(a2,b2)尽可能小

记得开long long

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)    for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define sc          ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
    (void)(cout << "L" << __LINE__ \
    << ": " << #x << " = " << (x) << '\n')
#define TIE \
    cin.tie(0);cout.tie(0);\
    ios::sync_with_stdio(false);

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 2000;
const int N = 500009;


int a[N],b[N],c[N],d[N]; 

bool cmp(ll a,ll b){
	return a>b;
}

void solve(){
	ll n,k,ans;
	while(cin>>n>>k){
		ans = 0;
		for(ll i=1;i<=n;i++)cin>>a[i];
		for(ll i=1;i<=n;i++){
			cin>>b[i];
			c[i] = min(a[i],b[i]);
			d[i] = max(a[i],b[i]);
			ans+=abs(a[i]-b[i]);
		}
		if(n==2){//n=2的时候特判一下 
			if(k%2==0){//换偶数次的就不用管了,反正都是换回来 
				cout<<ans<<endl;
			}else{//直接输出
				cout<<abs(a[1]-b[2])+abs(a[2]-b[1])<<endl;
			}
			continue;
		}else{
			sort(c+1,c+1+n,cmp);
			sort(d+1,d+1+n);
			for(ll i=1;i<=k;i++){
				if(c[i]<d[i]){
					break;
				}
				ans+=2*(c[i]-d[i]);
			}
			cout<<ans<<endl;
		}
	}
}

int main()
{
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
	solve();
//    sc{solve();}
//    sc{cout<<"Case "<<Q<<":"<<endl;solve();}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值