Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define sf(nn) scanf ("%d", &nn)
  5. #define sfll(nn) scanf ("%lld", &nn)
  6. #define pf printf
  7. #define casepf(nn) printf ("Case %d: ",nn)
  8. #define out(nn) cout <<nn <<endl
  9. #define loop(var,start,till) for(int var=start; var<till; var++)
  10. #define pb push_back
  11. #define mem(a,b) memset(a,b,sizeof(a))
  12. #define mp make_pair
  13. #define ll long long int
  14. #define mx 5005
  15. #define mod 100000007
  16. #define intinf 2100000000
  17. #define llinf 2e18
  18. #define READ(f) freopen(f,"r",stdin)
  19. #define WRITE(f) freopen(f,"w",stdout)
  20. #define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
  21.  
  22. int arr[17][107],sum[17],dp[17][105][1550],n,m,total=0,ii;
  23.  
  24.  
  25. int call(int pos,int type,int cur)
  26. {
  27.  
  28. if(pos<0) return 0;
  29.  
  30. if(dp[pos][type][cur] != -1) return dp[pos][type][cur];
  31.  
  32. int ans1=intinf,ans2=intinf,cnt=0,i,temp=0,ccnnt,ans=0,lastone;
  33.  
  34. if(type == 0)
  35. {
  36. cnt=0;
  37. ccnnt=-1;
  38. temp=0;
  39. for(i=0; i<m+2; i++)
  40. {
  41. ccnnt++;
  42. if(arr[pos][i] == 1) {
  43. ans+=i-type+cur;
  44. //ans += cur;
  45. cur=0;
  46. type=i; ///lastone
  47. }
  48. }
  49.  
  50. }
  51. else
  52. {
  53. cnt=0;
  54. ccnnt=-1;
  55. temp=0;
  56. for(int i=m+1; i>=0; i--)
  57. {
  58. ccnnt++;
  59. if(arr[pos][i] == 1) {
  60. ans += type-i+cur;
  61. //ans += cur;
  62. cur=0;
  63. type=i; ///lastone
  64. }
  65.  
  66. }
  67.  
  68. }
  69.  
  70. ans += min(call(pos-1,0,cur+type+1),call(pos-1,m+1,cur+m+2-type));
  71.  
  72. return dp[pos][type][cur] = ans;
  73. }
  74.  
  75. int main()
  76. {
  77. //int m;
  78. scanf("%d %d", &n,&m);
  79.  
  80. string s;
  81.  
  82. loop(i,0,n)
  83. {//cout<<i<<endl;
  84. cin>> s;
  85. for(int j=0; j<m+2; j++)
  86. {
  87. int temp = s[j];
  88. temp -= 48;
  89. arr[i][j] = temp;//cout<<arr[i][j];
  90. }
  91. }
  92.  
  93. loop(i,0,n){
  94. loop(j,0,m+2) {/*sf(arr[i][j]);*/ (j==0)? sum[i] = arr[i][j] : sum[i] = sum[i]+arr[i][j];}
  95. total += sum[i];
  96. }//cout<<total<<endl;
  97.  
  98. memset(dp,-1,sizeof(dp));//cout<<ii<<endl;
  99. cout<<call(n-1,0,0)<<endl;
  100. return 0;
  101. }
  102.  
  103. /*
  104. 3 4
  105. 0 0 1 0 0 0
  106. 0 0 0 0 1 0
  107. 0 0 0 0 1 0
  108.  
  109. 4 3
  110. 0 1 1 1 0
  111. 0 1 1 1 0
  112. 0 1 1 1 0
  113. 0 1 1 1 0
  114.  
  115. 2 2
  116. 0 0 1 0
  117. 0 1 0 0
  118.  
  119. 3 2
  120. 0000
  121. 0100
  122. 0100
  123.  
  124. 5 93
  125.  
  126. 00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010
  127.  
  128. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  129.  
  130. 00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  131.  
  132. 00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000
  133.  
  134. 00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000
  135.  
  136.  
  137. 2 21
  138. 00100110100010010010010
  139. 01000001111001010000000
  140.  
  141. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement