I was trying to solve MCARDS problem on spoj at http://www.spoj.com/problems/MCARDS/
I know it involves Longest increasing subsequnce logic , but after many attempts I did not figure out solution of this question , so I search for solution I find below solution:
int go (vector < int > &v) {
int ans = 1;
int n = v.size();
vector < int > d(n, 1);
for(int i = 1; i < n; ++i) {
int mmax = -1;
for(int j = 0; j < i; ++j) {
if(v[j] < v[i] && (mmax == -1 || d[mmax] < d[j])) {
mmax = j;
}
}
if(mmax != -1)
d[i] += d[mmax];
ans = max(ans, d[i]);
}
return ans;
}
int main () {
int test_case;
#ifndef ONLINE_JUDGE
IN("/home/tigran/Desktop/Debug/input.txt");
OUT("/home/tigran/Desktop/Debug/output.txt");
scanf("%d", &test_case);
#else
test_case = 1;
#endif
while(test_case--) {
int c, n;
scanf("%d%d", &c, &n);
int t = n * c;
vector < int > colors(t), values(t);
for(int i = 0; i < t; ++i) {
scanf("%d%d", &colors[i], &values[i]);
}
vector < int > ind;
for(int i = 0; i < c; ++i) {
ind.push_back(i);
}
int mmin = IINF;
vector < int > v(t);
do {
int cnt = 0;
for(int i = 0; i < c; ++i) {
for(int j = 0; j < n; ++j) {
mat[ind[i]][j] = cnt++;
}
}
for(int i = 0; i < t; ++i) {
v[i] = mat[colors[i] - 1][values[i] - 1];
}
mmin = min(mmin, t - go(v));
}while(next_permutation(ind.begin(), ind.end()));
printf("%d\n", mmin);
}
return 0;
}
Whats the logic behind permutation in above solution?
Thanks in advance