SRM 715

Topcoder SRM 715

Q1 : ImageCompression

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ImageCompression {
public String isPossible(String[] input, int k) {
boolean status = false;
for(int i = 0; i < input.length; i+=k) {
for(int j = 0; j < input[0].length(); j+=k) {
char start = input[i].charAt(j);
for(int l = i; l < i + k; l++)
for(int m = j; m < j + k; m++)
if (input[l].charAt(m) != start) {
System.out.println(l);
System.out.println(m);
return "Impossible";
}
}
}
return "Possible";
}
}

Q2: MaximumRangeDiv2

This question should be solved by greedy algorithm. But I didn’t realize that and choose to use brute force. I failed at only one test cases

Q3)

Failed to came up with the solution, I reference an accept user Abni_Saini:

Code Ref:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
#define rep(i,a,n) for(long long int i = (a); i <= (n); ++i)
#define repI(i,a,n) for(int i = (a); i <= (n); ++i)
#define repD(i,a,n) for(long long int i = (a); i >= (n); --i)
#define repDI(i,a,n) for(int i = (a); i >= (n); --i)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
typedef pair<lld,lld> PA;
class InPrePost{
public:
bool check(vector<int> A,vector<int> B){
sort(A.begin(),A.end());
sort(B.begin(),B.end());
int n = A.size();
if(n!=B.size()) return false;
rep(i,0,n-1) if(A[i]!=B[i]) return false;
return true;
}
bool tellMe(vector<string> S, vector<int> a1, vector<int> a2, vector<int> a3){
// Check if there are elements not equal.
if(!check(a1,a2) || !check(a1,a3)) return false;
int n = a1.size();
if(n<=1) return true;
int root = a1[0];
vector<int> a1l,a1r,a2l,a2r,a3l,a3r;
int leftn = -1;
rep(i,0,n-1){
if(a2[i]==root){
leftn = i;
}
else if(leftn==-1){
a2l.pb(a2[i]);
}
else a2r.pb(a2[i]);
}
rep(i,1,n-1){
if(i<=leftn) a1l.pb(a1[i]);
else a1r.pb(a1[i]);
}
rep(i,0,n-2){
if(i<leftn) a3l.pb(a3[i]);
else a3r.pb(a3[i]);
}
map<string,vector<int> > M;
M[S[0]] = a1l;
M[S[2]] = a2l;
M[S[4]] = a3l;
if(!tellMe(S,M["pre"],M["in"],M["post"])) return false;
M[S[1]] = a1r;
M[S[3]] = a2r;
M[S[5]] = a3r;
if(!tellMe(S,M["pre"],M["in"],M["post"])) return false;
return true;
}
string isPossible(vector<string> S, vector<int> a1, vector<int> a2, vector<int> a3){
if(tellMe(S,a1,a2,a3)) return "Possible";
else return "Impossible";
}
};
Share Comments