# |
Author |
Problem |
Lang |
Verdict |
Time |
Memory |
Sent |
Judged |
|
87406562 |
Practice:
k4droid3 |
1340B
- 36
|
C++17 (GCC 7-32)
|
Time limit exceeded on test 7
|
1000 ms
|
19492 KB
|
2020-07-20 10:11:28 |
2020-07-20 10:11:29 |
|
#include <iostream>
#include <cstring>
using namespace std;
int cache[2001][2001]; //{n, k} //-1 = not explored; -2 = explored but not possible; else = ans at that index
void printans(int arr[], int hashes[], int n, int k){
for(int i=0; i<n; i++){
cout << cache[i][k];
k = k - __builtin_popcount(hashes[cache[i][k]]) + __builtin_popcount(arr[i]);
}
}
bool rec(int arr[], int hashes[], int index, int n, int k){
if(index == n){
if(k == 0)
return true;
else
return false;
}
for(int i=9; ~i; i--){
if((hashes[i] & arr[index]) == arr[index]){
int new_k = k - __builtin_popcount(hashes[i]) + __builtin_popcount(arr[index]);
if(new_k >=0 && cache[index+1][new_k] != -1){
if(cache[index+1][new_k] != -2)
return true;
}
else if(rec(arr, hashes, index+1, n, new_k)){
cache[index][k] = i;
return true;
}
else
cache[index][k] = -2;
}
}
return false;
}
int main() {
#ifdef ks
freopen("a.in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
memset(cache, -1, sizeof(cache));
int n, k;
cin >> n >> k;
int hashes[10] = {119, 18, 93, 91, 58, 107, 111, 82, 127, 123}; //converted binaries to numbers
int arr[n];
for(int i=0; i<n; i++){
string s;
cin >> s;
arr[i] = stoi(s, 0, 2);
}
if(rec(arr, hashes, 0, n, k)){
printans(arr, hashes, n, k);
}
// for(int i=0; i<=n; i++){
// for(int j=0; j<=k; j++)
// cout << cache[i][j] << '\t';
// cout << '\n';
// }
else
cout << -1;
}
Click to see test details