NOT
".
NOT A
", after that, all digits of A will be reversed. (e.g. A=
1001101
, after operation "
NOT A
", A will be
0110010
).
NOT
" for some of his numbers.
For each case, first output the case number as "Case #x: ", and x is the case number. Then you should output an integer, indicating the maximum result that Elfness can get.
2 5 6 100100 001100 010001 010001 111111 5 7 0001101 0001011 0010011 0111000 1001011
Case #1: 51 Case #2: 103
题目意思是有多个01串,可以选择反转或者不反转,求出最大值与最小值的最大差
看完题目后,直接暴力,提交后毫无悬念的超时了,这个题的关键是怎么找到最大值与最小值,因为
一个串你只能选择一个值,但又不能保证选择一个值得情况下,就是最好的结果,想了好久也没
想明白,看了别人的代码后,才明白,最大值或最小值是由二进制得来的,应该用异或来解决
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int n,m,h; char str[100]; long long a[20010]; long long pow(int k){ return 1LL<<k; } long long solve(){ if(n==1) return 0; if(a[0]^a[h-1]) return max(a[h-2]-a[0],a[h-1]-a[1]); else return a[h-1]-a[0]; } int main(){ long long Max,p,q; int i,j,k,t,l=1; scanf("%d",&t); while(t--){ h=0; scanf("%d%d",&n,&m); for(k=0;k<n;k++){ scanf("%s",str); p=0; q=0; for(i=m-1,j=0;i>=0;i--,j++){ if(str[i]=='0') p+=pow(j); else q+=pow(j); } a[h++]=p; a[h++]=q; } sort(a,a+h); printf("Case #%d: %lld\n",l++,solve()); } return 0; }