每日一题-week03

day01

将矩阵按对角线排序

快乐模拟

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
/*
将矩阵按对角线排序
2022-09-12 12:01:07
by ergou
*/
#include<bits/stdc++.h>
using namespace std;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
int a[105];
int cnt=0;
int n,m;
public:
void so(int x,int y,vector<vector<int>>& mat)
{
for(int xx=x,yy=y;xx<n&&yy<m;xx++,yy++,cnt++)
a[cnt]=mat[xx][yy];

sort(a,a+cnt);

for(int xx=x,yy=y,t=0;t<cnt;xx++,yy++,t++)
mat[xx][yy]=a[t];
}
vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
n=mat.size();
m=mat[0].size();
for(int i=0;i<m;i++){
cnt=0;
so(0,i,mat);
}

for(int i=1;i<n;i++)
{
cnt=0;
so(i,0,mat);
}
return mat;
}
};
//leetcode submit region end(Prohibit modification and deletion)


signed main()
{
Solution s;
return 0;
}

day02

逃离迷宫

简单搜索

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
2022/9/12 12:31
by ergou
*/
#include<bits/stdc++.h>

#define ll long long
#define bug(x) cout<<#x<<"x="<<x<<endl
using namespace std;

const int maxx = 1e5 + 5;
const int mod = 1e9 + 7;

inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
int m,n;
bool mp[105][105];
bool vis[105][105];
int ans[105][105];
int st[4][2]={{1,0},{-1,0},
{0,1},{0,-1}};
int stx,sty,enx,eny;
int k;
struct no{
int x,y,cnt,di;
//0->下, 1->上,2->右,3->左,4->任意
};
void so()
{
memset(vis,0,sizeof(vis));

cin>>n>>m;
char c;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>c;
if(c=='.') mp[i][j]=1;
else mp[i][j]=0;
}
//不小心把xy写反了,把这里换一下凑活着用吧
cin>>k>>sty>>stx>>eny>>enx;


vis[stx][sty]=1;
queue<no> q;
q.push({stx,sty,0,4});
while(!q.empty())
{
no a=q.front();
if(a.x==enx&&a.y==eny)
{
if(a.cnt>k) continue;
else
{
cout<<"yes\n";
return ;
}
}
q.pop();
int xx,yy,cnt;
for(int i=0;i<4;i++)
{
xx=a.x+st[i][0];
yy=a.y+st[i][1];
if(a.di==4) cnt=0;
else if(a.di!=i) cnt=a.cnt+1;
else if(a.di==i) cnt=a.cnt;


if(cnt>k) continue;
if(xx>0&&xx<=n&&yy>0&&yy<=m&&mp[xx][yy])
{
if(!vis[xx][yy])
{
vis[xx][yy]=1;
ans[xx][yy]=cnt;
q.push({xx,yy,cnt,i});
}
else
{
if(ans[xx][yy]>=cnt)
{
ans[xx][yy]=cnt;
q.push({xx,yy,cnt,i});
}
}
}
}
}

cout<<"no\n";
return ;
}

signed main() {
int t;
cin>>t;
while(t--)
{
so();
}
return 0;
}

day03

将数字变成 0 的操作次数

无聊模拟

这题是怎么混进去的

布吉岛

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
/*
将数字变成 0 的操作次数
2022-09-12 13:20:22
by ergou
*/
#include<bits/stdc++.h>
using namespace std;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
int numberOfSteps(int num) {
int cnt=0;
while(num)
{
if(num&1) num--;
else num >>= 1;
cnt++;
}
return cnt;
}
};
//leetcode submit region end(Prohibit modification and deletion)


signed main()
{
Solution s;
return 0;
}

day04

大小为 K 且平均值大于等于阈值的子数组数目

带点双指针的感觉

一开始用的long long,发现内存很丑,试了试int,没爆

好耶

image-20220912135604759
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
/*
大小为 K 且平均值大于等于阈值的子数组数目
2022-09-12 13:26:58
by ergou
*/
#include<bits/stdc++.h>
using namespace std;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
int numOfSubarrays(vector<int>& arr, int k, int threshold) {

int n=arr.size();
if(n<k) return 0;
int num=k*threshold;
int sum=0;
int ans=0;
for(int i=0;i<k;i++) sum+=arr[i];
if(sum>=num) ans++;

int l=0,r=k-1;
while(r+1<n)
{
sum-=arr[l++];
sum+=arr[++r];
if(sum>=num) ans++;
}
return ans;
}
};
//leetcode submit region end(Prohibit modification and deletion)


signed main()
{
Solution s;
return 0;
}

day05

时钟指针的夹角

快乐算数题

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
/*
时钟指针的夹角
2022-09-12 13:57:30
by ergou
*/
#include<bits/stdc++.h>
using namespace std;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
double angleClock(int hour, int minutes) {
double x=abs((hour*60+minutes)*0.5-minutes*6.0);
if(x>180) return 360-x;
return x;
}
};
//leetcode submit region end(Prohibit modification and deletion)


signed main()
{
Solution s;
return 0;
}

day06

跳跃游戏 IV

一开始以为是记忆化搜索或者剪枝

模了几次觉得不太对劲的样子

看题解居然是bfs…

大意了

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
跳跃游戏 IV
2022-09-08 17:42:24
by ergou
*/
#include<bits/stdc++.h>
using namespace std;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
unordered_map<int,vector<int> > mp;
bool vis[50005]={0};
int dis[50005]={0};
int n;
public:
int minJumps(vector<int>& arr) {
n=arr.size();

for(int i=0;i<n;i++)
{
if(mp.count(arr[i])) mp[arr[i]].push_back(i);
else
{
vector<int> a;
a.push_back(i);
mp[arr[i]]=a;
}
}

queue<int> q;
vis[0]=1;
dis[0]=0;
q.push(0);
while(!q.empty())
{
int u=q.front();
q.pop();
if(mp.count(arr[u]))
{
for(auto i:mp[arr[u]])
{
if(!vis[i])
{
vis[i]=1;
dis[i]=dis[u]+1;
q.push(i);
}

}
//这里要清除,不然如[1,1,...,1,2],这样的样会超时
//因为压入q中还是会被遍历map,相当于n^2复杂度
mp.erase(arr[u]);
}

if(u+1<n&&!vis[u+1])
{
vis[u+1]=1;
dis[u+1]=dis[u]+1;
q.push(u+1);
}
if(u-1>=0&&!vis[u-1])
{
vis[u-1]=1;
dis[u-1]=dis[u]+1;
q.push(u-1);
}
}
return dis[n-1];
}

};
//leetcode submit region end(Prohibit modification and deletion)


signed main()
{
Solution s;

return 0;
}