Attendance problems, there are pit
Can be directly determined in each case, note that a = 9, b = 1 in the case.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a, b; 7 cin >> a >> b; 8 if(a == 9 && b == 1) cout << a << " 10" << endl; 9 else if(a > b) cout << "-1" << endl; 10 else if(a == b) cout << a << "1 " << b << "2" << endl; 11 else if(a + 1 == b) cout << a << "9 " << b << "0" << endl; 12 else cout << "-1" << endl; 13 return 0; 14 }
B2. TV Subscriptions (Hard Version)
Simple version to violence, nothing to say.
Difficulty edition:
I is calculated from the answers to the 1-d, recorded, and define two pointers L R, L point 1, R point d, the case of using the map to record a program for later, to define the record every answer temp
That the map [a [l]] -; if map [a [L]] == 0 then the a [L] program is not purchased, temp--; and L ++;
Let R ++, if the map [a [R]] == 0, the need to purchase, temp ++; and map [a [R]] ++;
Finally ans = min (ans, temp)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 using namespace std; 5 const int N = 2e5 + 10; 6 7 int a[N]; 8 9 int main() 10 { 11 int t; 12 cin >> t; 13 while(t--) 14 { 15 map<int, int> m; 16 int n, k, d; 17 cin >> n >> k >> d; 18 for(int i = 1; i <= n; i++) cin >> a[i]; 19 int ans = 0; 20 for(int i = 1; i <= d; i++) 21 { 22 if(m[a[i]] == 0) ans++; 23 m[a[i]]++; 24 } 25 int tmp = ans; 26 int l = 1, r = d; 27 while(r < n) 28 { 29 m[a[l]]--; 30 if(m[a[l]] == 0) tmp--; 31 l++; r++; 32 if(m[a[r]] == 0) tmp++; 33 m[a[r]]++; 34 ans = min(ans, tmp); 35 } 36 cout << ans << endl; 37 } 38 return 0; 39 }
Violence did not have to say
n = (2^x1 + p) + (2^x2 + p ) + ...
Conversion at n - p * cnt 2 ^ x1 + 2 ^ x2 + 2 ^ x3 =;
Obviously violence, enumeration cnt, starting at 1, the boundaries are (n - p * cnt)> 0, because the right> 0;
Setting x = n - cnt * p; x is calculated with a binary number 1, if the number of 1 <= cnt and x is> = cnt, the solution described is the smallest cnt
As for why,
First: the number of revolutions of any one of a binary, i.e., t = 1011, for example, t = 2 ^ 0 + 2 ^ 1 + 2 ^ 3, one can see that each are different, so if the number 1> cnt, then, is It can not match;
As it will be less because may be incorporated i.e. 2 ^ 2 + 2 ^ 2 ^ 3 = 2
Second: x must be greater than or equal cnt, as is the power of 2, i.e., the right side is a minimum value cnt number 2 ^ 0 = cnt, it must be greater than x cnt.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
. 1 #include <cstdio> 2 #include <the cmath> . 3 #include <the iostream> . 4 the using namespace STD; . 5 . 6 int n-, P; . 7 . 8 // calculate x has several. 1 . 9 int CAL ( int x) 10 { . 11 int RES = 0 ; 12 is the while (X) 13 is { 14 IF (X & . 1 ) ++ RES ; 15 X = >> . 1 ; 16 } . 17 return res; 18 } 19 20 int main() 21 { 22 cin >> n >> p; 23 int ans = 1; 24 while((n - ans * p) > 0) 25 { 26 int x = n - ans * p; 27 int cnt = cal(x); 28 if(cnt <= ans && x >= ans) 29 { 30 cout << ans << endl; 31 return 0; 32 } 33 ans++; 34 } 35 cout << "-1" << endl; 36 return 0; 37 }