Three Bags CodeForces - 1467C

Title:

For three piles of stones, you can take two piles of stones, one stone a and one b. Then eliminate a so that b=ba and put it in the pile of b. Do this until there is only one pebble left, and find the pebble the most valuable.

answer:

Construction questions
can construct both situations:

  1. Two of the piles are positive and one pile is negative
  2. One backpack is positive, the minimum of the other two backpacks is negative, and the others are positive.
    Why is it constructed like this?
    Reference problem solution
    For a number, if an odd number of operations are performed, then it is a negative contribution, and an even number of operations is a positive contribution.
    Assuming that the last number is in array a, then there are n-1 numbers left in a, which can be combined with other arrays at the end. Combine it with the last number, so it's all positive contributions. The same applies to the arrays of b and c, which is equivalent to the negative contribution of only one number in b and c, and the others are positive. We take b, the smallest of c is the negative contribution
    or we directly choose b and one of the arrays in c is positive Array, the other is a negative array

Code:

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

const int maxn = 300010;

int n1, n2, n3;
ll a[maxn], b[maxn], c[maxn];
ll s1, s2, s3;
ll m1, m2, m3;

ll ans = 0;

ll read(){
    
     ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){
    
     if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){
    
     s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
    
    
	n1 = read(), n2 = read(), n3 = read();
	for(int i = 1 ; i <= n1 ; ++i) a[i] = read(), s1 += a[i];
	for(int i = 1 ; i <= n2 ; ++i) b[i] = read(), s2 += b[i];
	for(int i = 1 ; i <= n3 ; ++i) c[i] = read(), s3 += c[i];
	
	ans = -1e18;

	ans = max(ans, s1 + s2 - s3); 
	ans = max(ans, s2 + s3 - s1);
	ans = max(ans, s3 + s1 - s2);
	
	m1 = a[1], m2 = b[1], m3 = c[1];
	
	for(int i = 2 ; i <= n1 ; ++i) m1 = min(m1, a[i]);
	for(int i = 2 ; i <= n2 ; ++i) m2 = min(m2, b[i]);
	for(int i = 2 ; i <= n3 ; ++i) m3 = min(m3, c[i]);
	
	ans = max(ans, s1 + s2 + s3 - 2 * m1 - 2 * m2);
	ans = max(ans, s1 + s2 + s3 - 2 * m1 - 2 * m3);
	ans = max(ans, s1 + s2 + s3 - 2 * m2 - 2 * m3);
	
	printf("%lld\n", ans);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_35975367/article/details/114096741