UVALive6832 - Bit String Reordering
水题
总结
不应该改变s数组,而是cur数组里面的数
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 20; 7 const int INF = 0x3f3f3f3f; 8 int n, m, t; 9 int s[maxn], cur0[maxn], cur1[maxn]; 10 int main() 11 { 12 // freopen("in.txt","r",stdin); 13 while(cin >> n >> m){ 14 memset(s, 0, sizeof s); 15 int num_0 = 0, num_1 = 0; 16 for(int i = 0; i < n; i++) 17 cin >> s[i]; 18 int k = 0; 19 bool flag = false; 20 for(int i = 0; i < m; i++){ 21 cin >> t; 22 while(t--){ 23 if(flag){ 24 cur0[k] = 1; 25 cur1[k++] = 0; 26 } 27 else{ 28 cur0[k] = 0; 29 cur1[k++] = 1; 30 } 31 } 32 flag = !flag; 33 } 34 int cnt_0 = 0, cnt_1 = 0; 35 for(int i = 0; i < n; i++){ 36 if(s[i] != cur0[i]) 37 for(int j = i+1; j < n; j++) 38 if(s[i] == cur0[j]){ 39 swap(cur0[j], cur0[i]); 40 cnt_0 += j-i; 41 break; 42 } 43 if(s[i] != cur0[i]) { 44 cnt_0 = INF; 45 break; 46 } 47 } 48 for(int i = 0; i < n; i++){ 49 if(s[i] != cur1[i]) 50 for(int j = i+1; j < n; j++) 51 if(s[i] == cur1[j]){ 52 swap(cur1[j], cur1[i]); 53 cnt_1 += j-i; 54 break; 55 } 56 if(s[i] != cur1[i]) { 57 cnt_1 = INF; 58 break; 59 } 60 } 61 cout << min(cnt_0, cnt_1) << endl; 62 } 63 return 0; 64 }