velimir

Karti

Mar 26th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i, j, n, k, l, count0=0, count1=0, cou=0, cou2=0;
  6. cin >> n >> k;
  7. int niza[n], tempNiza[n];
  8. for(i=0; i<n; i++)
  9. {
  10. cin >> niza[i];
  11. tempNiza[i] = niza[i];
  12. }
  13. for(i=0; i<n; i++)
  14. {
  15. if(tempNiza[i] == 0 and i+k<=n)
  16. {
  17. for(j=0, l=i; j<k; j++, l++)
  18. {
  19. if(tempNiza[l]==0)tempNiza[l] = 1;
  20. else tempNiza[l] = 0;
  21. }
  22. count1++;
  23. }
  24. if(niza[i] == 1 and i+k<=n)
  25. {
  26. for(j=0, l=i; j<k; j++, l++)
  27. {
  28. if(niza[l]==1)niza[l] = 0;
  29. else niza[l] = 1;
  30. }
  31. count0++;
  32. }
  33. }
  34. for(i=n-1; i>n-k; i--)
  35. if(niza[i]!=niza[i-1]) { cou++; break; }
  36. for(i=n-1; i>n-k; i--)
  37. if(tempNiza[i]!=tempNiza[i-1]) { cou2++; break; }
  38. if(cou!=0 and cou2!=0) { cout << "-1"; return 0; }
  39. if(count1<count0 and count1!=0 and cou2==0)cout << count1;
  40. else if(count0!=0 and cou==0)cout << count0;
  41. else cout << max(count0, count1);
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment