Skip to content

Commit 0d516f0

Browse files
committed
Practise 13-Jun-2020
1 parent f4c5142 commit 0d516f0

File tree

6 files changed

+512
-0
lines changed

6 files changed

+512
-0
lines changed
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Chef has an integer N and he wants to generate a matrix M with N rows (numbered 1 through N) and N columns (numbered 1 through N). He thinks that M would be delicious if:
3+
4+
Each element of this matrix is an integer between 1 and N2 inclusive.
5+
All the elements of the matrix are pairwise distinct.
6+
For each square submatrix containing cells in rows r through r+a and in columns c through c+a (inclusive) for some valid integers r, c and a≥0:
7+
Mr,c+Mr+a,c+a is even
8+
Mr,c+a+Mr+a,c is even
9+
Can you help Chef generate a delicious matrix? It can be proved that a solution always exists. If there are multiple solutions, you may find any one.
10+
11+
Input
12+
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
13+
The first and only line of each test case contains a single integer N.
14+
Output
15+
For each test case, print N lines describing a delicious matrix M. For each valid i, the i-th of these lines should contain N space-separated integers Mi,1,Mi,2,…,Mi,N.
16+
17+
Constraints
18+
1≤T≤10
19+
1≤N≤103
20+
the sum of N over all test cases does not exceed 103
21+
Subtasks
22+
Subtask #1 (100 points): original constraints
23+
24+
Example Input
25+
1
26+
2
27+
Example Output
28+
1 2
29+
4 3
30+
Explanation
31+
Example case 1: The matrix M has five square submatrices. Four of them ([1], [2], [4], [3]) have a=0, so they obviously satisfy all conditions. The last square submatrix is the whole matrix M, with r=c=a=1, and we can see that M1,1+M2,2=1+3=4 and M1,2+M2,1=2+4=6 are both even.
32+
*/
33+
34+
35+
36+
37+
38+
#include<bits/stdc++.h>
39+
using namespace std;
40+
41+
int main(){
42+
ios_base::sync_with_stdio(false);
43+
cin.tie(NULL);
44+
int t;
45+
cin>>t;
46+
while(t--){
47+
int n;
48+
cin>>n;
49+
int cnt=1;
50+
int a[n][n];
51+
if(n%2!=0){
52+
for(int i=0;i<n;i++){
53+
for(int j=0;j<n;j++)
54+
a[i][j]=cnt++;
55+
}
56+
} else {
57+
for(int i=0;i<n;i++){
58+
if(i%2==0){
59+
for(int j=0;j<n;j++)
60+
a[i][j]=cnt++;
61+
} else {
62+
for(int j=n-1;j>=0;j--)
63+
a[i][j]=cnt++;
64+
}
65+
66+
}
67+
}
68+
for(int i=0;i<n;i++){
69+
for(int j=0;j<n;j++)
70+
cout<<a[i][j]<<" ";
71+
cout<<endl;
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class RandomizedSet {
2+
public:
3+
unordered_map<int, int> m;
4+
vector<int>v;
5+
6+
/** Initialize your data structure here. */
7+
RandomizedSet() {
8+
9+
}
10+
11+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
12+
bool insert(int val) {
13+
if(m.find(val)!=m.end())
14+
return false;
15+
v.push_back(val);
16+
m.insert({val, v.size()-1});
17+
return true;
18+
19+
}
20+
21+
/** Removes a value from the set. Returns true if the set contained the specified element. */
22+
bool remove(int val) {
23+
if(m.find(val)==m.end())
24+
return false;
25+
int pos=m[val];
26+
v[pos]=v[v.size()-1];
27+
m[v[pos]]=pos;
28+
v.pop_back();
29+
m.erase(val);
30+
return true;
31+
}
32+
33+
/** Get a random element from the set. */
34+
int getRandom() {
35+
return v[rand()%v.size()];
36+
}
37+
};
38+
39+
/**
40+
* Your RandomizedSet object will be instantiated and called as such:
41+
* RandomizedSet* obj = new RandomizedSet();
42+
* bool param_1 = obj->insert(val);
43+
* bool param_2 = obj->remove(val);
44+
* int param_3 = obj->getRandom();
45+
*/
46+
47+
48+
49+
50+
51+
52+
class RandomizedSet {
53+
public:
54+
unordered_map<int, int> m;
55+
vector<int>v;
56+
57+
/** Initialize your data structure here. */
58+
RandomizedSet() {
59+
60+
}
61+
62+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
63+
bool insert(int val) {
64+
if(m.find(val)!=m.end())
65+
return false;
66+
v.push_back(val);
67+
m.insert({val, v.size()-1});
68+
return true;
69+
70+
}
71+
72+
/** Removes a value from the set. Returns true if the set contained the specified element. */
73+
bool remove(int val) {
74+
if(m.find(val)==m.end())
75+
return false;
76+
int pos=m[val];
77+
v[pos]=v[v.size()-1];
78+
m[v[pos]]=pos;
79+
v.pop_back();
80+
m.erase(val);
81+
return true;
82+
}
83+
84+
/** Get a random element from the set. */
85+
int getRandom() {
86+
return v[rand()%v.size()];
87+
}
88+
};
89+
90+
/**
91+
* Your RandomizedSet object will be instantiated and called as such:
92+
* RandomizedSet* obj = new RandomizedSet();
93+
* bool param_1 = obj->insert(val);
94+
* bool param_2 = obj->remove(val);
95+
* int param_3 = obj->getRandom();
96+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class RandomizedSet {
2+
public:
3+
unordered_map<int, int> m;
4+
vector<int>v;
5+
6+
/** Initialize your data structure here. */
7+
RandomizedSet() {
8+
9+
}
10+
11+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
12+
bool insert(int val) {
13+
if(m.find(val)!=m.end())
14+
return false;
15+
v.push_back(val);
16+
m.insert({val, v.size()-1});
17+
return true;
18+
19+
}
20+
21+
/** Removes a value from the set. Returns true if the set contained the specified element. */
22+
bool remove(int val) {
23+
if(m.find(val)==m.end())
24+
return false;
25+
int pos=m[val];
26+
v[pos]=v[v.size()-1];
27+
m[v[pos]]=pos;
28+
v.pop_back();
29+
m.erase(val);
30+
return true;
31+
}
32+
33+
/** Get a random element from the set. */
34+
int getRandom() {
35+
return v[rand()%v.size()];
36+
}
37+
};
38+
39+
/**
40+
* Your RandomizedSet object will be instantiated and called as such:
41+
* RandomizedSet* obj = new RandomizedSet();
42+
* bool param_1 = obj->insert(val);
43+
* bool param_2 = obj->remove(val);
44+
* int param_3 = obj->getRandom();
45+
*/
46+
47+
48+
49+
50+
51+
52+
class RandomizedSet {
53+
public:
54+
unordered_map<int, int> m;
55+
vector<int>v;
56+
57+
/** Initialize your data structure here. */
58+
RandomizedSet() {
59+
60+
}
61+
62+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
63+
bool insert(int val) {
64+
if(m.find(val)!=m.end())
65+
return false;
66+
v.push_back(val);
67+
m.insert({val, v.size()-1});
68+
return true;
69+
70+
}
71+
72+
/** Removes a value from the set. Returns true if the set contained the specified element. */
73+
bool remove(int val) {
74+
if(m.find(val)==m.end())
75+
return false;
76+
int pos=m[val];
77+
v[pos]=v[v.size()-1];
78+
m[v[pos]]=pos;
79+
v.pop_back();
80+
m.erase(val);
81+
return true;
82+
}
83+
84+
/** Get a random element from the set. */
85+
int getRandom() {
86+
return v[rand()%v.size()];
87+
}
88+
};
89+
90+
/**
91+
* Your RandomizedSet object will be instantiated and called as such:
92+
* RandomizedSet* obj = new RandomizedSet();
93+
* bool param_1 = obj->insert(val);
94+
* bool param_2 = obj->remove(val);
95+
* int param_3 = obj->getRandom();
96+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Given a boolean expression with following symbols.
3+
4+
Symbols
5+
'T' ---> true
6+
'F' ---> false
7+
8+
And following operators filled between symbols
9+
10+
Operators
11+
& ---> boolean AND
12+
| ---> boolean OR
13+
^ ---> boolean XOR
14+
15+
Count the number of ways we can parenthesize the expression so that the value of expression evaluates to true.
16+
17+
For Example:
18+
The expression is "T | T & F ^ T", it evaluates true
19+
in 4 ways ((T|T)&(F^T)), (T|(T&(F^T))), (((T|T)&F)^T)
20+
and (T|((T&F)^T)).
21+
22+
Return No_of_ways Mod 1003.
23+
24+
Input:
25+
First line contains the test cases T. 1<=T<=500
26+
Each test case have two lines
27+
First is length of string N. 1<=N<=100
28+
Second line is string S (boolean expression).
29+
Output:
30+
No of ways Mod 1003.
31+
32+
33+
Example:
34+
Input:
35+
2
36+
7
37+
T|T&F^T
38+
5
39+
T^F|F
40+
41+
Output:
42+
4
43+
2
44+
*/
45+
46+
47+
48+
49+
50+
#include<bits/stdc++.h>
51+
using namespace std;
52+
53+
int booleanParenthesis(string s, int i, int j, bool isTrue){
54+
if(i>j) return 0;
55+
if(i==j){
56+
if(isTrue==true)
57+
return s[i]=='T'?1:0;
58+
else return s[i]=='F'?1:0;
59+
}
60+
int ans=0;
61+
for(int k=i+1;k<j;k+=2){
62+
int lt=booleanParenthesis(s, i, k-1, true);
63+
int lf=booleanParenthesis(s, i, k-1, false);
64+
int rt=booleanParenthesis(s, k+1, j, true);
65+
int rf=booleanParenthesis(s, k+1, j, false);
66+
67+
if(s[k]=='&'){
68+
if(isTrue=true)
69+
ans+=lt*rt;
70+
else ans+=lt*rf + lf*rt + lf*rf;
71+
} else if(s[k]=='|'){
72+
if(isTrue==true)
73+
ans+=lt*rt + lt*rf + lf*rt;
74+
else ans+=lf*rf;
75+
} else if(s[k]=='^'){
76+
if(isTrue==true)
77+
ans+=lt*rf + lf*rt;
78+
else ans+=lf*rf + lt*rt;
79+
}
80+
}
81+
return ans;
82+
}
83+
84+
int main(){
85+
ios_base::sync_with_stdio(false);
86+
cin.tie(NULL);
87+
cout.tie(NULL);
88+
int t;
89+
cin>>t;
90+
while(t--){
91+
int n;
92+
cin>>n;
93+
string s;
94+
cin>>s;
95+
cout<<booleanParenthesis(s, 0, n-1, true)<<endl;
96+
}
97+
98+
return 0;
99+
}

0 commit comments

Comments
 (0)