Skip to content

Commit fcbe129

Browse files
committed
.
1 parent 8add44f commit fcbe129

18 files changed

+136
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const int N=55,P=1000000007,mul=173;
2+
typedef long long ll;
3+
size_t get_hash(vector<int> &v){
4+
static size_t P=1000000007,M=173;
5+
int n=v.size(); size_t res=0;
6+
/*unordered_set<int> S;
7+
for (int i=0;i<n;++i)S.insert(v[i]);
8+
for (int i=0;i<S.bucket_count();++i){
9+
vector<int> u(S.begin(i),S.end(i)); sort(u.begin(),u.end());
10+
for (int j=0;j<u.size();++j)res=((long long)res*M+u[j])%P;
11+
}*/
12+
sort(v.begin(),v.end());
13+
for (int j=0;j<v.size();++j)res=((long long)res*M+v[j])%P;
14+
return ((long long)res*M+n)%P;
15+
}
16+
struct point{
17+
int x,y;
18+
}q[N*N];
19+
int pw[N*N];
20+
class Solution {
21+
public:
22+
int numDistinctIslands2(vector<vector<int>>& grid) {
23+
int n=grid.size(),m=grid[0].size(),ans=0;
24+
unordered_set<int> S;
25+
for (int i=0;i<n;++i)
26+
for (int j=0;j<m;++j)
27+
if (grid[i][j]){
28+
int h=0,t=1,xmin=i,xmax=i,ymin=j,ymax=j;
29+
q[0].x=i; q[0].y=j; grid[i][j]=0;
30+
while (h<t){
31+
point &p=q[h++]; int i=p.x,j=p.y;
32+
if (i+1<n&&grid[i+1][j]){
33+
q[t].x=i+1; q[t++].y=j;
34+
grid[i+1][j]=0; if (i+1>xmax)xmax=i+1;
35+
}
36+
if (i-1>=0&&grid[i-1][j]){
37+
q[t].x=i-1; q[t++].y=j;
38+
grid[i-1][j]=0; if (i-1<xmin)xmin=i-1;
39+
}
40+
if (j+1<m&&grid[i][j+1]){
41+
q[t].x=i; q[t++].y=j+1;
42+
grid[i][j+1]=0; if (j+1>ymax)ymax=j+1;
43+
}
44+
if (j-1>=0&&grid[i][j-1]){
45+
q[t].x=i; q[t++].y=j-1;
46+
grid[i][j-1]=0; if (j-1<ymin)ymin=j-1;
47+
}
48+
}
49+
for (int k=0;k<t;++k)q[k].x-=xmin,q[k].y-=ymin;
50+
int W=xmax-xmin+1,H=ymax-ymin+1,tmp,h1=1;
51+
//vector<int> v;
52+
for (int I=0;I<=1;++I){
53+
for (int J=0;J<4;++J){
54+
int h=0;
55+
for (int k=0;k<t;++k)h=(h+pw[q[k].x*H+q[k].y])%P;
56+
h=((ll)h*mul+W)%P; h=((ll)h*mul+H)%P;
57+
h1=(ll)h1*h; //v.push_back(h);
58+
if (J<4){
59+
for (int k=0;k<t;++k)tmp=W-1-q[k].x,q[k].x=q[k].y,q[k].y=tmp;
60+
swap(W,H);
61+
}
62+
}
63+
if (!I)for (int k=0;k<t;++k)q[k].x=W-1-q[k].x;
64+
}
65+
//h1=get_hash(v);
66+
if (S.find(h1)==S.end())S.insert(h1),++ans;
67+
}
68+
return ans;
69+
}
70+
};
71+
72+
//IO
73+
int _IO=[](){
74+
ios::sync_with_stdio(0);
75+
cin.tie(0); //cout.tie(0);
76+
for (int i=0,j=1;i<N*N;++i)
77+
pw[i]=j,j=(ll)j*mul%P;
78+
return 0;
79+
}();
80+
81+
Binary file not shown.

0701-0800/711. Number of Distinct Islands II.txt

-4
This file was deleted.

0901-1000/932. Beautiful Array.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
divide and conquer. put all even numbers on the left half, and all odd numbers on the right half, then recurse. O(n).
2+

0901-1000/962. Maximum Width Ramp.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
monotone stack. O(n).
2+

1401-1500/1494. Parallel Courses II.txt

-5
This file was deleted.
Binary file not shown.

1501-1600/1564. Put Boxes Into the Warehouse I.txt

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const int N=100005;
2+
void radix_sort(int A[],int l,int r){ //a[i]>=0
3+
//const int base=65535,W=16;
4+
const int base=2047,W=11;
5+
//const int base=255,W=8;
6+
const int T=(32-1)/W+1;
7+
static int B[N],s[base+1];
8+
A+=l-1;r-=l-1;l=1;
9+
int *a=A,*b=B,x=0;
10+
for (int i1=1;i1<=T;++i1){
11+
for (int i=0;i<=base;++i)s[i]=0;
12+
for (int i=1;i<=r;++i)++s[a[i]>>x&base];
13+
for (int i=1;i<=base;++i)s[i]+=s[i-1];
14+
for (int i=r;i>=1;--i)b[s[a[i]>>x&base]--]=a[i];
15+
int *tmp=a;a=b;b=tmp;
16+
x+=W;
17+
}
18+
if (a!=A)for (int i=1;i<=r;++i)A[i]=a[i];
19+
}
20+
class Solution {
21+
public:
22+
int maxBoxesInWarehouse(vector<int>& b, vector<int>& w) {
23+
int ans=0,n=b.size(),p1=0,p2=w.size()-1;
24+
radix_sort(&b[0],0,n-1);
25+
for (int i=n-1;i>=0&&p1<=p2;--i){
26+
if (b[i]<=w[p1])++p1,++ans;
27+
else if (b[i]<=w[p2])--p2,++ans;
28+
}
29+
return ans;
30+
}
31+
};
32+
33+
//IO
34+
int _IO=[](){
35+
ios::sync_with_stdio(0);
36+
cin.tie(0); //cout.tie(0);
37+
return 0;
38+
}();
39+
40+
Binary file not shown.

1501-1600/1580. Put Boxes Into the Warehouse II.txt

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. counting the number of occurrences of each variable. O(n).
2+
2. random evaluation. O(n).
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
greedily increment the rightmost character. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
prefix sum and suffix sum. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sort by minimum-actual in decreasing order, then greedily complete the rightmost task at the end. O(sort(n)).
2+

figs/1580.png

181 KB
Loading

figs/711.png

162 KB
Loading

0 commit comments

Comments
 (0)