题意:给出m和一个长度为2m的数组a,令数组b长度也为m,且对于b任意一个长度为m的子序列的积等于剩下的和,求出最小的Σ|a[i]-b[i]|
Solution
显然只有一下几种情况:
1.m=1时,a[1]=a[2]
2.m=2是,4个数均为2,
3.m>2且m为偶数,有2n-1个-1和1个m
PS:这里m>=3的奇数不行,被坑了
1 void solve() 2 { 3 int n;cin>>n; 4 int sum1=0,sum2=0; 5 for(int i=1;i<=2*n;i++) 6 { 7 cin>>a[i]; 8 sum1+=abs(a[i]); 9 sum2+=abs(a[i]+1); 10 } 11 int ans=sum1; 12 if(n==1) 13 { 14 ans=min(ans,abs(a[2]-a[1])); 15 cout<<ans<<"\n"; 16 return; 17 } 18 if(n==2) 19 { 20 int res=0; 21 for(int i=1;i<=2*n;i++)res+=abs(2-a[i]); 22 ans=min(res,ans); 23 } 24 if(n%2==0) 25 { 26 for(int i=1;i<=2*n;i++) 27 { 28 int res=abs(n-a[i]); 29 res=res-abs(a[i]+1)+sum2; 30 ans=min(res,ans); 31 //cout<<res<<"\n"; 32 } 33 } 34 cout<<ans<<"\n"; 35 }View Code
标签:Sequence,int,sum2,sum1,abs,Master,ans,CF1806C From: https://www.cnblogs.com/HikariFears/p/17255571.html