版本比较
zhclg个人题目题单题解
正文
增加 122 行删除 2 行
19 行未改动202122232425262743 行未改动717273747576777820 行未改动9910010110210310410510610710849 行未改动158159160161162163164165166139 行未改动全部展开30630730830931031131231350 行未改动36436536636736836937037191 行未改动46346446546619 行未改动## [地图行走](https://www.luogu.com.cn/problem/U578916)地图搜索算法,深搜、广搜均可,在这里介绍深搜做法。```cpp#include<iostream>using namespace std;int n,m,ans=0;43 行未改动看到数据范围就炸了,考虑**前缀和**。这题还是个二维的,画个图就能推出前缀和式子。具体见代码。```cpp#include<iostream>#define int long long20 行未改动## [玩小飞棍](https://www.luogu.com.cn/problem/U605731)搜。不要爆搜。剪枝。爆索应该都会,剪枝就是发现**任何一个**比边长要长了就回溯。```cpp#include<bits/stdc++.h>#define ll long long#define endl '\n'49 行未改动设$m$ 表示$k$所有因数的积。容易想到,因数个数为奇数的要加上$n \div m$,否则剪掉。时间复杂度$O((\log k)^2)$,足够优秀。```cpp#include<bits/stdc++.h>#define int long long139 行未改动全部展开首先在树上跑一个dfs,记录一下时间戳(in时间和out时间),读入操作之后,借助跑出来的时间戳,进行差分。最后借助差分结果与in时间戳,统计答案。```cpp#include<bits/stdc++.h>using namespace std;const int MAXN=1e5+10;50 行未改动其实跑两遍两个人就行了,没什么难的,只是偏移量特殊一点。坑:注意**过滤空格**```cpp#include<bits/stdc++.h>using namespace std;const int dx8[]={-1, -1, -1, 0, 0, 1, 1, 1};91 行未改动}return 0;}```19 行未改动2021222324252627282943 行未改动7374757677787980818220 行未改动10310410510610710810911011111211311449 行未改动164165166167168169170171172139 行未改动全部展开31231331431531631731831932032150 行未改动37237337437537637737837938038191 行未改动47347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558619 行未改动## [地图行走](https://www.luogu.com.cn/problem/U578916)地图搜索算法,深搜、广搜均可,在这里介绍深搜做法。时间复杂度$O(nm)$,但又很多额外开销。```cpp#include<iostream>using namespace std;int n,m,ans=0;43 行未改动看到数据范围就炸了,考虑**前缀和**。这题还是个二维的,画个图就能推出前缀和式子。时间复杂度$O(nm)$具体见代码。```cpp#include<iostream>#define int long long20 行未改动## [玩小飞棍](https://www.luogu.com.cn/problem/U605731)搜。不要爆搜。剪枝。爆搜应该都会,剪枝就是发现**任何一个**比边长要长了就回溯。理论复杂度$O(T\times2^n)$,但有了剪枝会很快。```cpp#include<bits/stdc++.h>#define ll long long#define endl '\n'49 行未改动设$m$ 表示$k$所有因数的积。容易想到,因数个数为奇数的要加上$n \div m$,否则剪掉。时间复杂度$O((\log k)^2)$。```cpp#include<bits/stdc++.h>#define int long long139 行未改动全部展开首先在树上跑一个dfs,记录一下时间戳(in时间和out时间),读入操作之后,借助跑出来的时间戳,进行差分。最后借助差分结果与in时间戳,统计答案。时间复杂度$O(n+q)$```cpp#include<bits/stdc++.h>using namespace std;const int MAXN=1e5+10;50 行未改动其实跑两遍两个人就行了,没什么难的,只是偏移量特殊一点。坑:注意**过滤空格**时间复杂度$O(nm)$```cpp#include<bits/stdc++.h>using namespace std;const int dx8[]={-1, -1, -1, 0, 0, 1, 1, 1};91 行未改动}return 0;}```## [竞赛真理](https://www.luogu.com.cn/problem/U631943)数据不大。考虑背包DP。首先,按结束时间排序。状态:$dp_{ij}$表述大脑1在时间i,大脑2在时间j时的最大分数。转移:当i在这题结束时间,有`dp[i][j]=max(dp[i][j],dp[a[idx].s-1][j]+a[idx].w);`否则,有`dp[i][j]=max(dp[i][j],dp[i-1][j]);`最后,大脑2和大脑1copy一下就可以了。时间复杂度$O(n\times t^2)$```cpp#include <bits/stdc++.h>typedef long long LL;using namespace std;const int MAXN = 110 ,MAXT = 510;LL t ,n ,dp[MAXT][MAXT] ,ans = 0;struct node {int s, e ,w;}a[MAXN];bool cmp(node a,node b) {return a.e < b.e;}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> t >> n;for (int i = 1;i <= n;++i) {cin >> a[i].s >> a[i].e >> a[i].w;}sort(a + 1 , a + 1 + n , cmp);//排序if (n == 1) {cout << a[1].w << '\n';return 0;//特判}dp[0][0] = 0;for (int idx = 1;idx <= n;++idx) {for (int i = a[idx].e;i <= t;++i) {for (int j = 0;j <= t;++j) {if (i == a[idx].e)dp[i][j]=max(dp[i][j],dp[a[idx].s-1][j]+a[idx].w);else dp[i][j]=max(dp[i][j],dp[i-1][j]);//dp式}}for (int i = 0;i < MAXT;++i) {for (int j = 0;j < MAXT;++j) {dp[i][j]=dp[j][i];//copy}}}cout <<dp[t][t];return 0;}```## [魔法水晶](https://www.luogu.com.cn/problem/U634573)“第k小”,很容易看出二分。在`check`中考虑用双指针优化至$O(n)$。注意要排序。时间复杂度$O(n\log n)$```cpp#include <bits/stdc++.h>using namespace std;typedef long long ll;int n;ll k ,a[100010];bool check(ll mid) {ll cnt = 0;int j = n - 1;//双指针for (int i = 0; i < n; i++) {while (j > i && (ll)a[i] + a[j] > mid) {j--;}if (j > i) {cnt += (j - i);}}return cnt >= k;}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> k;for (int i = 0; i < n; i++) {cin >> a[i];}sort(a, a+n);//排序ll l = (ll)a[0] + a[1] ,r = (ll)a[n-1] + a[n-2];ll ans = 0;while (l <= r) {//二分板子ll mid = l + (r - l) / 2;if (check(mid)) {ans = mid;r = mid - 1;}else {l = mid + 1;}}cout << ans << endl;return 0;}```