关于随机化(悬关,先到先得)
查看原帖
关于随机化(悬关,先到先得)
1004336
Hope5365楼主2023/10/16 21:32

蒟蒻做的CSPJ第二轮模拟赛,第三题总是没过,求条!(悬关,先到先得,情有价值)

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2e5 + 10;
int t,a[N],s[N];

int read()
{
	int g;
	cin >> g;
	return g;
}

void func()
{
	int n;
	cin >> n;
	for(int i = 1;i <= n;i ++) int x = read();
	for(int i = 1;i <= n;i ++) a[i] = read(),s[i] = i;
	int ans = 0;
	for(int _ = 1;_ <= 1000;_ ++)
	{
		int x = 0;
		random_shuffle(s + 1,s + n + 1);
		for(int i = 1;i <= n;i ++)
		{
			if (s[i] > i) x -= a[i];
			else if (s[i] < i) x += a[i];
		}
		ans = max(ans,x);
	}
	cout << ans << endl;
}

int main()
{
	ios::sync_with_stdio(0);
	cin >> t;
	while (t --) func();
	return 0;
}
2023/10/16 21:32
加载中...