当前位置:网站首页>Codeforces Global Round 19

Codeforces Global Round 19

2022-07-07 07:46:00 whitewall_ nine

B.

This competition is full of violence , I didn't expect the positive solution , For such a complex situation, there must be a conclusion that can make the topic simple . Yes A Speaking of, if it has been ordered , Then it must be no otherwise It indicates that there is a point a[i] > a[j], i < j Or let a[i] < a[j] i > j For the first case, it must be YES, For the second case, we can let j - 1 It is also natural and non reducing . Explain that if there is no order , It must be YES about B Consider greed , Because we need to make the value as big as possible , Let's divide into as many paragraphs as possible , That is to say 1 One number, one segment , except 0 The unexpected number did not contribute , Only 0 Yes, there will be 1 The contribution of . Then consider including 0 All interval numbers of are (i + 1) * (n - i) * (1 + (a[i] == 0));

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

#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int, int>
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fir first
#define pb push_back
#define sec second
#define sortall(x) sort((x).begin(),(x).end())
inline int read () {
	int x = 0, f = 0;
	char ch = getchar();
	while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return f?-x:x;
}
template<typename T> void print(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x >= 10) print(x/10);
	putchar(x % 10 + '0');
}
#define int long long
const int N = 2e5 + 10;
int n;
int f[N];
int a[N], b[N];
int sum;
void solve() {
    cin >> n;
    memset(f, 0, sizeof f);
    sum = 0;
 	for (int i = 1; i<= n ;i ++) {
 		cin >> a[i];
 		sum += a[i];
	 }
	 for (int i = 1; i<= n ;i ++) {
		cin >> b[i];
	 	sum += b[i];
	 }
	 f[0] = 1;
	 for (int i = 1; i<= n; i ++) {
	 	 int ndp[N] = {0};
		 for (int j = 0; j <= sum; j ++) {
	 		 if (f[j]) {
	 		 	ndp[j + a[i]] = ndp[j + b[i]] = 1;
			  }
		 }
		 memcpy(f, ndp,sizeof f);
	 }
	 int ans = sum * sum;
	 for (int i = 0; i <= sum; i ++) {
	     if (f[i])
	 	ans = min (ans, i * i + (sum - i) * (sum - i));
	 }
	 for (int i = 1; i <= n; i ++) {
	 	ans += (a[i] * a[i] + b[i] * b[i]) * (n - 2);
	 } 
	
	cout << ans << endl;
	
}
signed main () {
    int t;
    cin >> t;
    while (t --) solve();
    
}


01 knapsack problem , Judge which volume is feasible through backpack . Then the formula can be found as shown in the figure One part is unchanged , Then let a part take the minimum . This is a classic technique . Get the global minimum through traversal

 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[whitewall_ nine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130650037006.html