1. 程式人生 > 實用技巧 >Codeforces Round #695 (Div. 2) C. Three Bags

Codeforces Round #695 (Div. 2) C. Three Bags

題目連結:https://codeforces.ml/contest/1467/problem/C

題意:有三個揹包,裡面各自裝了數, 每次可以選擇任意兩個非空揹包,取其中一個數a和b 然後變成

a-b 放回 a揹包中,b揹包中的該數消失 直到最後只剩下一個數, 問剩下的數最大為多少

思路:考慮最後所有的數都會歸到一個數的身上,這個數 給的貢獻不是正數就是負數,取決於中間走了幾步

奇數為負數,偶數為正數, 所有最後的答案是 總的sum-2*sub , ×2是為了抵消掉負數加進去sum的貢獻

那麼就只有幾種情況, 要麼是某兩個揹包的最小的數來作為跳板, 要麼是隻用某一個揹包的最小數來作為跳板變成正數

然後 這一個揹包的數 全部當作負數貢獻

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=3e5+10;
 4 const int mod=1e8;
 5 #define ll long long
 6 #define pb push_back
 7 ll a[maxn],b[maxn],c[maxn];
 8 
 9 
10 
11 int main()
12 {
13     ios::sync_with_stdio(0);
14     cin.tie(0);
15     int n1,n2,n3;
16     cin>>n1>>n2>>n3;
17 ll sum1=0,sum2=0,sum3=0; 18 for(int i=1;i<=n1;i++) 19 cin>>a[i],sum1+=a[i]; 20 for(int i=1;i<=n2;i++) 21 cin>>b[i],sum2+=b[i]; 22 for(int i=1;i<=n3;i++) 23 cin>>c[i],sum3+=c[i]; 24 ll ans=0; 25 sort(a+1,a+1+n1); 26 sort(b+1
,b+1+n2); 27 sort(c+1,c+1+n3); 28 ll min1=1e18; 29 ans=sum1+sum2+sum3; 30 min1=min({sum1,sum2,sum3}); 31 min1=min({min1,a[1]+b[1],a[1]+c[1],b[1]+c[1]}); 32 ans-=min1*2; 33 cout<<ans<<'\n'; 34 35 36 37 38 39 40 }
View Code